Page 1 of 1

HELP: Generic Growl AppleScript?

PostPosted: Fri Nov 26, 2010 7:10 pm
by gcoghill
How would I code the basic Growl AppleScript in order to send out a Growl alert via Hazel rules?

I don't want to set *all* my Growl alerts to my iPhone (via Howl), just for certain events (like file conversion completing).

I tried just adding a generic Growl AppleScript, but Hazel gives me an error:

Code: Select all
tell application "GrowlHelperApp" -- ** the daemon that is behind the scenes
   -- Make a list of all the notification types that this script will ever send:
   -- ** They really mean "ever" or you'll have to reregister.
   set the allNotificationsList to {"Handbrake convert complete"}
   
   -- Make a list of the notifications that will be enabled by default.
   -- ** We'll see shortly that a note must be enabled and the user can disable it.
   -- Notifications not enabled by default can be enabled later in the 'Applications' tab of the growl prefpane.
   set the enabledNotificationsList to {"Handbrake convert complete"} -- ** just one turned on, the other not.
   
   -- Register our script with growl.
   -- You can optionally (as here) set a default icon for this script's notifications.
   -- ** Theoretically, you only have to register once, but there is absolutely no harm in doing
   -- it every time the script runs, [i]i.e.[/i], leaving this line in your script.
   register as application "Handbrake Encode Growler" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "HandBrake"
   
   --    Send a Notification...
   -- This one will appear because it was enabled by default.
   notify with name "Handbrake convert complete" title "Handbrake convert is complete" description "Handbrake has finished encoding." application name "Handbrake Encode Growler"
end tell


I know in the Hazel help there is some extra code I need to add ("theFile" etc.) to get external scripts to work with Hazel, but not sure how to go about doing this with Growl alerts.

BY doing this, I can set just this one Hazel event to send a specific Growl alert, and then have that forwarded to my iPhone (via Howl). Otherwise I need to send *all* my Hazel Growl alerts to the iPhone, which is undesirable.

Re: HELP: Generic Growl AppleScript?

PostPosted: Fri Nov 26, 2010 11:47 pm
by gcoghill
OK I discovered that the code I posted works fine if you add the AppleScript right into the Hazel rules. I'd still like to know how to do this with an external AppleScript though, so if anyone can help it would be appreciated.

One followup question: how would I go about incorporating the file name into the Growl alert?

I tried this:
Code: Select all
set movieName to name of theFile
tell application "GrowlHelperApp"
   set the allNotificationsList to {"Handbrake convert complete"}
   set the enabledNotificationsList to {"Handbrake convert complete"}
   register as application "Handbrake Encode Growler" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "HandBrake"
   notify with name "Handbrake convert complete" title "Handbrake conversion complete" description "Handbrake has finished converting " & movieName & "." application name "Handbrake Encode Growler"
end tell

I also tried putting the "set movieName" line within the main "tell", but no difference.

Re: HELP: Generic Growl AppleScript?

PostPosted: Sat Nov 27, 2010 4:10 am
by gcoghill
I answered my own question, here's the AppleScript code if anyone is interested:
Code: Select all
on hazelProcessFile(theFile)
   tell application "Finder"
      set itemName to name of item theFile
   end tell
   tell application "GrowlHelperApp"
      set the allNotificationsList to {"Hazel action complete"}
      set the enabledNotificationsList to {"Hazel action complete"}
      register as application "Hazel Action Growler" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "HazelHelper.app"
      notify with name "Hazel action complete" title "Hazel Action Complete" description "The file \"" & itemName & "\" has been processed by Hazel." application name "Hazel Action Growler"
   end tell
end hazelProcessFile

This will result in a Growl alert being displayed for whatever file was just acted upon by Hazel. By saving this as an AppleScript, and changing the Growl Application for each copy you make, you can set up specific growl alerts for individual Growl actions, and not be limited by the blanket Growl alerts from Hazel.

In particular I wanted to use this with Howl, an iPhone app and growl style that will forward Growl alerts from your Mac to your iPhone. By having Hazel Growl alerts separate from the blanket alerts sent by the app, I can pick and choose which events/actions send Growl alerts to my iPhone via Howl.

Even more particular, I set this up so that as Hazel processed video files, I could get Growl alerts sent to me when a video was finished converting, and then again after Hazel imported the file into iTunes.

Re: HELP: Generic Growl AppleScript?

PostPosted: Sun Nov 28, 2010 8:45 pm
by dhy8386
Sounds amazing. Can you be more specific how you incorporate the apple script? Is it embedded within certain hazel rules? How do you specify which processes send a growl? How do u make it work with howl?

Re: HELP: Generic Growl AppleScript?

PostPosted: Sun Nov 28, 2010 9:32 pm
by gcoghill
The code I posted is for a standalone (external) AppleScript file, which you would call up as a Hazel result. If you wanted to use this as an embedded script, I believe you just need to delete the code "on hazelProcessFile(theFile)" at the top, and "end hazelProcessFile" at the bottom. In Hazel, just choose "Run AppleScript" as (one of) your result action(s), and then either embed the AppleScript or locate the saved external file on your hard drive. The template code I posted is very simple, it has one notification ("Hazel action complete") and will show up in your Growl apps list as "Hazel Action Growler". To add more notifications, just add them like so:

set the allNotificationsList to {"Hazel action complete"} -> set the allNotificationsList to {"Hazel action complete" , "Another Hazel action"}

Be sure to add them to the enabled notifications as well. To choose which notification to use, edit the line: notify with name "Hazel action complete", and the name of the alert would be in the quotes. So if you used the second alert above, it would read: notify with name "Another Hazel action"

Since you can specify per-alert styles, it is probably best to just add all your alerts to this one virtual Growl app, and specify as needed. If you want to change the icon of the alert or the title of the alert, you'll need to create separate "virtual Growl apps" like this one.

As far as using it with Howl, you'll need to buy Howl for your iPhone, install Howl's Growl style, and then specify the Howl style alert as your alert for these notifications you want sent to your iPhone. There's a setting in the Howl style to "forward" notifications, which will allow you to have them not only get sent to your iPhone, but also display locally on your Mac with your preferred style.

Hope that was clear enough, any questions just ask.

Re: HELP: Generic Growl AppleScript?

PostPosted: Sun Nov 28, 2010 9:37 pm
by gcoghill
Also, the way I used this in particular was to have Hazel send me a growl alert when files were done being processed and added to iTunes, so in my case the AppleScript actually looks like this:
Code: Select all
on hazelProcessFile(theFile)
   tell application "Finder"
      set movieName to name of item theFile
   end tell
   tell application "GrowlHelperApp"
      set the allNotificationsList to {"iTunes import complete"}
      set the enabledNotificationsList to {"iTunes import complete"}
      register as application "iTunes Import Growler" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "iTunes"
      notify with name "iTunes import complete" title "iTunes import complete" description "The video \"" & movieName & "\" has been tagged and imported to iTunes." application name "iTunes Import Growler"
   end tell
end hazelProcessFile

This way, I can send a Growl alert for just this one specific result action, and have just that one Growl alert sent to my iPhone via Howl. For this one, I made a unique "virtual Growl app" to send this alert with an iTunes icon.