【问题标题】:Find/replace in Keynote with Applescript使用 Applescript 在 Keynote 中查找/替换
【发布时间】:2014-09-12 21:09:09
【问题描述】:

我终其一生都无法弄清楚如何在 Keynote 6.2 中自动执行查找/替换功能。

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

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

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

谁能帮忙?

【问题讨论】:

    标签: replace find applescript keynote


    【解决方案1】:

    只是根据调整您链接到的内容以使用我的 v6.2.2 的 Keynote 进行此操作。希望这可以帮助您入门(注意 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
    

    之前: 后:

    [编辑] 要在您的主题项目中执行文本项目(与“标题对象”或“正文对象”相反),您可以执行以下操作(请注意,这是区分大小写的,当然,要执行多个项目,您将重复循环):

    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
    

    之前和之后:

    [编辑两个]

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

    【讨论】:

    • 我已经尝试过了,但似乎没有任何反应。我应该指出,我想搜索/替换文档中文本的所有实例——而不仅仅是当前幻灯片。另外,我对标题本身并不真正感兴趣——文本的任何实例都应该更改。我可以有效地定位它吗?
    • 您必须重复循环播放幻灯片。但这对我有用。如果我们想深入了解它为什么不适合您,您将不得不提供更多线索/代码。
    • 我不使用文本标题和正文。我的布局中只有文本项,所以这些不是此搜索功能的目标。
    • 好的。我即将更新答案以仅包含文本项。如果您能提供更多关于 1)项目结构和 2)您尝试过的代码的更多信息,那将非常有帮助。
    • 我有一个 Excel 电子表格,每行有 6 个条目。我必须将这些值插入到我已在其中输入值占位符的主题模板中(例如地址、名称等)。我想将每行中数据的第 1-6 项搜索/替换为相应的主题演讲。一些数据在主题演讲中有多个实例。到目前为止,我只是使用applescript激活excel,输入击键将选择器移动到下一个值,复制,返回keynote,搜索该列占位符标题,全部替换,返回excel并重复。
    猜你喜欢
    • 1970-01-01
    • 2013-12-17
    • 2015-03-13
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 2014-01-14
    • 2021-07-26
    相关资源
    最近更新 更多