[ AppleScript ] 汎用・その他のサンプル集

汎用的に使える命令・その他の AppleScript サンプル集です。

AppleScript のサブルーチン ———————————————————————-

AppleScript でサブルーチンを書く時の書式。

log (Test(5, 5))
on Test(x, y)
  return x + y
end Test

スクリプトエディタのイベントログウインドウにログを出力する ———————————————————————-

イベントログウインドウにログを出力します。 デバッグの時に便利です。

set TestString to "abc"
log "Test String = " & TestString

イベントログを表示するには、メニューバーから [ 表示 ] → [ イベントログを表示 ] を選ぶか、[ ウインドウ ] → [ イベントログの履歴 ] を選択します。

start log と stop log と言うのもあった気がするんですが使い方を忘れてしまいましたw

システムの警告音を鳴らす ———————————————————————-

次のように書くと、システム環境設定で設定した警告音が鳴ります。

beep

システム警告音を二回鳴らしたい時は、次のようになります。

beep 2

指定時間処理を待つ ———————————————————————-

次の例では、5秒毎に警告音を鳴らします。 動作させるには、アプリケーションとして保存して起動します。

on idle
  beep
  return (5)
end idle

指定時間処理を遅らせる ———————————————————————-

delay はどのアプリケーションでも使えるコマンドで単位は秒。 delay 10 なら 10 秒間停止するみたい。

次のコードは実行後 10 秒待って警告音を鳴らします

tell application "Finder"
  delay 10
  beep
end tell

ドロップしたファイルに対して処理をする ———————————————————————-

ドロップしたファイルに対して処理をする AppleScript は次のように書く事ができます。

on open DROPFILES
    count item of DROPFILES
    repeat with i from 1 to result
        -- ドロップファイル数分繰り返される処理
        -- get info for item i of DROPFILES
    end repeat
end open

こういった形式のアプリをドロップレット ( Droplet ) と言うんだそうな。ドロップレットにするには、スクリプトをアプリケーションとして保存します。

文字列の最後に出現するセミコロン以降を削除するサブルーチン ———————————————————————-

文字列の最後に出現するセミコロン以降を削除する AppleScript のサンプルです。パス操作用?

Macintosh HD:Users:USERNAME:Desktop:test.scpt

こんなパス(文字列)を

Macintosh HD:Users:USERNAME:Desktop:

こうしたくて書いてみました。

set x to "Macintosh HD:Users:USERNAME:Desktop:test.scpt"
log (MyFilePath(x))

on MyFilePath(this_text)
  set y to 0
  repeat with thisChar in this_text
    set y to y + 1
    set x to the offset of thisChar in ":"
    if x is not 0 then
      set z to y
    end if
  end repeat
  return (text from character 1 to z of this_text)
end MyFilePath

動作確認:Mac OS 10.4.7

タブ切りテキストを読み込んで配列に格納するサンプル ———————————————————————-

タブ切りテキストを読み込んで配列に格納する AppleScript のサンプルです。

set Sourcetxt to (choose file with prompt "ソーステキストファイルを指定して下さい" as string) as string

-- ファイル読み込み
set aFilePointer to open for access alias Sourcetxt
set aData to read aFilePointer as text
close access aFilePointer

-- 配列に格納
set AppleScript's text item delimiters to return
set aList to text items of aData
set AppleScript's text item delimiters to tab
repeat with i from 1 to count of aList
  set item i of aList to (text items of item i of aList)
end repeat

-- 結果出力
repeat with i from 1 to count of aList
  log (text item 2 of item i of aList)
  log (text item 1 of item i of aList)
end repeat

動作確認:Mac OS 10.4.5

ダイアログを表示するサンプル ———————————————————————-

ダイアログを表示する AppleScript のサンプルです。

set msg to "変数内の文字列"
display dialog msg & "です。" buttons {"Cancel", "OK"} default button 2

動作確認:Mac OS 10.4.5

アプリケーションを切り替える・起動するサンプル ———————————————————————-

アプリケーションを切り替える(アクティブにする)AppleScript のサンプルです。指定したアプリケーションが起動していない場合は、起動します。

tell application "TextEdit" to activate

動作確認:Mac OS 10.4.5

指定したアプリを起動するサンプル ———————————————————————-

指定したアプリを起動します。ただ、それだけ(笑)

tell application "Jedit X"
  launch
end tell

フォルダを選ぶサンプル ———————————————————————-

フォルダを選ぶ AppleScript のサンプルです。

set ThisFolder to (choose folder with prompt "フォルダを選んでください")
log ThisFolder

動作確認:Mac OS 10.4.5

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です