# Add info from NSWorkspace notifications as context variable

**URL:** <https://community.folivora.ai/t/add-info-from-nsworkspace-notifications-as-context-variable/21308>\
**Category:** Feature Requests\
**Created:** [February 5, 2021, 1:59pm UTC](https://community.folivora.ai/t/add-info-from-nsworkspace-notifications-as-context-variable/21308 "2021-02-05T13:59:20Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Dirk](https://community.folivora.ai/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Dirk](https://community.folivora.ai/u/Dirk)\
**Post date:** [February 5, 2021, 5:34pm UTC](https://community.folivora.ai/t/add-info-from-nsworkspace-notifications-as-context-variable/21308/2 "2021-02-05T17:34:14Z")

</div>

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:

```auto
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:

```auto
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_

---

_[View the full topic](https://community.folivora.ai/t/add-info-from-nsworkspace-notifications-as-context-variable/21308)._
