回覆列表
  • 1 # 使用者3707185764512444

    關於AppleScript說到AppleScript,可能涉及到三個含義:

    1.AppleScript語言:就是蘋果指令碼的語言,用來編寫運行於Mac系統的指令碼。

    2.AppleScript指令碼:就是使用AppleScript語言編寫的一系列指令。以後簡稱指令碼。

    3.AppleScript指令碼解釋程式:用於解釋和執行AppleScript指令碼中指令的程式。是Mac系統的原生程式和重要組成部分。下面簡稱AppleScript指令碼程式。AppleScript Editor要編寫AppleScript指令碼,當然就需要編輯工具了,首先開啟AppleScript編輯器:貌似可以使用Xcode編寫Cocoa - AppleScript Application,這個暫時還沒嘗試。我們先從AppleScript編輯器開始。介面很簡單,主要分為工具欄、編輯區和執行結果區。編輯區用來編寫指令碼,工具欄用來編譯和執行指令碼等,執行結果區呈現執行結果,這個不用多說了吧。say和beep首先來玩兩件很好玩的事:讓Mac發音和說話。在編輯區輸入以下指令碼:view sourceprint?1.say "How are you?" using "Zarvox"2.say "Fine, thank you." using "Victoria"3.say "Ha Ha"4.beep點選工具欄的編譯(或Command + K),可以看到指令碼變了顏色,具體顏色的含義,可以在偏好設定中檢視並設定:接著點選執行(Command + R執行,Command + .停止執行)。可以看到執行結果區的回覆欄中的顯示如下:更加好玩的是Mac系統在自然自語了,還有最後的一聲咚。下面來解釋下指令碼和執行結果的意思:view sourceprint?1.say "說話的內容" using "指定人的嗓音"這句指令碼的意思就是讓Mac系統按照指定人的嗓音說出我們指定的說話內容。當然using "..."是可以預設的。view sourceprint?1.beep 發聲次數這句指令碼的意思是讓Mac系統發出咚的聲音,發聲次數決定了咚出現的次數,例如beep 2那麼Mac會咚兩次。發聲次數預設為1。在執行時回覆輸出的意思是:tell表明將以上say和beep的任務交付給current application去完成,也就是AppleScript指令碼程式。tell模組同樣道理,我們可以用tell模組將特定的任務交付給Mac中特定的程式去執行。tell模組的語法為:view sourceprint?1.tell application "Application Name"2.do your job here3.end tell例如我們想讓Finder程式清空垃圾簍然後開啟磁碟,可以這樣寫:view sourceprint?1.tell application "Finder"2.empty the trash3.beep4.open the startup disk5.end tell注意"Finder"的雙引號必不可少。回覆輸出為:view sourceprint?01.tell application "Finder"02.empty trash03.--> current application04.--> error number 005.beep06.--> error number -1000407.end tell08.tell current application09.beep10.end tell11.tell application "Finder"12.open startup disk13.end tell可以看到empty the trash任務是交給Finder程式去完成的,而beep任務是交給current application程式去完成,接下來的open the startup disk任務還是交回給Finder程式完成。在tell模組中的任意位置可以插入beep,say等由current application程式解釋執行的語句。注意,交付給某個程式執行的語句必須在tell模組之內。錯誤寫法:view sourceprint?1.tell application "Finder"2.empty the trash3.beep4.end tell5.open the startup disk編譯時可以看到沒有報錯,執行起來看看:執行結果區的回覆輸出如下:view sourceprint?01.tell application "Finder"02.empty trash03.--> current application04.--> error number 005.beep06.--> error number -1000407.end tell08.tell current application09.beep10.end tell11.tell application "AppleScript Editor"12.open startup disk13.--> missing value14.end tell15.結果:16.missing valueboot應該由Finder程式去開啟,但是由於超出了tell applicaiton "Finder"模組,所以給語句將交由AppleScript Editor程式解釋執行,很可惜,AppleScript編輯器無法開啟boot,報錯。AppleScript Editor的小技巧1.使用esc鍵撥出程式碼補全選單在寫te時按下esc鍵,此時出現代

  • 中秋節和大豐收的關聯?
  • 西方女性為何人高馬大?