property theApps : {"System Preferences"} -- a list of applications to watch for
on run -- example
# add a couple of observers for workspace notifications
tell current application's NSWorkspace's sharedWorkspace's notificationCenter
addObserver_selector_name_object_(me, "appLaunched:", current application's NSWorkspaceDidLaunchApplicationNotification, missing value)
addObserver_selector_name_object_(me, "diskMounted:", current application's NSWorkspaceDidMountNotification, missing value)
end tell
end run
on appLaunched_(aNotification) -- an application was launched
# aNotification's userInfo record contains the name, path, bundle identifier, etc, of the application
# the record also includes an instance of NSRunningApplication that contains various information
set theApplication to (aNotification's userInfo's NSWorkspaceApplicationKey's localizedName()) as text
if theApplication is in theApps then
-- do whatever when a watched application launches
say "a watched application has been launched." -- for testing
end if
end appLaunched_
on diskMounted_(aNotification) -- a disk was mounted
# aNotification's userInfo record contains the name, path, URL, etc, of the volume
set theVolume to (NSWorkspaceVolumeLocalizedNameKey of aNotification's userInfo) as text
if theVolume is "Recovery HD" then return -- skip recovery partition
-- do whatever when a disk is mounted
say "the volume " & quoted form of theVolume & " has been mounted." -- for testing
end diskMounted_