Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nelly22

macrumors 6502
Original poster
Sep 29, 2009
366
5
Now and then i need to write small AppleScript to fix some temporary problem with my computer.

I would like my script to receive notifications even it is taxing. App launched or external disk mounted.

How i can do this even it's not recommended. Thanks
 

nelly22

macrumors 6502
Original poster
Sep 29, 2009
366
5
Depending on the version of OS X you are running, you can use NSWorkspace Notifications from a Cocoa-AppleScript template or an Xcode project.

Thanks. I use 10.8. I don't know anything about Cocoa and only little AppleScripting. How i can put one of those notifications to my AppleScript project created in Xcode or AppleScript Editor? For instance notification when app is launched or hard drive mounted. Thanks
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
Well, some knowledge of Cocoa and AppleScript will be needed to tweak things to get the desired result. In Lion and Mountain Lion, the AppleScript Editor includes a Cocoa-AppleScript Applet template in which you can access Cocoa via AppleScriptObjC.

Creating a new template using the following example in the main.scpt file will get notifications when applications launch and volumes are mounted:

Code:
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_
 

nelly22

macrumors 6502
Original poster
Sep 29, 2009
366
5
Well, some knowledge of Cocoa and AppleScript will be needed to tweak things to get the desired result. In Lion and Mountain Lion, the AppleScript Editor includes a Cocoa-AppleScript Applet template in which you can access Cocoa via AppleScriptObjC.

Creating a new template using the following example in the main.scpt file will get notifications when applications launch and volumes are mounted:

Code:
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_

Thanks!!! This is cool.

There is old app called GURL Watcher which don't work anymore. It logged all URLs that was visited by Safari.

What is the name of notification it uses? I try to do something similar.
 

nelly22

macrumors 6502
Original poster
Sep 29, 2009
366
5
There isn't a NSWorkspace notification when going to web pages - you might take a look at Parental Controls.

Thanks. I tried Parental Controls and similar apps, but those are not suitable for me.

How GURL Watcher knows what URL was opened in Safari? There is probably simple way to get this information. Thanks again
 

nelly22

macrumors 6502
Original poster
Sep 29, 2009
366
5
I'm asking again how GURL Watcher knows what URL was opened in Safari? Thanks
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.