【发布时间】:2022-02-05 12:56:33
【问题描述】:
我有多个 Pages 文档,我需要在其中替换一组特殊字符 - 在我们的语言中,我们有一个字符的介词(例如 v、s、k、u、a),这些介词最后不能被孤立行数,所以我需要用介词和不可破坏的空格替换介词和下一个空格。一直在尝试像这样使用 AppleScript(我是编程新手):
set findList to {"v ", "s "}
set replaceList to {"v ", "s "}
set AppleScript's text item delimiters to ""
tell application "Pages"
activate
tell body text of front document
repeat with i from 1 to count of findList
set word of (words where it is (item i of findList)) to (item i of replaceList)
end repeat
end tell
end tell
return
只要findList 和replaceList 参数中有空格,这将不起作用。
所以我发现text item delimiters 可能对我有帮助。我能够制作这个脚本
set theText to "Some of my text with v in it"
set AppleScript's text item delimiters to "v "
set theTextItems to text items of theText
set AppleScript's text item delimiters to "v " --this is v with non-breakable space (alt+space)
set theText to theTextItems as string
set AppleScript's text item delimiters to {""}
theText
这可行,但只能在代码的第一行设置纯文本(当我将结果复制到 Pages 时,确实有一个不可破坏的空间)。
但现在我需要编写一个脚本,它适用于 Pages 文档的整个文本。
我尝试过这样的事情:
tell application "Pages"
activate
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "v "
set textItems to body text of front document
set AppleScript's text item delimiters to "v " --again v with non-breakable space (alt+space)
tell textItems to set editedText to beginning & "v " & rest --again v with non-breakable space (alt+space)
set AppleScript's text item delimiters to astid
set text of document 1 to editedText
end tell
但我得到了错误
无法从“这是 Pages 文档的整个文本”开始。“从插入点 1 开始的编号 -1728 以及“”的整个文本文件"
如果我将脚本更改为:
tell application "Pages"
activate
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "v "
set textItems to text items of body text of front document
set AppleScript's text item delimiters to "v "
tell textItems to set editedText to beginning & "v " & rest
set AppleScript's text item delimiters to astid
set text of document 1 to editedText
end tell
我得到另一个错误
页面出现错误:无法获取文档 1 正文的每个文本项。" 从文档 1 正文的每个文本项中获取编号 -1728
谁能指出正确的方向如何正确编写此脚本?
谢谢。
【问题讨论】:
标签: replace applescript document delimiter