Notifications

Discussion in 'Mac Programming' started by nelly22, Nov 20, 2012.

  1. macrumors regular

    Joined:
    Sep 29, 2009
    #1
    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
     
  2. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    Depending on the version of OS X you are running, you can use NSWorkspace Notifications from a Cocoa-AppleScript template or an Xcode project.
     
  3. thread starter macrumors regular

    Joined:
    Sep 29, 2009
    #3
    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
     
  4. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #4
    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_
     
  5. thread starter macrumors regular

    Joined:
    Sep 29, 2009
    #5
    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.
     
  6. thread starter macrumors regular

    Joined:
    Sep 29, 2009
    #6
  7. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #7
    There isn't a NSWorkspace notification when going to web pages - you might take a look at Parental Controls.
     
  8. thread starter macrumors regular

    Joined:
    Sep 29, 2009
    #8
    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
     
  9. thread starter macrumors regular

    Joined:
    Sep 29, 2009
    #9
    I'm asking again how GURL Watcher knows what URL was opened in Safari? Thanks
     
  10. macrumors 6502a

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #10
    Maybe it uses a Safari plug-in. You could look for Safari SDK documentation.
     
  11. thread starter macrumors regular

    Joined:
    Sep 29, 2009
    #11
    It don't use Safari plug-in. Safari SDK is too much for my programming skills.
     

Share This Page