使用Applescript在Keynote中查找/替换

时间:2014-09-12 21:09:09

标签: replace find applescript keynote

在我的生活中,我无法弄清楚如何在Keynote 6.2中自动执行查找/替换功能。

我的目标是在整个文档中查找/替换。我有剪贴板中的文本,我想用。替换字符串。

我最接近的是此页面上的脚本,但它在Keynote 6.2中不起作用。

http://macscripter.net/viewtopic.php?id=26603

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

这是基于调整你链接的内容来使用我的vyn.2的v6.2.2。希望这可以帮助您入门(请注意AppleScript词典中的Keynote示例):

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

在: enter image description here 后: enter image description here

<强> [编辑] 要在主题演讲项目中执行文本项目(而不是&#39;标题对象&#39;或&#39;正文对象&#39;),您可以执行以下操作(请注意,这是区分大小写的,并且,当然,要做多个项目,你将重复循环):

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

之前和之后:

enter image description here

enter image description here

[编辑二]

以下代码要求用户搜索和替换字符串(使用两个对话框),并用替换的版本替换每个文本项的每个匹配项。奇怪的是,它包括default body itemdefault 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