AppleScriptスクリプティング応用編

ここでは、私が作ってきたスクリプトの中で使った命令を紹介します。
なお、細かい用語の解説は省きます。

【1】文字列のByte数を調べる
【2】文字列の置換
【3】データフォーク消去
【4】ドロップレットでのエイリアスの扱い
【5】他のアプリケーションの処理を待つ
【6】ダイアログ
【7】アプリケーションの有無
【8】リストダイアログ

【1】文字列のByte数を調べる
on ByteCheck(AllStr)
  set ByteNum to 0
  repeat with str in AllStr
    if (ASCII number str) < 128 then
      set ByteNum to ByteNum + 1
    else
      set ByteNum to ByteNum + 2
    end if
  end repeat
  return ByteNum
end ByteCheck
入力されたファイルタイプなどを調べるための関数です。
・引数 AllStr:調べたい文字列
・変数 ByteNum:バイト数
1文字ずつ「ASCII number」を調べて、128以下なら1バイト、128以上なら2バイトとしてByteNumに加算します。
ByteNum = 4 and length of AllStr = 4
がtrueなら、4文字4バイトであることが判ります。

【2】文字列の置換
on changeDelim(cangeText, setDelim, cangeDelim)
  set LastDelim to AppleScript's text item delimiters
  set AppleScript's text item delimiters to setDelim
  set cangeText to text items of cangeText
  set AppleScript's text item delimiters to cangeDelim
  set cangeText to cangeText as string
  set AppleScript's text item delimiters to LastDelim
  return cangeText
end changeDelim

文字列中の文字の置換を行います。
・引数 cangeText:置換する文章。
・引数 setDelim:検索文字。
・引数 cangeDelim:置換文字。
・変数 LastDelim:置換開始前のAppleScript's text item delimiters
「AppleScript's text item delimiters」という文字列中の区切りデータを変更する事で置換を行います。
動作としては、cangeText中に含まれるsetDelim全てをcangeDelimに置換した文章が得られます(LastDelimで変更前に戻しておきます)。
ファイル名を「.」で検索して、置換を行わずに分解された文字列を返すようにすれば、ファイル名中の拡張子のみが得られるようにする事もできます。
また、テキストファイル中の文字列の置換等にも使えます。

【3】データフォーク消去
tell application "Finder"
  set obj to item 1 of files of selection
  try
    set OpenFile to open for access file obj with write permission
    set eof OpenFile to 0
  end try
  close access file obj
end tell

選択ファイルのデータフォークを消去します。
・変数 obj:対象とするファイル
・変数 OpenFile:開いた状態のファイル
「open for access 〜 with write permission」によって、書き込み可能な状態でファイルを開いて、データフォークの終了位置を表す「eof」を0にすることで、データを消去しています。
 ※本当にファイルデータが消失するので注意して下さい。

【4】ドロップレットでのエイリアスの扱い
on open
  tell application "Finder" to set obj to selection
end open

普通ドロップレットでは、ドロップしたファイルを「on open 〜」の引数で取得しますが、それだとエイリアスファイルをドロップした場合に、その処理の対象がエイリアスの本体(オリジナル)に対して実行されてしまいます。
そのため、あえて「selection」でファイルを取得しています。
この方法だと、処理はエイリアスそのものに対して行われます。
ドロップする時にはファイルが選択された状態になっている、ということを利用しています。

【5】他のアプリケーションの処理を待つ
repeat until (application file of (item -1 of (processes whose frontmost = true))) /= anyApplication
  delay 1
end repeat

スクリプトから他のアプリケーションでなんらかの処理を行って、さらに元のスクリプトで処理を続ける場合に、スクリプトの処理を待たせる必要があります。
・変数 anyApplication:アプリケーションの参照
指定したアプリケーションが前面にある間は「delay」をリピートし続けるというものです。
これによって、スクリプトのこれ以降の処理を待たせることができます。

【6】ダイアログ
「disiplay dialog」には様々な引数がありますが、それらは変数に入れても使用できます。
repeat
  set DlgBtn to {"OK", "OK", "OK"}
  set num to some number of {1, 2, 3}
  set item num of DlgBtn to "キャンセル"
  set DlgAns to item num of {return & "stop", return & "note" & return & return, "caution"}
  set IconNum to num - 1
  set DlgTxt to item num of {"ストップ", "ノート", "コーション"}
  display dialog DlgTxt buttons DlgBtn default answer DlgAns default button num with icon IconNum giving up after (num * 2)
end repeat

disiplay dialogで使用可能な引数を全て変数にして、さらにランダムに値を選ぶようにしてみました。
上記のスクリプトには実用性はありませんが、一つの「disiplay dialog」命令でも、条件によって表示内容を変化させるといった使い方が可能なのは判ると思います。

【7】アプリケーションの有無
tell application "Finder" to set obj to application file id "ttxt"
これだけで変数objにSimpleTextの参照が得られます。
"ttxt"を存在を確認したいアプリケーションのクリエータコードに変更して下さい。
なお、アプリケーションが存在しない場合はエラーになるので、try構文内で使用して下さい。
この処理によって、ユーザーのアプリケーションの所持状況に応じたスクリプトを書く事ができます。
また、アプリケーション名が変更されていても、そのアプリケーション名を得ることができます。

【8】リストダイアログ
choose from listを使用することで、3つ以上の選択肢を提示することが可能ですが、選択内容はリストに表示されたテキストそのものなので、選択内容を元に処理を行うのは面倒です。
そこで、表示項目の先頭にナンバリングを行い、選択項目から数字だけを取得し、その数字から処理対象を選択することで、リストの内容自体を可変的なものにすることが可能です。
ちょっと長いですが、choose from listを使ったファイルランチャーをまるごと掲載しておきます。
property OpenFile : {} --開くファイルのパス
property NameList : {} --開くファイルの名前

on run
  OpenDialog()
end run

on OpenDialog() --リスト表示
  set DlgName to {}
  repeat with ListNum from 1 to (count NameList) --項目作成
    set end of DlgName to {(ListNum & ":" & item ListNum of NameList) as string} --ナンバリング
  end repeat
  set DlogFile to {"追加"} & DlgName --リストの先頭に追加を追加
  if OpenFile /= {} then set end of DlogFile to {"削除"} --リストに項目がある場合は削除を追加
  set List_Res to choose from list (DlogFile as list) with prompt ツ
    "起動するファイルを選択して下さい。" with multiple selections allowed --リスト表示
  if List_Res /= false then
    repeat with select_res in List_Res --選択項目判定
      set select_res to select_res as string
      if select_res = "追加" then
        FileSet()
        OpenDialog() --項目追加後、選択に戻る
      else if select_res = "削除" then
        FileDelete(DlgName)
      else
        set SelectFile to item (character 1 of select_res) of OpenFile
        --↑選択項目の順番と同じ順番にあるOpenFile中のファイル
        FileOpen(SelectFile) --選択ファイルを開く
      end if
    end repeat
  end if
end OpenDialog

on FileSet() --ファイルの追加
  set end of OpenFile to (choose file with prompt "リストに追加するファイルを選んで下さい。")
  tell application "Finder" to set end of NameList to {(name of last item of OpenFile) as string}
end FileSet

on FileOpen(SelectFile) --ファイルを開く
  ignoring application responses --ファイルを開けたかどうかを無視
    tell application "Finder" to open file SelectFile
  end ignoring
end FileOpen

on FileDelete(DeleteList) --リスト項目削除
  set Delete_Res to (choose from list DeleteList with prompt ツ
    "リストから削除する項目を選択して下さい。" OK button name "削除")
  set Delete_Num to (character 1 of (Delete_Res as string) as integer) --項目番号取得
  set ItemNum to (count OpenFile)
  if (count NameList) /= 1 then --項目が1つではない時
    if Delete_Num = 1 then --1番目の項目を選択した時
      set NameList to rest of NameList
      set OpenFile to rest of OpenFile
    else if Delete_Num = (count NameList) then --最後の項目の時
      set NameList to (items 1 thru (Delete_Num - 1) of NameList)
      set OpenFile to (items 1 thru (Delete_Num - 1) of OpenFile)
    else --中間の項目の時
      set NameList to (items 1 thru (Delete_Num - 1) of NameList) ツ
        & (items (Delete_Num + 1) thru ItemNum of NameList)
      set OpenFile to (items 1 thru (Delete_Num - 1) of OpenFile) ツ
        & (items (Delete_Num + 1) thru ItemNum of OpenFile)
    end if
  else --項目が1つの時
    set NameList to {}
    set OpenFile to {}
  end if
end FileDelete

FileSetハンドラのchoose fileで選択したファイルの参照をプロパティOpenFileに追加し、同時にリスト表示用にファイル名だけをプロパティNameListに収めます。
OpenDialogハンドラのchoose from listで項目を表示する際にファイルの参照をそのまま表示すると長いので、プロパティNameListのファイル名を使います。
さらに、実際に表示させる場合には「repeat」でナンバリングを行います。
こうして番号の付いたファイル名をリスト上に表示し、選択された項目から「character 1」によって番号だけを取り出し、その番号に対応したプロパティOpenFileの「item」を開くことでランチャーとして機能します。
最後のFileDeleteハンドラでは登録したファイルをリストから削除できるようにしてあります(動作の高速化のため、項目数によって異なる処理を行っています)。

御質問等がありましたらメール、掲示板にて


ゲーム 健康 テクノロジー 教育 ユーザビリティ ダウンロード 誰かの独り言 ⇒トップページへ