Sep 08, 2008
AppleScript から rsync を実行
rsync にはいつもお世話になっている。 コマンドラインで叩いてもさほど面倒というわけでもないのだけれど、ブログの更新の様に何度も繰り返して行う作業は自動化しておきたい。 今まではシェルスクリプトを利用していたのだけれど、何となく AppleScript 化してみた。
rsync.expect
rsync に AppleScript からパスワードを渡すために expect を使用する。 少々面倒だけれど、とりあえず expect で 汎用的な rsync 制御用の関数を用意しておく。 何かで使い回せるかもしれないし。 ・・・といって使い回した試しはないが。
#!/usr/bin/expect
proc Rsync {RsyncServer RsyncPort RemoteUser RemotePassword RemoteDirectory LocalDirectory} {
set timeout 10;
spawn rsync -av --port=$RsyncPort $LocalDirectory $RemoteUser@$RsyncServer:$RemoteDirectory
expect {
password: {send "$RemotePassword¥r";}
timeout {puts stderr "rsync timeout."; return 1;}
}
expect {
sent {return 0;}
Permission {puts stderr "login failed."; return 2;}
}
return 0
}
set ARG0 [lindex $argv 0]
set ARG1 [lindex $argv 1]
set ARG2 [lindex $argv 2]
set ARG3 [lindex $argv 3]
set ARG4 [lindex $argv 4]
set ARG5 [lindex $argv 5]
set Result [Rsync $ARG0 $ARG1 $ARG2 $ARG3 $ARG4 $ARG5]
exit $Result
bloguploader.scpt
次に、rsync.expect を呼び出す AppleScript を作成する。 Password はスクリプト内には埋め込みたくないので、ダイアログで入力するようにしておいた。 このスクリプトはこのまま Automator で使用しても OK。
display dialog "Enter password: " default answer "" set RsyncPassword to text returned of result do shell script "/usr/bin/expect -f /Users/me/Documents/rsync.expect www.example.com 873 me " & RsyncPassword & " /home/me/blog /Users/me/Documents/Blog/"
TrackBack ping me at
http://www.in-vitro.jp/blog/index.cgi/Apple/20080908_01.trackback
writeback message: Ready to post a comment.
