2

Im building an app for learning purposes of using applescript to send multiple commands during an action. Below is the code I'm messing with but I have stripped out the actions in between the "" and replaced them with numbers. Everything works fine in applescript but making this into an NSApplescript initwithsource: line has been a bother.

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

What is the best way to combine this applescript into a single line? Thanks!

4

1 に答える 1

4

「このアップルスクリプトを 1 行にまとめる最良の方法は何ですか?」

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

(このスクリプトは完全ではないことに注意してください。提供されたような単純なスクリプトでは問題なく動作しますが、それ自体を変換することはできません)。

それを、前に明らかにした Scripts フォルダーにスクリプトとして保存します。

次に、変換したいスクリプト ドキュメントを開き、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"
于 2011-09-26T02:40:26.450 に答える