将富文本从一个对象的属性复制到另一个对象

时间:2017-08-23 14:34:57

标签: applescript omnifocus

我尝试使用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的新手,所以感谢任何帮助;)

3 个答案:

答案 0 :(得分:1)

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

也许这有帮助。

代码:

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

答案 1 :(得分:0)

AppleScript本身不理解样式文本,并且没有标准的方法让应用程序返回附加了样式信息的文本,因此:

set task_note to note of the_task

仅返回纯文本。

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

set note of new_task to note of the_task

该命令是否有效取决于应用程序脚本支持的实现程度;你只需要尝试看看。

答案 2 :(得分:0)

我实际上通过创建新任务并将其项目和上下文分配给现有任务来解决这个问题有点不直观,这样我就不需要重新创建注释了。可能有更好的方法来获取项目和上下文,虽然我找不到更好的方法来执行它涉及查找嵌套项目/上下文

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