- スクリプトエディタで下のコードを保存
- ファイルフォーマット:アプリケーション
- オプション:「ハンドラの実行後に終了しない」にチェック
- 通常のアプリケーションとして実行
- 起動後は何も動作しないように見える
- 通常のアプリケーションと同様、
command + Q
などで終了
- メニューバーアプリケーションを使用すると
notify
ハンドラが実行される- アプリケーションが起動している間は何回でも繰り返し実行される
-
notify
ハンドラ内で動作内容を決める-
if
から始まる行の"com.google.musicmanager"
を、動作のきっかけとなる別のメニューバーアプリケーションのバンドルIDに変更 -
delay 3
の行からが動作内容のメイン部分
-
triggeredByUsingMenuBarApplication.app
use scripting additions
use framework "AppKit"
use framework "Foundation"
on run
--NSDistributedNotificationCenterに"com.apple.HIToolbox.beginMenuTrackingNotification"という通知があったとき、このAppleScript内の"notify:"ハンドラを実行するobserverを追加
current application's NSDistributedNotificationCenter's defaultCenter()'s addObserver:me selector:"notify:" |name|:"com.apple.HIToolbox.beginMenuTrackingNotification" object:(missing value)
end run
on quit
--アプリケーション終了時、runハンドラでNSDistributedNotificationCenterに追加したobserverを削除
current application's NSDistributedNotificationCenter's defaultCenter()'s removeObserver:me
continue quit
end quit
on notify:notification
--メニューバーアプリケーションのバンドルIDが"com.google.musicmanager"かどうかチェック
set notifiedApplication to current application's NSRunningApplication's runningApplicationWithProcessIdentifier:(notification's userInfo's ToolboxMessageEventData)
if (notifiedApplication's bundleIdentifier's isEqual:"com.google.musicmanager") as boolean then
--3秒待ってダイアログ表示
delay 3
activate
display dialog (notifiedApplication's localizedName as text) & " is being used" buttons {"OK"} default button 1
end if
end notify:
更新履歴
- 2016-10-07: FoundationフレームワークのNSDistributedNotificationCenterクラスを使って作成