Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Also.. AppleScript to Find and Replace Word in Pages

Also...


An AppleScript to Find and Replace a word...


Ex: Replace all words such as " banana " for " pumpkin "


Same function as Find and Replace in the Pages app but in an AppleScript


Thanks!!!!

AppleScript

Posted on Aug 30, 2013 3:54 AM

Reply
9 replies

Aug 30, 2013 9:12 AM in response to thebluedrg

If you use Replace All be sure your original word is not contained within another word such as "work" within "reworked" because Replace All will literally replace ALL and is not undoable. In this instance you would need to type space work sapce in the Find box. To be safe, save your document just before doing Replace All so that you can Restore if necessary.


Walt

Aug 30, 2013 10:05 AM in response to Walt K

Good point, Walt, although specifying " banana " (with the trailing and leading spaces) would leave any instance of banana at the beginning of a paragraph or the end of a sentence still in the document.


Checking "Whole words" in the Advanced pane of Find/Replace might be a better choice.

User uploaded file


Regards,

Barry

Aug 30, 2013 10:10 AM in response to Barry

Thanks for all your answers but my original question was .. how to create an AppleScript .


I'm pretty familiar with Find n Replace.


Reasons I wanted an AppleScript are..


1) I have over 60 words to replace in the document

2) I have over 200 documents to go through

3) I will insert this script with two other AppleScript (combine 3 of them - automated)

4) Solution have to be automated - not manual.


Any help regarding AppleScript would be appreciated.


Thanks

Aug 31, 2013 3:53 AM in response to thebluedrg

You may try the following script. You need to enable System Preferences > Universal Access > Enabel access for assistive devices.


Regards,

H


(*
    find & replace > advanced (Pages 09)
*)
Pages_replace_all_advanced("banana", "pumpkin", {|whole word|:1, |match case|:0})

on Pages_replace_all_advanced(x, y, options)
    (*
        string x : search string
        string y : replace string
        record options : search & replace options
                |search area| : 1 = entire document, 2 = main text body (default = 1)
                |match case| : 0 = disabled, 1 = enabled (default = 0)
                |whole word| : 0 = disabled, 1 = enabled (default = 0)
                |search loop| : 0 = disabled, 1 = enabled (default = 1)
    *)
    set options to options & {|search area|:1, |match case|:0, |whole word|:0, |search loop|:1}
    
    tell application "Pages"
        if not (exists document 1) then return
        activate
    end tell
    tell application "System Events"
        tell process "Pages"
            set find_panel_new to not (exists (window 1 whose subrole = "AXDialog"))
            if find_panel_new then
                keystroke "f" using command down
                repeat until exists (window 1 whose subrole = "AXDialog")
                    delay 0.2
                end repeat
            end if
            
            tell (window 1 whose subrole = "AXDialog") -- find & replace window
                tell tab group 1
                    tell radio button 2 -- 1 = simple, 2 = advanced
                        click
                    end tell
                    tell checkbox 1 -- search previous text (loop)
                        if value ≠ options's |search loop| then click
                    end tell
                    tell checkbox 2 -- match case
                        if value ≠ options's |match case| then click
                    end tell
                    tell checkbox 3 -- whole word
                        if value ≠ options's |whole word| then click
                    end tell
                    tell pop up button 1 -- search area (entire document | main text body)
                        click
                        tell menu 1's menu item (options's |search area|) to click
                    end tell
                    
                    tell scroll area 1's text area 1 -- find
                        set value to x
                    end tell
                    tell scroll area 2's text area 1 -- replace
                        set value to y
                    end tell
                    tell button 3 -- replace all
                        click
                    end tell
                end tell
                if find_panel_new then click (button 1 whose subrole = "AXCloseButton")
            end tell
        end tell
    end tell
end Pages_replace_all_advanced

Also.. AppleScript to Find and Replace Word in Pages

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.