Pages App 内的 Applescript 文本解析

Applescript text parsing inside Pages App

我正在使用 applescript 编写 Pages 表单模板脚本。我正在使用此处教程中的代码:https://iworkautomation.com/pages/script-tags-placeholder-text.html

该脚本工作正常,但是,我需要能够为一些占位符文本字段插入一个列表。我的计划是使用分隔符,并拆分我正在使用此处代码的字符串:https://erikslab.com/2007/08/31/applescript-how-to-split-a-string/

似乎当您在 tell application pages 块中时,文本项对象很特殊,用分隔符解析字符串的正常方法将不起作用。

我收到的错误是“页面出现错误:文档 ID“E1303B92-B79A-4786-841B-EC5F46ACB05D”不理解“findAndReplaceInText”消息。

这里是相关的代码片段:

tell application "Pages"
-- ...

        -- PROMPT USER FOR REPLACEMENT TEXT
        set searchString to ";;"
        set crlf to return & linefeed
        repeat with i from 1 to the count of uniqueTags
            set thisTag to item i of uniqueTags
            display dialog "Enter the replacement text for this tag:" & ¬
                return & return & thisTag default answer "" buttons ¬
                {"Cancel", "Skip", "OK"} default button 3
            copy the result to {button returned:buttonPressed, text returned:replacementString}
            if buttonPressed is "OK" then
                set replacementString to findAndReplaceInText(replacementString, searchString, crlf)
                set (every placeholder text whose tag is thisTag) to replacementString
            end if
        end repeat
    end tell
end tell

on findAndReplaceInText(theText, theSearchString, theReplacementString)
    set AppleScript's text item delimiters to theSearchString
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to theReplacementString
    set theText to theTextItems as string
    set AppleScript's text item delimiters to ""
    return theText
end findAndReplaceInText

It seems that when you're inside a tell application pages block, that text item objects are special, and the normal way of parsing strings with delimiters will not work.

不,这只是目标范围的问题。这是合法的:

on sayHowdy()
    display dialog "howdy"
end sayHowdy
sayHowdy()

这不是:

on sayHowdy()
    display dialog "howdy"
end sayHowdy
tell application "Finder"
    sayHowdy() -- error
end tell

要修复它,我们需要告诉 Finder sayHowdy 属于我们,而不属于它。为此,请使用 my:

on sayHowdy()
    display dialog "howdy"
end sayHowdy
tell application "Finder"
    my sayHowdy()
end tell