Add info from NSWorkspace notifications as context variable

Currently it's possible to filter NSWorkspace notifications so that an action is only triggered when it the notification contains a specific word. However, I'd find it very useful to choose the action depending on whatever is contained in the notification, e.g. in a script triggered by that notification.
What I mean is: if I have a trigger on NSWorkspaceDidMountNotification it would be interesting to know which volume was triggered. So if, for example, a BTT variable was set to the path of that Volume I could take actions accordingly. Or if the NSWorkspaceDidLaunchNotification would set a BTT variable to the localized name or bundeURL of that application.
I know that this requires different variables for different notifications. But maybe the 2 examples above are feasible to implement as it could be the same variable (e.g. BTT_NSWORKSPACE_VOLUME) for many different notifications (NSWorkspace Did/Will Mount/Unmount Volume).

I think it will be difficult for Andreas to provide variables for all the different NSNotifications. What you can do is to subscribe the event itself.

Create an AppleScript file with the following content:

script theScript
	use framework "Foundation"
	use scripting additions
	
	property ca : current application
	property NSString : a reference to ca's NSString
	property workspaceClass : class "NSWorkspace"
	
	on addObserver()
		set ws to workspaceClass's sharedWorkspace()
		set nc to ws's notificationCenter()
		tell nc to addObserver:me selector:"deviceDidMount:" |name|:"NSWorkspaceDidMountNotification" object:(missing value)
	end addObserver
	
	on deviceDidMount:theNSNotification
		set userInfo to theNSNotification's userInfo()
		set theDevicePath to (userInfo's valueForKey:(NSString's stringWithString:"NSDevicePath")) as text
		tell application "BetterTouchTool"
			-- set BTT-Variable
			set_string_variable "lastMountedDevicePath" to theDevicePath
			-- trigger the named trigger
			trigger_named "OnDeviceDidMount"
		end tell
	end deviceDidMount:
end script

theScript's addObserver()

Add the predefined trigger "After BTT was started" and add the action "Open Application / File / Apple Script". Select the script file.

Create a named trigger "OnDeviceDidMount" and add an action "Run Apple Script (enter directly as text)" with the following content for the test:

use scripting additions

tell application "BetterTouchTool"
	-- get BTT-Variable
	set theDevicePath to get_string_variable "lastMountedDevicePath"
	-- make what ever you want…
	display notification (theDevicePath) with title "DeviceDidMount:"
end tell

Restart BTT.

Note!:
The observer runs during the application is running which executed the script file (in this case BTT).

If the script file is called multiple times, multiple observer instances will be created.

Have fun!

Tested on Big Sur

And here is the Observer-Script for the "NSWorkspaceDidLaunchApplicationNotification"

script theScript
	use framework "Foundation"
	use scripting additions
	
	property ca : current application
	property NSString : a reference to ca's NSString
	property workspaceClass : class "NSWorkspace"
	
	on addObserver()
		set ws to workspaceClass's sharedWorkspace()
		set nc to ws's notificationCenter()
		tell nc to addObserver:me selector:"launchApplicationNotification:" |name|:"NSWorkspaceDidLaunchApplicationNotification" object:(missing value)
	end addObserver
	
	on launchApplicationNotification:theNSNotification
		set userInfo to theNSNotification's userInfo()
		set theApp to userInfo's valueForKey:(ca's NSWorkspaceApplicationKey)
		set theAppName to (theApp's localizedName()) as text
		tell application "BetterTouchTool"
			-- set BTT-Variable
			set_string_variable "lastLaunchedApplication" to theAppName
			-- trigger the named trigger
			-- trigger_named "OnLaunchApplication"
		end tell
	end launchApplicationNotification:
end script

theScript's addObserver()

You can also use it for "NSWorkspaceDidActivateApplicationNotification", "NSWorkspaceDidDeactivateApplicationNotification", "NSWorkspaceDidTerminateApplicationNotification" etc.

That's great! That's exactly what I had in mind! Thank you very much!
I can conform that this also works on Catalina.

Imprint | Privacy Policy