実践編」はこちら

(最終更新2007.12.26)

はAppleScriptで使用可能 / はXCodeで使用可能
イベントハンドラ
実行型 run runハンドラ(省略可)
起動した時に実行される部分
※そのままではXcodeで機能しない
on run

 …
end run

opened NIBファイル設定で「window/opened」アクションに
チェックを入れておかねばならない
コレを使用する事によってウインドウが開いた直後に実行される
on opened theObject

 …
end run

常駐型 idle 起動したときにすぐには実行しないが return文で指定した時間(秒単位)を過ぎる毎に実行される部分
※そのままではXcodeで機能しない
on idle
 …
 
return 30
end idle
ドロップレット open アプリケーションとして保存したアイコンに書類をドラック&ドロップした際に実行される
対象の情報は
drop_itemsにその情報が入る
on open drop_items
 …
end open
クリック clicked ユーザーが作成したボタンなどをクリックした際に実行される
クリックされた対象は
theObjectにその情報が入る
on clicked theObject
 …
end clicked
サブルーチン型 ユーザー指定
(非予約語)
あらかじめ命令語に予約されていないユーザー独自の単語を使って 繰り返し使うスクリプトを用意しておく事ができる
(x,y,zは変数 特別に受け渡す変数が無ければ省略可)
xで渡された変数はyに代入される zは元のルーチンに戻ったときに返したい値で zの値はbに代入される。特別な受け取り命令が無ければ
resultに記憶される
set b to test( a)

--ココからハンドラ
on test(y)
 …
return z
end test
表示
ダイアログ display dialog 画面にメッセージを表示させる事ができる
オプションを使いコメント入力や3個までのボタンを設定できる
display dialog
display dialog … & return & …

display dialog … [オプション]
buttons {"Aボタン", "Bボタン"}
default button "Aボタン"
default answer 入力初期値
with icon (番号[0〜2])
オプションは文字列をつなぐのではないので & は必要ない

入力された情報は
set ans to
result
set ans to
button returned of result
で抜き出せる。

上記の使い方をそのままにウインドウから飛び出す形にもできる attached to window "main"
アラート display alert
使えなくなったバージョンが有ります
アイコンや太字のメッセージと説明文を表示できる display alert "…"
display alertmessage
上記の使い方をそのままにウインドウから飛び出す形にもできる display alertmessageattached to window "main"

そして、アラートウインドウを閉じた後の処理は
on alert ended theObject with reply withreply

end
alert ended
のハンドラ内に書けば良い
ログ log スクリプト内部でのログなので アプリケーション化した場合はログの表示はされない。デバック用 log "ans" & obj
計算
配列変数 {○, ○, …} 一つの変数の中にいくつもの値を記憶する事ができる
初期状態なら左からitem 1,2,3,…と番号が付いている
set ans to {1, 2, 3}
set obj to item 1 of ans
{要素1:○, …} 配列のアイテムに名前を付けられる set ans to {"要素1":1, "要素2":2}
set obj to item "要素2" of ans
配列の計算 配列数を増やす場合は要素を選ばずに加える
(あらかじめ配列定義をしておかなければいけない)
配列内を計算する場合は、item 1 of … を使う
(存在しない要素は選べない)
配列定義:
set ans to {}

set ans to ans & |定数又は変数|

set item 1 of ans to |定数又は変数|

set |変数| to item "要素1" of ans

演算 +, ー, ×, ÷ 加減乗除の計算が出来ます
たし算(+)・ひき算(-)・かけ算(*)・わり算(/)
set ans to x + 1
set ans to x - 2
set ans to x * 3
set ans to x / 4
^ 乗数の計算が出来ます set ans to x ^ 2

元の数 ^ 乗数
配列連結 & 要素が増えます(文字列の場合は文字列どうしをつなげます) set obj to {"あいう"}
set obj to obj & "かきく"

{"あいう","かきく"}となる

文字連結 & 文字列をつなげます
(配列の場合はitemが増えます)
set obj to "あいう" & "ABC"

"あいうABC"となる

比較 contains 左に文字列から右の文字列があるか探す。
あればtrue、なければfalseを返す
set a to "AppleScript" contains "esc"

大文字・小文字が無視されるので
trueとなる。

数える number 文字数を得る set NamCount to number of NamFF as text
count item数を得る set myLst to {"東京", "千葉", "埼玉", "神奈川"}
set Cntml to
count of myLst

上のスクリプトならitem数が4個なので“4”となる

厳密に言うとnumbercountは違う命令なのですが どちらの状況でどちらを使っても同じ働きをするようです。
文字を
抜き出す
character 一文字抜き出す set a to "あいうえお"
set
b to character 3 of a

文字列aの中の3文字目を変数bに
代入しています。

一文字抜き出す(後ろから) set b to character -2 of a

数字に"-"(マイナス)をつけると後ろから何文字目と数えます

text from 数文字を抜き出す set a to "あいうえお"
set
b to text from character 2 to character 4 of a

文字列aの中の2文字目から4文字目
を変数bに代入しています。

数文字を抜き出す(後ろから) set b to text from character 2 to character -2 of a

数字に"-"(マイナス)をつけると後ろから何文字目と数えます


文字列を
抜き出す
word 単語単位に抜き出します 上記のcharacterの部分を変更すれば使える
paragraph 行単位に抜き出します

変数設定
グローバル定義 global 通常の変数はハンドラがかわると認識できなくなるが
スクリプトの最初にglobal定義をするとハンドラを超えて使える様になる
ただし、スクリプト起動毎に初期化される
global |変数|
ここではまだ中身が空の(数値の0でも0文字でもない)状態なので、 別に初期値を入れなければならない
プロパリティ定義 property グローバル定義と同じでハンドラを超えて変数が使える様になる
さらに、初期の値を割り当てることが出来る。スクリプトを再編集しない限り、前回に起動した値を覚えている(AppleScriptのみ)
Xcodeではグローバル定義と同じ扱いになり初期値を設定しても無視される
property |変数| : (初期値)
ファイル選択
読込み choose 操作をするユーザーにファイルを選択させる事が出来ます
フォルダを選んだり、読み込むファイルを選択する時のウインドウを開きます
choose file … 選択ファイルを取得します
choose folder … 選択フォルダを取得します
choose application … 選択アプリケーションを取得します
choose URL… 選択URLを取得します
※取得するのは全てエイリアスとしてです。処理は別にしなければなりません。
set loadObj to result
で得られます。
option:
 … with prompt "ファイルの選択:"

  コメント表示を変更できます
 … with prompt "name" default location (path to home folder)
  最初に参照する先を(例ではHOMEに)設定できます
書出し choose file name 書出しをする為のファイルネームを取得します。
※コレ単体では書出しをしません。
choose file name

書き出し先を得るには 前記と同じresultで入手
名前だけならば
name of result
で入手
オプションは「読込み」のが同等に使える
現在の
情報を
得る
path to me folder of ( 〜 ) as text

"OS X:Applications:AppleScript:"
tell application "Finder"
set Pathme to folder of (path to me) as text
end tell
name of ( 〜 ) as text

"Script Editor.app"
tell application "Finder"
set PathName to name of (path to me) as text
end tell
( 〜 ) as text

"OS X:Applications:AppleScript:Script Editor.app:"
tell application "Finder"
set Pathonly to (path to me) as text
end tell

※|変数|はアルファベットの頭文字を使った英数が使える。(1文字目を数字にする事ができない。)[例] set AnsObj01 to 1
※「| |」という記号で囲む事によって変数名にひらがなや漢字も使用できる。[例]
set |漢字| to "変数テスト"

情報(一部OS Xでしか機能しないものがあります)
ファイル
情報
neme ファイル名を取得します set dammy to name of |書類|
displayed name 表示されているファイルネームを取得します(隠れている拡張子は取得しない)(読出しのみ) set dammy to displayed name of 書類|
name extension 拡張子のみを取得します(読出しのみ) set dammy to name extension of |書類|
file type ファイルタイプを取得します set dammy to file type of |書類|
creator type クリエータを取得します set dammy to creator type of |書類|
kind ファイルの種類を示す情報を取得します set dammy to kind of |書類|
comment コメントを取得します set dammy to comment of |書類|
size サイズを取得します(読出しのみ) set dammy to size of |書類|
creation date 作成日を取得します(読出しのみ) set dammy to creation date of |書類|
modification date 変更日を取得します set dammy to modification date of |書類|
日時を得る   日時を得る "2007年 3月 30日 金曜日 5:12:31 PM"
set ToDate to current date
年数を得る "2007" set YearNN to year of toDate
月名を得る "March" set MonthNN to month of toDate
月日を得る "2007年 3月 30日 金曜日" set YearMt to date string of toDate
set toDayofTime to time string of ToDate





NSTextField内にテキストを書込む set contents of text field "TextFieldName" of window "MainWindow" to |変数| tell application "Finder"を実行中はエラーが出る
NSTextField内のテキストを読込む set |変数| to contents of text field "TextFieldName" of window "MainWindow" 同上

  (Xcodeでも使えるはず)
情報の取得(例ではコメント) tell application "Finder"
set dammy to comment of file |アドレス|
/folder/disk
end tell
"Finder"を使う事を忘れずに
タイムアウト処理 with timeout of (30 * 60) seconds
  〜
end timeout
時間のかかる処理をしている場合 Applescriptでは、一つのコマンド処理で1分30秒 を超えるとタイムアウト処理としてエラーを発生させます。その処理を先延ばしする為にこのスクリプトを使います。例では30分に設定しています。
アプリケーション終了 tell application "Finder" to quit

on quit
continue quit
end quit

quit of me

quit me
保存する必要のあるときに
ダイアログがでる
quit application "textedit.app" saving yes 保存して終了
システム(Finder)のバージョンを調べる tell application "Finder"
set x to version as text
end tell
メジャーバージョン(OS X 10.3.9なら10.3の部分)はそのままなのですが、マイナーバージョン(末尾の.9)は違う数字が出る事が有ります。
原因は不明ですが、システムと Finderアプリケーションの違いなのでしょうか?
テキスト読み書き tell application "Finder"

(* 読み出し部分 *)
choose file with prompt "ファイルの選択:"
set add1 to result
open for access add1
try
read add1
set answ to result
on error
close access add1
return
end try

close access add1

(* 内容確認 *)
set answ to answ as text
set a to number of answ
display dialog "文字数は" & (a as text) & "でした"

(* 書き出し部分 *)
try
choose file name with prompt "保存選択" set Filename1 to result
open for access Filename1 with write permission
try
write answ to Filename1
on error
close access Filename1
return
end try

close access Filename1
end try


例は、『テキストを読み込み』『文字数を数え』『そのまま別なファイルを作って書出す』だけのものです
(これだけは注意)読込み・書込みに関わらず開いた(open)ドキュメントは、必ず(close)しないといけません。最悪、開けなくなります
何らかの失敗(error)があった場合に対応するために 必ずtry文を使い閉じるように(close)してください






テキストだけではなくいろいろな応用が利くと思います

以下続