使用 AppleScript 替换多个 Word 文档中的多个短语

问题描述 投票:0回答:1

我一直在尝试使用 Gemini 来帮助我为标题中的任务编写脚本。

我尝试了这段代码:

-- Set up the phrases to find and their replacements (ensure proper formatting)

set textToFindList to {"May 23, 2023", "Aboriginal Day", "June 21", "2023"}

set textReplaceWithList to {"May 24, 2024", "National Indigenous Peoples Day", "June 20", "2024"}

-- Ensure the lists are the same length
if (length of textToFindList) is not equal to (length of textReplaceWithList) then return

-- Choose the folder containing the Word documents
set folderPath to POSIX file "/Users/stirlingtait/Documents/Donation Letters"

tell application "Finder"
    -- Look for both .docx and .doc files
    set fileList to every file of folder folderPath whose name extension is {"doc"}
end tell

tell application "Microsoft Word"
    repeat with fileRef in fileList
        open (fileRef as alias)
        set doc to active document
        tell doc

            -- Print the list content for verification (optional)
            -- display dialog textToFindList
        
            repeat with i from 1 to length of textToFindList
                set oldPhrase to item i of textToFindList
                set newPhrase to item i of textReplaceWithList
            
                -- Create a new find object and set its properties
                set findObject to find object of text object of selection
                tell findObject
                    set content to oldPhrase
                    set replacement text to newPhrase
                    set forward to true
                    set wrap to find stop
                    execute replace all with match case -- Replace all occurrences
                end tell
            end repeat
            save
            close
        end tell
    end repeat
end tell

display dialog "Replacement complete!"

并收到此错误:

错误“无法将缺失值的 «class 1650» 设置为“2023 年 5 月 23 日”。” 来自“1650 级”缺失值的数字 -10006

ms-word applescript google-gemini
1个回答
0
投票

我遇到了一些我不记得的问题,这些问题可以是新引入的,但说实话,我使用 AppleScripting Word 的经验已经很老了,而且不是很广泛。

主要问题似乎是

  • 您不能只引用所选内容(即使单词词典建议您可以
  • 试图告诉
    FindObject
    有些东西似乎适用于
    content
    属性,但不适用于
    replacement
    属性
  • 如果您的测试未到达代码的该部分,您可能没有发现一些语法错误

这是在这里工作的代码 - 您可能需要检查查找 doc 和 docx 的代码(也许您更改了它以简化测试?)

-- Set up the phrases to find and their replacements (ensure proper formatting)

set textToFindList to {"May 23, 2023", "Aboriginal Day", "June 21", "2023"}

set textReplaceWithList to {"May 24, 2024", "National Indigenous Peoples Day", "June 20", "2024"}

-- Ensure the lists are the same length
if (length of textToFindList) is not equal to (length of textReplaceWithList) then return

-- Choose the folder containing the Word documents
set folderPath to POSIX file "/Users/stirlingtait/Documents/Donation Letters"    

tell application "Finder"
    -- Look for both .docx and .doc files
    set fileList to every file of folder folderPath whose name extension is {"doc"}
end tell


    tell application "Microsoft Word"
        repeat with fileRef in fileList
            open (fileRef as alias)
            -- setting doc to active document caused problems later
            tell active document
                - I seemed to have to select something
                select text object
                -- Print the list content for verification (optional)
                -- display dialog textToFindList
                
                repeat with i from 1 to length of textToFindList
                    set oldPhrase to item i of textToFindList
                    set newPhrase to item i of textReplaceWithList
                    
                    -- Create a new find object and set its properties
                    -- I had to specify ' of active pane of active window'
                    set findObject to find object of selection of active pane of active window

                    -- I had to omit the tell FindObject block and spell everything out
                    set content of findObject to oldPhrase

                    -- changed 'replacement text' -> 'content of replacement'
                    set content of replacement of findObject to newPhrase
                    set forward of findObject to true
                    set wrap of findObject to find stop
                    
                    --  a couple of syntax changes in here.
                    execute find findObject replace replace all with match case -- Replace all occurrences              
                end repeat
                save
                close
            end tell
        end repeat
    end tell
    
    display dialog "Replacement complete!"
© www.soinside.com 2019 - 2024. All rights reserved.