mattintosh note

どこかのエンジニアモドキの備忘録

osascript でダイアログ表示「tell application "Finder"」と「tell application "AppleScript Runner"」

osascript でダイアログを表示するのに今まで tell application "Finder" 使ってたけど都度 activate しなきゃいけないし Finder の制御を持っていかれるのが面倒だった。AppleScript と同じ感じで使えればいいんだけどな〜と思って調べてみたら tell application "AppleScript Runner" っていうのがあった。

でもこれまた挙動が違って Finder の方では display alert ~ buttons {"OK", "キャンセル"} cancel button "キャンセル" が使えたのに AppleScript Runner では使えない。でもこのウィンドウの挙動は美味しいので AppleScript Runner は外せない。

で、とりあえず other button でボタン追加して返り値を if でなんとかすればできた。

osascript -e '
    tell application "AppleScript Runner"
        activate
        display alert "'"${prefix}/Resources"'\nに Wine フレームワークをインストールします" message "この処理には時間がかかることがあります。" other button "Cancel"
        if button returned of result is "Cancel"
            error number -128
        end if
    end tell

シェルスクリプト内で何度も使う場合は関数にして cat したものをパイプで繋いで osascript に流した方がいいかも。

DisplayAlert (){
cat <<__SCRIPT__ | osascript || exit
tell application "AppleScript Runner"
    activate
    display alert "$1" message "$2" other button "Cancel"
    if button returned of result is "Cancel"
        error number -128
    end if
end tell
__SCRIPT__
}
DisplayAlert "タイトル" "メッセージ"

前に Automator で using terms from application "Finder" 使ったこともあったけど osascript からは呼べないらしい。Automator も挙動違うし、この辺の挙動は統一して欲しいなぁ。