*趣旨 [#hdfc4a81] applescriptを独学するため サイトに記載されている例を元に逆引きの技術をつくってみる **参考にしたサイト [#yf8f1afd] http://tukaikta.blog135.fc2.com/blog-entry-67.html *ボタンを表示するダイアログ [#pc54f515] display dialog "Choose VNC or SSH" buttons {"VNC", "SSH", "Cancel"} default button 3 with icon note *ダイアログの結果を得る [#r8c94f5f] copy the result as list to {buttonp} *条件分岐 [#d1490793] if the buttonp is "VNC" then else if the buttonp is "SSH" then end if *変数の代入 [#z18ee450] set aUserName to "--ユーザー名" *文字列の連結 [#oc4595d1] "vnc://" & aUserName & ":" & aPassword *失敗するかもしれない文の実行 [#jd53f334] try end try *すでに開いているアプリケーションを利用 [#e8bed1c4] tell application "Terminal" to activate *新規にアプリケーションを利用 [#xcbb4a93] tell application "Terminal" *開いているタブの数を数える [#rfdb3a57] set wCount to count (every window whose visible is true) *新規ウィンドウでコマンド実行 [#uf06b351] do script aCMD *既存のウィンドウでコマンド実行 [#ia108fb6] do script aCMD in front window *コマンドを実行したタブを変数に格納する [#zafd9a69] set currentTab to do script "echo do something" *同一のシェルウィンドウに複数のコマンドを送る方法 [#y7d10b8f] tell application "Terminal" set currentTab to do script "echo do something" do script "echo do something else" in currentTab do script "echo do more" in currentTab end tell **別解1 [#q98487ed] tell application "Terminal" do script "date" do script "time" in window 1 do script "who" in window 1 end tell **別解2 [#f8efc6de] tell application "Terminal" do script "date;time" end tell **遅延をつかってsshを実行する例 [#qaea9288] ※ただし、10.6.8ではpauseという変数名は予約語につかわれているらしい 参考 http://stackoverflow.com/questions/1870270/sending-commands-and-strings-to-terminal-app-with-applescript tell application "Terminal" activate my execCmd("ssh me@localhost", 1) my execCmd("myPassw0rd", 0) my execCmd("ls", 2) my execCmd("exit", 0) end tell on execCmd(cmd, pause) tell application "System Events" tell application process "Terminal" set frontmost to true keystroke cmd keystroke return end tell end tell delay pause end execCmd *AppleScriptのコード集 [#f9afa5dc] http://macscripter.net/viewforum.php?id=2 *ターミナル画面の文字列を変数に格納して判定 [#d3475686] set screenData to (the clipboard) as text if screenData contains "password" then end if *expectとspawnの例 [#l6b3a4ef] set shScript to "#!/bin/bash HOST=192.168.1.xxx USER=username PASS=passwrd PROMPT=# expect -c \" spawn ssh $USER@$HOST expect \\\"password:\\\" send \\\"$PASS\\r\\\" expect \\\"$PROMPT\\\" send \\\"cd /\\r\\\" expect \\\"$PROMPT\\\" send \\\"ls -al\\r\\\" expect \\\"$PROMPT\\\" send \\\"exit\\r\\\" \" " do shell script "sh -c " & quoted form of shScript