【问题标题】:Applescript text parsing inside Pages AppPages App 中的 Applescript 文本解析
【发布时间】:2021-03-20 12:42:14
【问题描述】:

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

脚本运行良好,但是,我需要能够为某些占位符文本字段插入一个列表。我的计划是使用分隔符,并从这里使用代码拆分字符串:https://erikslab.com/2007/08/31/applescript-how-to-split-a-string/

似乎当您在一个告诉应用程序页面块中时,文本项对象是特殊的,并且使用分隔符解析字符串的正常方法将不起作用。

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

这里是相关代码sn-p:

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

【问题讨论】:

  • 您在主脚本中使用了“replacementString”和“searchString”,但在处理程序中使用了“theReplacementString”和“theSearchString”。如果你让它们保持一致,脚本至少会运行。

标签: applescript


【解决方案1】:

似乎当您在一个告诉应用程序页面块中时,文本项对象是特殊的,并且使用分隔符解析字符串的正常方法将不起作用。

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

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

【讨论】:

  • 这非常有效,谢谢。当我将处理程序中的代码直接放入循环时,我感到很困惑,我得到了同样的错误。由于页面文本项对象,我认为范围仅在处理程序中有效,这是否正确?有没有办法限制循环内的代码,以便不需要处理程序?
  • 您没有显示足够的代码来重现该问题。但你总是可以在tell document 1 块内放置一个tell me 块或其他任何东西。
猜你喜欢
  • 2014-02-12
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
相关资源
最近更新 更多