AppleScriptであなたのMacをパワーアップ


AppleScriptは簡単なプログラムで様々なアプリケーションのコントロールが行え、 決まった手順の作業の繰り返しの時など非常に役立ちます。
AppleScript対応のアプリケーションはたくさんあるのですが、ほとんどの場合にマニュアル中ではAppleScriptからの使い方が解説されていないことや、AppleScriptの一般的な参考書が少ないのが欠点です。
AppleScriptをうまく使うと、コマンドの出力を別のコマンドの入力に用いるUnixのパイプ処理と同様のことができるようになり、なにかと便利です。

以下にちょっと便利なAppleScriptのプログラムを紹介しましょう。

(使用環境:PM7600 & Mac OS.8 and AppleScript 1.1.2)


MENU
(1) Drag&DropでEudoraからメールを出す

以下は、Eudora Proを用いて、 Drag&Dropされたテキストファイルの内容を メールの本文として指定の宛先に自動送信するAppleScriptです。
(AppleScript 1.1.2とEudora Pro 2.1.3-Jの組み合わせで動作確認)


on open DropFiles
 repeat with aFile in DropFiles
  set FileHandler to open for access aFile
  set TextContents to read FileHandler
  close access FileHandler
  tell application "Eudora Pro 2.1.3-J"
   set NewMessage to make message at the end of mailbox "送信"
   set field "To" of NewMessage to "yasuda@eri.u-tokyo.ac.jp"
   set field "Subject" of NewMessage to "put_subject_here"
   set body of NewMessage to TextContents
   save NewMessage
   queue NewMessage
   connect with sending without checking
   quit application "Eudora Pro 2.1.3-J"
  end tell
 end repeat
end open

7行目のyasuda@eri.u-tokyo.ac.jpの部分には適当な宛先e-mail addressを入れ、
8行目のput_subject_hereにはメールのSubject: 欄にかく内容を 入れておきます。
特定のアドレスにしょっちゅうメールを送信するような場合に便利です。
(最初からEudora中で文章を書けばこんなScriptはいりませんが、別のエディタを使うことだってたまにはあるでしょう...)

on open DropFiles・・end open
の書式をもつAppleScriptは「実行専用で保存」という形式で保存してください。そうしないとDrag&Drop機能が働きません。

To MENU
(2) Drag&DropでKaleidaGraphに作図させる

以下は、Drag&Dropされたテキストファイルの内容を KaleidaGraphにデータとして 読み込んで、定まった形式のグラフを書かせるスクリプトです。
あらかじめ、 KaleidaGraph側でPlotScriptを作成してハードディスクに保存しておき、 それを読み出して使用します。
(AppleScript 1.1.2とKaleidaGraph3.08dの組み合わせで動作確認)


on open dropFiles
 repeat with aFile in dropFiles
  set FileHandler to open for access aFile
  set TextContents to read FileHandler
  close access FileHandler
  tell application "KaleidaGraph"
  SendData TextContents
  LoadPlotScript "HDdisk:KGScript"
  RunPlotScript
 end tell
 end repeat
end open

テキストファイルのデータの書き方は、普段自分が使用しているKaleidaGraphの設定によります。ファイル:データ読み込みの部分で、テキストファイルの読み込み形式を指定していますが、その設定にしたがいます。(これを変更したい場合には、7行目のSendDataの部分に適当なoptionをつけます。詳しくはScript編集プログラムからKaleidaGraphを開いて用語説明を読んでください。)
8行目のHDdisk:KGScriptの部分には、予め作成してあるプロットスクリプトのファイルパスを記入してください。

To MENU
(3) Drag&DropでFetch転送

以下はFetchを用いて、 Drag&Dropされたfileをremote hostにの決まった ディレクトリに転送するAppleScriptです。
Macで作成した.htmlファイルや.gifファイルをWWW serverにuploadする時などに便利です。
(AppleScript 1.1.2とFetch3.0.1の組み合わせで動作確認)


on open DropFiles
 tell application "Fetch 3.0.1"
  make new transfer window at beginning with properties {hostname:"remote.host", userid:"yourID", password:"yourPassword"}
  open remote directory "targetDirectory_in_remotehost"
  repeat with AFile in DropFiles
   put into transfer window "your.remote.host" item AFile text format Text binary format Raw Data
  end repeat
  quit Application "Fetch 3.0.1"
 end tell
end open

3行目のremote.host部分には接続するremoteマシンのIPあるいはdomainnameが、 yourIDとyourPasswordの部分にはそのマシンにlogin時に使用するIDと passwordとをそれぞれ記入します。 4行目はlogin directory以外にremote directoryからデータを送る時に必要です。 Fetchの側ではPreferences:MiscのShow sign-on dialog at startupのチェックをはずしておく必要があります。
To MENU
(4) FileMakerからの書き出しレコードをメールする

これは(1)の応用です。 FileMakerから特定のファイルにデータを出力し、その出力ファイルをメールで決まったメールアドレスへ送ろうというものです。ファイルメーカーのスクリプトとそのスクリプト中で用いるAppleScriptを紹介します。
tell application "Finder"
 set FileHandler to open for access file ((path to desktop folder as text) & "My_mail")
 set TextContents to read FileHandler
 close access FileHandler
end tell
tell application "Eudora Pro 2.1.3-J"
 set NewMessage to make message at the end of mailbox "送信"
 set field "To" of NewMessage to "e-mail_address@domain"
 set field "Subject" of NewMessage to "put_subject_here"
 set body of NewMessage to TextContents
 save NewMessage
 queue NewMessage
 connect with sending without checking
 quit application "Eudora Pro 2.1.3-J"
end tell
tell application "Finder"
 delete the file ((path to desktop folder as text) & "My_mail")
end tell

中心部分は(1)と同じですが、デスクトップ上に存在する決まったファイルの内容を読み込んで、メール送信後にはそのファイルを消去してしまうというところが違っています。 8行目のe-mail_address@domainの部分には適当な宛先e-mail addressを入れて、 9行目のput_subject_hereにはメールのSubject: 欄にかく内容を入れておきます。

このAppleScriptをファイルメーカーのスクリプト中で、

・レコード書き出し[記憶する,「My_mail」]
・AppleScriptを実行[ .......]

のように、連続実行させるように用いれば、ボタン一つでファイルメーカーの特定のレコードをメール送信することができるようになります。

To MENU
(5) Fetchを介在させたFileMakerとリモートサーバーの連携

これは(3)の応用です。Remote hostにFetchでloginして必要なファイルをいったんMac上にファイルとして保存して、そのファイルをFilemakerの取り込みファイルとして使用しようというものです。 Remote host 上のファイルは、 Filemakerへの取り込みが完了した時点で、Fetchを用いて消去しています。
Script1
tell application "Fetch 3.0.1"
 make new transfer window at beginning with properties {hostname:"remote.host", userid:"yourID", password:"yourPassword"}
 open remote directory "targetDirectory_in_remotehost"
 duplicate remote file "Name_of_TransferFile" to beginning of alias "Path_of_Folder_to_save"
 quit Application "Fetch 3.0.1"
end tell

Script2
tell application "Fetch 3.0.1"
 make new transfer window at beginning with properties {hostname:"remote.host", userid:"yourID", password:"yourPassword"}
 open remote directory "targetDirectory_in_remotehost"
 delete remote file "Name_of_TransferFile"
 quit Application "Fetch 3.0.1"
end tell

(3)と同様の部分の説明は省略します。
Script1の転送ファイルをMac上に保存する部分では
"Path_of_Folder_to_save"
に、"My_Harddisk:Folder1:"のようにディスク名とフォルダ名を”: ”で区切って記入します。 最後が”: ”で終了することに注意してください。

Script2の
 delete remote file "Name_of_TransferFile"
は消去するファイル名を記入します。

これらの2つのAppleScriptをファイルメーカーのスクリプト中で、

・AppleScriptを実行[...Script1...]

・レコード取り込み[記憶する,「Name_of_TransferFile」]
・AppleScriptを実行[ ... Script2..]

のように、連続実行させるように用います。

To MENU
Nov-1997, Web-MAGMA, Earthq.Res. Inst., Univ Tokyo.