Applicationのactivate(最前面への移動)のイベントを検知して、記録するAppleScriptです。30秒間待って、その間に最前面に移動されたアプリケーションのローカライズ名称をリストに蓄積して返します。Script終了時には、activateされたアプリの名称をリストで返します。
macOS 15.5上で動作を確認しています。スクリプトエディタ、Script Debugger、Xcode(Cocoa Application)での実行を確認していますが、observerの追加宣言の箇所、
notifCenter’s addObserver:me selector:”appWasActivated:” |name|:(current application’s NSWorkspaceDidActivateApplicationNotification) object:(missing value)
で、objectなど記述にエスケープを記述しないと動かないこともありました(Xcode)。「|object|:」と記述する必要が。
イベント記録用の実験用Scriptなので、ほぼ実用性はありません。実際に使い物にするのであれば、リアルタイムでGUIに反映させるとか、アプリごとのactivate期間を集計するとかいったレベルに昇華させる必要があることでしょう。
AppleScript名:activateを記録する v2.scptd |
— Created 2015-05-30 18:11:12 +0900 by Piyomaru — Modified 2025-06-08 12:56:30 +0900 by Piyomaru — 2015–2025 Piyomaru Software use AppleScript version "2.8" use scripting additions use framework "Foundation" use framework "AppKit" property appList : {} property prevApp : missing value –アプリケーションのActivateをNotification Centerに登録 set theWorkspace to current application’s NSWorkspace’s sharedWorkspace() set notifCenter to theWorkspace’s notificationCenter() notifCenter’s addObserver:me selector:"appWasActivated:" |name|:(current application’s NSWorkspaceDidActivateApplicationNotification) object:(missing value) delay 30 my stopObserving:(me) return appList –> {"Safari", "Finder", "Keynote", "QuickTime Player", "Keynote", "Finder", "スクリプトエディタ"} on stopObserving:sender set theWorkspace to current application’s NSWorkspace’s sharedWorkspace() set notifCenter to theWorkspace’s notificationCenter() notifCenter’s removeObserver:me |name|:(current application’s NSWorkspaceDidActivateApplicationNotification) object:(missing value) end stopObserving: on appWasActivated:aNotification — query the notification for the active app set theApp to aNotification’s userInfo()’s valueForKey:(current application’s NSWorkspaceApplicationKey) set theAppName to (theApp’s localizedName()) as string if prevApp is not equal to theAppName then set prevApp to theAppName set the end of appList to theAppName end if using terms from scripting additions display notification theAppName & " is activated" end using terms from end appWasActivated: |
More from my site
(Visited 1 times, 1 visits today)