跳轉到內容

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
下一頁: 計算器 | 上一頁: 溫度轉換器
主頁: AppleScript 程式設計/示例程式
華夏公益教科書