2011-09-26 30 views
2

我構建了一個應用程序,用於學習使用applescript在操作過程中發送多個命令的目的。下面是我搞亂的代碼,但我已經刪除了「」之間的操作,並用數字替換了它們。一切工作正常在蘋果腳本,但使這成爲一個NSApplescript initwithsource:行一直是一個麻煩。將AppleScript更改爲單行NSApplescript源碼

tell application "Terminal" 
    activate 
    set currentTab to do script "1" 
    do script "2" in currentTab 
    do script "3" in currentTab 
    do script "4" in currentTab 
    delay 0.5 
    tell application "Finder" to set visible of process "Terminal" to false 
end tell 

什麼是最好的方式來結合這個蘋果腳本成一條線?謝謝! 「

回答

4

」將這個applescript合併成一條線的最佳方法是什麼?「

使用AppleScript? :-D

首先,在AppleScript編輯器中,打開「首選項」窗口並單擊選項Show Script menu in menu bar

然後從屏幕右上方的腳本菜單項中選擇Open Scripts Folder

創建一個新的AppleScript .scptd與下面的腳本文件:

tell application "AppleScript Editor" 
    set string_ to text of first document 

    -- make a list with each line of the script 
    set stringLines to paragraphs of string_ 
    set originalDelims to AppleScript's text item delimiters 

    -- add newlines 
    set AppleScript's text item delimiters to "\\n" 

    -- now combine the items in the list using newlines 
    set stringNewlines to stringLines as string 

    set AppleScript's text item delimiters to "\"" 
    set stringNewlines to text items of stringNewlines 
    set AppleScript's text item delimiters to "\\\"" 
    set stringNewlines to stringNewlines as string 

    set AppleScript's text item delimiters to originalDelims 
    set stringNewlines to "@\"" & stringNewlines & "\"" 

    set the clipboard to stringNewlines 
end tell 

(注意,該腳本並不完美:它工作正常進行簡單的腳本,就像您所提供的一個,但不能自行轉換)。

將它保存爲之前顯示的腳本文件夾中的腳本。

然後打開您想要轉換的腳本文檔,並使其成爲AppleScript編輯器中的前端文檔。然後通過從「腳本」菜單中選擇它來調用轉換腳本。

給你提供的腳本,它應該會產生以下常數NSString

@"tell application \"Terminal\"\n activate\n set currentTab to do script \"1\"\n do script \"2\" in currentTab\n do script \"3\" in currentTab\n do script \"4\" in currentTab\n delay 0.5\n tell application \"Finder\" to set visible of process \"Terminal\" to false\nend tell\n" 
+0

謝謝您的幫助是花了一段時間,我最終得到由小破它,它在可可應用工作並且將applescript語法寫出來。我不僅僅使用applescript文件的唯一原因是我需要根據給出的信息動態地改變發送到終端窗口的行。到目前爲止,它的工作,我剛剛得到其他問題來解決大聲笑!感謝NSString系列,我會檢查它,看看它是否適用於我正在做的事情。我真的不知道你可以用蘋果做這一切,這將有助於很多 – Kyle