趣旨

applescriptを独学するため

サイトに記載されている例を元に逆引きの技術をつくってみる

参考にしたサイト

http://tukaikta.blog135.fc2.com/blog-entry-67.html

ボタンを表示するダイアログ

display dialog "Choose VNC or SSH" buttons {"VNC", "SSH", "Cancel"} default button 3 with icon note

ダイアログの結果を得る

copy the result as list to {buttonp}

条件分岐

if the buttonp is "VNC" then

else if the buttonp is "SSH" then

end if

変数の代入

set aUserName? to "--ユーザー名"

文字列の連結

"vnc://" & aUserName? & ":" & aPassword

失敗するかもしれない文の実行

try

end try

すでに開いているアプリケーションを利用

tell application "Terminal" to activate

新規にアプリケーションを利用

tell application "Terminal"

開いているタブの数を数える

set wCount to count (every window whose visible is true)

新規ウィンドウでコマンド実行

do script aCMD

既存のウィンドウでコマンド実行

do script aCMD in front window

コマンドを実行したタブを変数に格納する

set currentTab to do script "echo do something"

同一のシェルウィンドウに複数のコマンドを送る方法

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

tell application "Terminal"
   do script "date"
   do script "time" in window 1
   do script "who" in window 1
end tell

別解2

tell application "Terminal"
   do script "date;time"
end tell

遅延をつかってsshを実行する例

※ただし、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?のコード集

http://macscripter.net/viewforum.php?id=2

ターミナル画面の文字列を変数に格納して判定

set screenData to (the clipboard) as text
if screenData contains "password" then
end if

expectとspawnの例

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
トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2012-07-17 (火) 02:08:04 (4301d)