【发布时间】: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