Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 20, 2012, 07:13 AM   #1
nelly22
macrumors regular
 
Join Date: Sep 2009
Notifications

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 is offline   0 Reply With Quote
Old Nov 20, 2012, 02:16 PM   #2
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
Depending on the version of OS X you are running, you can use NSWorkspace Notifications from a Cocoa-AppleScript template or an Xcode project.
__________________
MacBook Pro / OS X Mavericks (10.9.4) / Xcode 5.1.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Nov 21, 2012, 10:28 AM   #3
nelly22
Thread Starter
macrumors regular
 
Join Date: Sep 2009
Quote:
Originally Posted by Red Menace View Post
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
nelly22 is offline   0 Reply With Quote
Old Nov 24, 2012, 08:32 PM   #4
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, 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_
__________________
MacBook Pro / OS X Mavericks (10.9.4) / Xcode 5.1.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Dec 1, 2012, 09:10 AM   #5
nelly22
Thread Starter
macrumors regular
 
Join Date: Sep 2009
Quote:
Originally Posted by Red Menace View Post
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 is offline   0 Reply With Quote
Old Dec 5, 2012, 01:31 AM   #6
nelly22
Thread Starter
macrumors regular
 
Join Date: Sep 2009
This is the GURL Watcher app i was talking about:

http://www.quicomm.com/gw_overview.html
nelly22 is offline   0 Reply With Quote
Old Dec 5, 2012, 08:18 AM   #7
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
There isn't a NSWorkspace notification when going to web pages - you might take a look at Parental Controls.
__________________
MacBook Pro / OS X Mavericks (10.9.4) / Xcode 5.1.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Dec 6, 2012, 04:59 AM   #8
nelly22
Thread Starter
macrumors regular
 
Join Date: Sep 2009
Quote:
Originally Posted by Red Menace View Post
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 is offline   0 Reply With Quote
Old Dec 14, 2012, 09:39 AM   #9
nelly22
Thread Starter
macrumors regular
 
Join Date: Sep 2009
I'm asking again how GURL Watcher knows what URL was opened in Safari? Thanks
nelly22 is offline   0 Reply With Quote
Old Dec 14, 2012, 11:33 AM   #10
mfram
macrumors 6502a
 
Join Date: Jan 2010
Location: San Diego, CA USA
Maybe it uses a Safari plug-in. You could look for Safari SDK documentation.
mfram is offline   0 Reply With Quote
Old Dec 15, 2012, 09:38 AM   #11
nelly22
Thread Starter
macrumors regular
 
Join Date: Sep 2009
Quote:
Originally Posted by mfram View Post
Maybe it uses a Safari plug-in. You could look for Safari SDK documentation.
It don't use Safari plug-in. Safari SDK is too much for my programming skills.
nelly22 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Forum notifications in safari notifications? Nimrad Site and Forum Feedback 3 Nov 26, 2013 11:22 PM
AP Mobile - double notifications - clusters of delayed notifications Gomer iOS 6 0 Apr 16, 2013 09:45 PM
Facebook Notifications... Notifications Center Jack9034 OS X 10.8 Mountain Lion 4 Jan 25, 2013 08:36 PM
Easily Enable Gmail Notifications In Notifications Centre smoge OS X 10.8 Mountain Lion 2 Jan 13, 2013 02:54 PM
How do I get app & system update notifications in Notifications Center? inspirations365 OS X 10.8 Mountain Lion 5 Aug 3, 2012 07:35 AM

Forum Jump

All times are GMT -5. The time now is 03:10 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC