【问题标题】:Copying rich text from the property of one object to another将富文本从一个对象的属性复制到另一个对象
【发布时间】:2018-02-01 05:15:22
【问题描述】:

我正在尝试使用 AppleScript 来查找现有的 OmniFocus 任务并根据某些规则附加项目和上下文。这是有效的,除了:

当我创建新任务时,我试图直接复制 note 属性。在 OmniFocus 的字典中,它说 note 属性是“富文本”,但在新任务中它似乎已经变成纯文本(特别是,我想保留的文本中的链接正在消失,但是是其他正在消失的风格)

on set_project_and_context(the_task, the_project, the_context)
    tell application "OmniFocus"
        tell front document
            set task_name to name of the_task
            set task_note to note of the_task
            set new_text to task_name & " ::" & the_project & " @" & the_context
            set new_tasks to (parse tasks into with transport text new_text with as single task)
            set new_task to item 1 of new_tasks
            set due date of new_task to missing value
            set note of new_task to task_note # <- HERE IS WHERE I'M TRYING TO COPY THE NOTE
            delete the_task
        end tell
    end tell
end set_project_and_context

我是 AppleScript 新手,感谢任何帮助 ;)

【问题讨论】:

    标签: applescript omnifocus


    【解决方案1】:

    您不能直接复制注释属性,因为新注释将是没有所有样式和链接的纯文本。 要保留格式,您需要设置note 属性的每个段落的style 属性。我添加了一个处理程序来设置它。

    也许这会有所帮助。

    代码:

    on set_project_and_context(the_task, the_project, the_context)
        tell application "OmniFocus"
            tell document 1
                set task_name to name of the_task
                set task_note to note of the_task
                set new_text to task_name & " ::" & the_project & " @" & the_context
                set new_tasks to (parse tasks into it with transport text new_text with as single task)
                set new_task to item 1 of new_tasks
                set due date of new_task to missing value
                my SetNote(the_task, new_task) -- NEW HANDLER
                delete the_task
            end tell
        end tell
    end set_project_and_context
    
    on SetNote(old_task, new_task)
        using terms from application "OmniFocus"
            set text of note of new_task to text of note of old_task
            set lst_paragraphs to (every paragraph of note of old_task)
            repeat with i from 1 to count lst_paragraphs
                set style of paragraph i of note of new_task to (style of paragraph i of note of old_task)
            end repeat
        end using terms from
    end SetNote
    

    【讨论】:

    • 谢谢你,但不幸的是,当段落中间有样式(如线条)时,它对我不起作用。我将paragraph 更改为word,这确实有效,但看起来很奇怪而且速度很慢。我实际上想出了一个从根本上更好的方法,我将作为另一个答案分享它
    【解决方案2】:

    AppleScript 本身不理解带样式的文本,并且没有标准的方式让应用程序返回带有样式信息的文本,所以:

    set task_note to note of the_task
    

    只返回纯文本。

    您需要告诉应用程序将富文本直接从一个属性复制到另一个属性:

    set note of new_task to note of the_task
    

    该命令是否有效将取决于应用的脚本支持的实现情况;你只需要试试看。

    【讨论】:

    • 当我尝试得到OmniFocus got an error: Wrong type for note. Should be text or rich text, "OSPropertyScriptText"。可能是因为 OmniFocus 的脚本支持不好?
    • 我没有 OF 的副本可以在这里玩,所以我在猜测语法。 Omni 拥有良好的用户支持和论坛,因此请先尝试在那里提问。否则,实验。尝试set text of note of new_task to text of note of the_taskset text of note of new_task to note of the_task 以及您能想到的任何其他变体,看看您是否可以得到它来产生所需的结果。杂乱无章的行为和令人沮丧的不完整文档是大多数“AppleScriptable”应用程序的标准“功能”;有时你只需要用一根棍子戳它,希望能碰到有用的东西。
    • 是的,到目前为止我已经经历了一些;)。谢谢!
    【解决方案3】:

    我实际上通过创建新任务并将其项目和上下文分配给现有任务来解决这个问题有点不直观,这样我就不需要重新创建笔记。可能有更好的方法来获取项目和上下文,尽管我找不到更好的方法来找到嵌套的项目/上下文

    on set_project_and_context(the_task, project_text, context_text)
        tell application "OmniFocus"
            tell front document
                set task_name to name of the_task
                set new_text to task_name & " ::" & project_text & " @" & context_text
                set new_tasks to (parse tasks into with transport text new_text with as single task)
                set new_task to item 1 of new_tasks
    
                set context of the_task to context of new_task
                set the_project to containing project of new_task
                set project_name to name of the_project
                move the_task to end of tasks of the_project
                delete new_task
            end tell
        end tell
    end set_project_and_context
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-08
      • 2016-12-21
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多