AppleScript Programming/Letter Replacer

From Wikibooks, open books for an open world
Jump to: navigation, search

A problem:
"I need a script to change the unicode character to old Slovenian ASCII character because of some fonts we use. Right now we use find and replace in TextEdit, but it is a pain to do it on 50 documents and with 10 different replacing character in each text."


Result:
Automatic Letter Replacer

Just copy the code, paste it in ScriptEditor, edit all necessary info (read the comments), and save as a program. Then just happily drag and drop - a punch at a time, so you don't have to do it one by one.

Note: I would suggest testing the program with a single document first (with "autosave" off), as all the letters that you put into variables might not appear correct in TextEdit. The script has difficulties to respect - for example - Scandinavian letters "ä" and "ö".

More sophisticated version would save the changed document with different name, maybe even in different location. It could be a lot better, but current shall satisfy for now.

(*
This script works as "drag and drop", and utilizes TextEdit. It's purpose is to replace letters (or even lines) in
documents.

To make it work properly in the first place, you need to edit variables "find" and "replace" to resemble the actual 
wording as they appear in your TextEdit with your OS language set.

Do NOT interrupt the script in run by switching program, as result might be something nasty.

There is disabled abilities at the end of the script. You can enable them if you wish.

If you get NSReceiverEvaluationScriptError: 4 ("target object doesn't exist" or something like that) in middle of
the script in action, you can ignore it and re-run the script.

If you get the above error at every time you run the script, you got either button name or window name wrong
- if you changed them in the first place.

Brought to you by F-3000
*)

on run
        display dialog ¬
                "You need to drag and drop documents into this application in order it to work. Try again." buttons ¬
                {"OK"} default button 1
end run

on open (ListItem)
        set find to "Find" -- Name of the "Find"-window in TextEdit.
        set replace to "Replace All" -- Name of the "Replace All"-button in the Find-window in TextEdit.
        set TE to "TextEdit" -- Change this to the name as it shows in your language set, if you have problems
        -- with the english program name. I didn't have any problems - and I don't use english as OS language.
        
        set r11 to "a" -- Determines letter to be replaced.
        set r12 to "b" -- Determines replacing letter.
        set r21 to "c"
        set r22 to "d"
        set r31 to "e"
        set r32 to "f"
        set r41 to "g"
        set r42 to "h"
        set r51 to "i"
        set r52 to "j"
        set r61 to "k"
        set r62 to "l"
        set r71 to "m"
        set r72 to "n"
        set r81 to "o"
        set r82 to "p"
        set r91 to "q"
        set r92 to "r"
        set r101 to "s"
        set r102 to "t"
        
        -- Feel free to modify the length of the list if you wish, just also remember to do the same for "replacing
        -- script" as well.
        
        display dialog "You are about to replace 10 letters with others within " & ¬
                (count (ListItem)) & ¬
                " documents. You cannot abort the process in run. Also, remember to save the changes." buttons ¬
                {"Cancel", "OK"} default button 2
        
        
        repeat with thisItem in ListItem
                tell application TE
                        activate
                        open thisItem
                        tell application "System Events"
                                keystroke "f" using command down
                                keystroke r11
                                keystroke "        "
                                keystroke r12
                                tell process TE to click button replace in window find
                                keystroke "f" using command down
                                -- Above command is in repetative manner because it'll select the text in the first text-field in
                                -- the Find-window.
                                keystroke r21
                                keystroke "        "
                                keystroke r22
                                tell process TE to click button replace in window find
                                keystroke "f" using command down
                                keystroke r31
                                keystroke "        "
                                keystroke r32
                                tell process TE to click button replace in window find
                                keystroke "f" using command down
                                keystroke r41
                                keystroke "        "
                                keystroke r42
                                tell process TE to click button replace in window find
                                keystroke "f" using command down
                                keystroke r51
                                keystroke "        "
                                keystroke r52
                                tell process TE to click button replace in window find
                                keystroke "f" using command down
                                keystroke r61
                                keystroke "        "
                                keystroke r62
                                tell process TE to click button replace in window find
                                keystroke "f" using command down
                                keystroke r71
                                keystroke "        "
                                keystroke r72
                                tell process TE to click button replace in window find
                                keystroke "f" using command down
                                keystroke r81
                                keystroke "        "
                                keystroke r82
                                tell process TE to click button replace in window find
                                keystroke "f" using command down
                                keystroke r91
                                keystroke "        "
                                keystroke r92
                                tell process TE to click button replace in window find
                                keystroke "f" using command down
                                keystroke r101
                                keystroke "        "
                                keystroke r102
                                tell process TE to click button replace in window find
                                keystroke "w" using command down
                                -- keystroke "s" using command down
                                -- keystroke "w" using command down
                                -- If you want to enable "save and close document", just take the double-lines (that makes
                                -- them mere comments) off before the two above commands.
                        end tell
                end tell
                beep -- Will note that a single document is done.
        end repeat
        -- tell application TE to quit
        beep 3 -- Will note that the *whole* process is over.
end open