AppleScript 编程/示例程序/字母替换
外观
< AppleScript 编程 | 示例程序
问题
"我需要一个脚本将 Unicode 字符转换为旧的斯洛文尼亚 ASCII 字符,因为我们使用了一些字体。目前我们使用 TextEdit 中的查找和替换功能,但对 50 个文档进行操作,并且每个文本都需要替换 10 个不同的字符,这太麻烦了。"
结果
自动字母替换
只需复制代码,将其粘贴到 ScriptEditor 中,编辑所有必要的信息(阅读注释),并保存为程序。然后,您只需快乐地拖放 - 一次一个,这样您就不必一个一个地操作了。
注意:我建议先用单个文档(关闭“自动保存”)测试程序,因为您在变量中输入的所有字母可能在 TextEdit 中显示不正确。脚本难以尊重 - 例如 - 斯堪的纳维亚字母“ä”和“ö”。
更复杂的版本将保存更改后的文档,并使用不同的名称,甚至可能在不同的位置。它可以做得更好,但目前应该足够了。
(* This script works as "drag and drop", and utilizes TextEdit. Its 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 repetitive 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