我无法为自己的生活弄清楚如何在Keynote 6.2中自动执行查找/替换功能。在Keynote中查找/替换为AppleScript
我的目标是在整个文档中查找/替换。我从剪贴板中获取了要替换字符串的文本。
我来最接近的是此页面上的脚本,但它并不在主题演讲6.2工作。
http://macscripter.net/viewtopic.php?id=26603
任何人都可以帮忙吗?
我无法为自己的生活弄清楚如何在Keynote 6.2中自动执行查找/替换功能。在Keynote中查找/替换为AppleScript
我的目标是在整个文档中查找/替换。我从剪贴板中获取了要替换字符串的文本。
我来最接近的是此页面上的脚本,但它并不在主题演讲6.2工作。
http://macscripter.net/viewtopic.php?id=26603
任何人都可以帮忙吗?
只是做此基础上调整你挂什么与我的主题演讲V6.2.2工作。希望这可以让你开始(注意主题的AppleScript字典的例子):
tell application "Keynote"
tell document 1
set slidebody to object text of default body item of current slide
set slidebody to my searchnreplace("foo", "BEE", slidebody)
set object text of default body item of slide 1 to slidebody
set slidetitle to object text of default title item of current slide
set slidetitle to my searchnreplace("foo", "AAK", slidetitle)
set object text of default title item of slide 1 to slidetitle
end tell
end tell
-- I am a very old search & replace function...
on searchnreplace(searchstr, replacestr, txt)
considering case, diacriticals and punctuation
if txt contains searchstr then
set olddelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {searchstr}
set txtitems to text items of txt
set AppleScript's text item delimiters to {replacestr}
set txt to txtitems as Unicode text
set AppleScript's text item delimiters to olddelims
end if
end considering
return txt
end searchnreplace
前: 后:
[编辑] 要做到在您的演讲文本项目项目(与“标题对象”或“主体对象”相对),可以执行以下操作(请注意,这是区分大小写的,当然,要执行多个项目,您需要重复循环):
tell application "Keynote"
tell document 1
set textObjectText to object text of text item 1 of current slide
set adjustedText to my searchnreplace("Foo", "BEE", textObjectText)
set object text of text item 1 of slide 1 to adjustedText
end tell
end tell
-- I am the same very old search & replace function...
on searchnreplace(searchstr, replacestr, txt)
considering case, diacriticals and punctuation
if txt contains searchstr then
set olddelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {searchstr}
set txtitems to text items of txt
set AppleScript's text item delimiters to {replacestr}
set txt to txtitems as Unicode text
set AppleScript's text item delimiters to olddelims
end if
end considering
return txt
end searchnreplace
前后:
[EDIT TWO]
下面的代码询问用户用于搜索和替换字符串(使用两个对话框),和用替换的版本替换每个文本项的每一个出现。奇怪的是,它包括default body item
和default title item
,但不改变文本(为了做到这一点,你必须使用我第一次展示的例子的变体)。
tell application "Keynote"
activate
set q1 to display dialog "Enter Search String:" default answer "" buttons {"Cancel", "OK..."} default button 2
set s to (text returned of q1)
set q2 to display dialog "Enter Replace String:" default answer s buttons {"Cancel", "OK"} default button 2
set r to (text returned of q2)
tell document 1
set ii to (count of text items of current slide)
set textItemmax to ii
repeat with i from 1 to textItemmax by 1
set textObjectText to (object text of text item i of current slide) as text
set adjustedText to my searchnreplace(s, r, textObjectText)
set object text of text item i of current slide to adjustedText
end repeat
end tell
end tell
-- I am the same very old search & replace function...
on searchnreplace(searchstr, replacestr, txt)
considering case, diacriticals and punctuation
if txt contains searchstr then
set olddelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {searchstr}
set txtitems to text items of txt
set AppleScript's text item delimiters to {replacestr}
set txt to txtitems as Unicode text
set AppleScript's text item delimiters to olddelims
end if
end considering
return txt
end searchnreplace
[编辑三] 通过幻灯片和文本项目(我真的希望这可以让你想要的位置),这样就可以循环:
tell application "Keynote"
activate
set q1 to display dialog "Enter Search String:" default answer "" buttons {"Cancel", "OK..."} default button 2
set s to (text returned of q1)
set q2 to display dialog "Enter Replace String:" default answer s buttons {"Cancel", "OK"} default button 2
set r to (text returned of q2)
set slideMax to (count of slides of document 1)
repeat with slideIndex from 1 to slideMax by 1
set ii to (count of text items of slide slideIndex of document 1)
set textItemmax to ii
repeat with i from 1 to textItemmax by 1
set textObjectText to (object text of text item i of slide slideIndex of document 1)
set adjustedText to my searchnreplace(s, r, textObjectText as text)
set object text of text item i of slide slideIndex of document 1 to (adjustedText as text)
end repeat
end repeat
end tell
-- I am the same very old search & replace function...
on searchnreplace(searchstr, replacestr, txt)
considering case, diacriticals and punctuation
if txt contains searchstr then
set olddelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {searchstr}
set txtitems to text items of txt
set AppleScript's text item delimiters to {replacestr}
set txt to txtitems as Unicode text
set AppleScript's text item delimiters to olddelims
end if
end considering
return txt
end searchnreplace
我已经试过这一点,似乎没有任何发生。我应该指出,我想搜索/替换文档中的所有文本实例 - 而不仅仅是当前的幻灯片。另外,我对标题本身并不感兴趣 - 任何文本实例都应该改变。我可以有效地瞄准吗? – 2014-09-19 18:40:53
您必须重复循环浏览幻灯片。但这对我很有用。如果我们想深入了解为什么它不适合你,你将不得不提供更多的线索/代码。 – CRGreen 2014-09-19 20:00:19
我不使用文本标题和正文。我的布局中只有文本项目,因此这些搜索功能不是针对这些项目的。 – 2014-09-19 22:25:27