Page 1 of 2

Growl Applescript and no Growl installed

Posted: Wed Aug 03, 2005 5:54 am
by crashtron
Hallo,

I wrote same Applescripts that make use of Growl. Now I want to share this scripts. Growl is just a extra in this scripts so the user don't have to install Growl to use it. But when somebody starts the script and he has Growl not installed he gets an error messages that GrowlHelper.app could not be found. What could I do to eliminate this messages in the case that Growl is not installed?

Thx
crashtron

Posted: Wed Aug 03, 2005 7:07 am
by crashtron
I did it by my own. I wrote a small function that checks if Growl is installed on the system. You could get it here http://www.macmediacenter.de/node/89

Check it out and post if it is working for you.

Perhaps somebody has a more elegant way to check.

Bye
Jan

Posted: Wed Aug 03, 2005 4:04 pm
by nanovivid
I don't have it with me right now, but there's a way to check if Growl is running. I'll post it once I get home tonight.

Posted: Wed Aug 03, 2005 4:41 pm
by DeltaTee
You should just be able to wrap the growl commands in a try ... end try block which would effectively ignore any errors that are produced.

Posted: Wed Aug 03, 2005 6:45 pm
by nanovivid
Not necessarily... AppleScript is panicky about

Code: Select all

tell application "SomethingOrOther"
statements where it doesn't know about an app called SomethingOrOther. It'll pop up a box saying "Where is SomethingOrOther?"

In Growl's case, if Growl isn't installed, AppleScript will ask "Where is GrowlHelperApp?" before letting the script run at all. It's a pain.

That's why it's necessary to do some trickery to get around this behavior and fool AppleScript into not looking for GrowlHelperApp until you're sure it's installed.

Posted: Wed Aug 03, 2005 10:53 pm
by nanovivid
Here's a function I created for an AppleScript project. It should give you a good idea of what's needed to detect Growl's presence.

Code: Select all

on GrowlNotify(myTitle, myText)
	set myApp to ""
	
	tell application "System Events" to set GrowlRunning to ((application processes whose (name is equal to "GrowlHelperApp")) count)
	
	if GrowlRunning >= 1 then
		try
			set myApp to "GrowlHelperApp"
			
			set appName to "My Spiffy Script"
			set notifs to "{\"Exciting Thing #1\", \"Exciting Thing #2\"}"
			
			tell application myApp to run script "register as application \"" & appName & "\" all notifications " & notifs & " default notifications " & notifs & " icon of application \"Pretty Application\""
			
			tell application myApp to run script "notify with name \"Number Dialed\" title \"" & myTitle & "\" application name \"" & appName & "\" description \"" & myText & "\" icon of application \"Pretty Application\""
		end try
	end if
end GrowlNotify

Posted: Thu Aug 04, 2005 4:41 am
by zaudragon
nanovivid wrote:Here's a function I created for an AppleScript project. It should give you a good idea of what's needed to detect Growl's presence.

Code: Select all

on GrowlNotify(myTitle, myText)
	set myApp to ""
	
	tell application "System Events" to set GrowlRunning to ((application processes whose (name is equal to "GrowlHelperApp")) count)
	
	if GrowlRunning >= 1 then
		try
			set myApp to "GrowlHelperApp"
			
			set appName to "My Spiffy Script"
			set notifs to "{"Exciting Thing #1", "\Exciting Thing #2"}"
			
			tell application myApp to run script "register as application "" & appName & "" all notifications " & notifs & " default notifications " & notifs & " icon of application "Address Book""
			
			tell application myApp to run script "notify with name "Number Dialed" title "" & myTitle & "" application name "" & appName & "" description "" & myText & "" icon of application "Pretty Application""
		end try
	end if
end GrowlNotify
Wow. Why? You could always do:

Code: Select all

set toppage to do shell script "ps ax | grep --count -i GrowlHelperApp"
if toppage >= 1 then
bla
end if

Posted: Thu Aug 04, 2005 9:27 am
by crashtron
nanovivid wrote:Here's a function I created for an AppleScript project. It should give you a good idea of what's needed to detect Growl's presence.

Code: Select all

on GrowlNotify(myTitle, myText)
	set myApp to ""
	
	tell application "System Events" to set GrowlRunning to ((application processes whose (name is equal to "GrowlHelperApp")) count)
	
	if GrowlRunning >= 1 then
		try
			set myApp to "GrowlHelperApp"
			
			set appName to "My Spiffy Script"
			set notifs to "{"Exciting Thing #1", "\Exciting Thing #2"}"
			
			tell application myApp to run script "register as application "" & appName & "" all notifications " & notifs & " default notifications " & notifs & " icon of application "Address Book""
			
			tell application myApp to run script "notify with name "Number Dialed" title "" & myTitle & "" application name "" & appName & "" description "" & myText & "" icon of application "Pretty Application""
		end try
	end if
end GrowlNotify
The script seems to cognize if growlhelperapp is running but i can't get the growl message to work.

I always get an error message from the script editor "unexpected end of line" or "identifer expected" and so on.

How do I have to escape the script that stand in the "tell application myApp to run script"? Perhaps that is the problem.

thx for the help

Posted: Fri Aug 05, 2005 11:41 pm
by zaudragon
crashtron wrote:
nanovivid wrote:Here's a function I created for an AppleScript project. It should give you a good idea of what's needed to detect Growl's presence.

Code: Select all

on GrowlNotify(myTitle, myText)
	set myApp to ""
	
	tell application "System Events" to set GrowlRunning to ((application processes whose (name is equal to "GrowlHelperApp")) count)
	
	if GrowlRunning >= 1 then
		try
			set myApp to "GrowlHelperApp"
			
			set appName to "My Spiffy Script"
			set notifs to "{"Exciting Thing #1", "\Exciting Thing #2"}"
			
			tell application myApp to run script "register as application "" & appName & "" all notifications " & notifs & " default notifications " & notifs & " icon of application "Address Book""
			
			tell application myApp to run script "notify with name "Number Dialed" title "" & myTitle & "" application name "" & appName & "" description "" & myText & "" icon of application "Pretty Application""
		end try
	end if
end GrowlNotify
The script seems to cognize if growlhelperapp is running but i can't get the growl message to work.

I always get an error message from the script editor "unexpected end of line" or "identifer expected" and so on.

How do I have to escape the script that stand in the "tell application myApp to run script"? Perhaps that is the problem.

thx for the help
Two ways:

Code: Select all

tell application myApp to do whatever

Code: Select all

tell application "GrowlHelperApp" to do whatever
The latter’s “” are required.

Posted: Fri Aug 05, 2005 11:45 pm
by nanovivid
Sorry about that! I was typing too fast and wrote "\ instead of \" at one point. I've edited my original post to correct it and the corrected version is posted here as well:

Code: Select all

on GrowlNotify(myTitle, myText)
	set myApp to ""
	
	tell application "System Events" to set GrowlRunning to ((application processes whose (name is equal to "GrowlHelperApp")) count)
	
	if GrowlRunning >= 1 then
		try
			set myApp to "GrowlHelperApp"
			
			set appName to "My Spiffy Script"
			set notifs to "{\"Exciting Thing #1\", \"Exciting Thing #2\"}"
			
			tell application myApp to run script "register as application \"" & appName & "\" all notifications " & notifs & " default notifications " & notifs & " icon of application \"Pretty Application\""
			
			tell application myApp to run script "notify with name \"Number Dialed\" title \"" & myTitle & "\" application name \"" & appName & "\" description \"" & myText & "\" icon of application \"Pretty Application\""
		end try
	end if
end GrowlNotify

Posted: Sat Aug 06, 2005 2:32 am
by The_Tick
Hey nanovivid. When you guys get this figured out, can you add this and http://trac.growl.info/trac/ticket/324 to the applescript docs?

Posted: Sat Aug 06, 2005 4:31 am
by DeltaTee
This code should be equivalent, shorter and easier to use (it sets a boolean):

tell application "System Events" to set GrowlExists to exists application process ("GrowlHelperApp" as string)

Posted: Sat Aug 06, 2005 1:29 pm
by nanovivid
The_Tick wrote:Hey nanovivid. When you guys get this figured out, can you add this and http://trac.growl.info/trac/ticket/324 to the applescript docs?
The ironic thing about that is that it was in the original version of the applescript docs that I wrote and somebody removed it, commenting that the docs were too "heavy handed" or something. *shrug*

I'll work on getting it back in there. :)

Posted: Sat Aug 06, 2005 7:17 pm
by The_Tick
Cool. I have no problem with the docs being verbose.

Posted: Wed Sep 07, 2005 7:31 pm
by MattPat
OK, I'm having a problem similar to the original problem (well, not similar, exactly the same). It seems that this problem has not yet been solved.

We've established it's possible to check if Growl is installed in many, many ways. Personally, I just ask the user to tell me, and choose whether they want Growl enabled or not.

The problem is that if AppleScript references another application, it always tries to find that application to check to see if the script is valid-- whether that application actually gets used or not. The only way I've found to circumvent this is to comment out any "tell" line that uses Growl, but I'd really rather not distribute "Growl-enabled" and "Growl-Free" versions of my application.

The only workaround I was able to think up was to create a tiny, false application (with the same dictionary as Growl) that could be distributed with Growl-enabled scripts, and then use "using terms from" to change the dictionary. For example:

Code: Select all

if growlexists is true then
   tell application "GrowlHelperApp"
      using terms from application "FakeGrowl"
         -- put Growl code here
      end using terms from
   end tell
end if
This would work, because AppleScript would look for FakeGrowl (distributed with your script/application) rather than GrowlHelperApp to validate the code, but would send the actual statements to GrowlHelperApp if present. The only problem: that's a lot of work just to get the script to run without an error! (It would admittedly become easier if someone create the app, then posted it online for everyone to use, but that's still a lot of work).

The question is: is it possible to make AppleScript not try to validate the code inside tell blocks (or is there another way to make it work without having to create a fake Growl)?

Thanks

-Matt

Posted: Wed Sep 07, 2005 7:56 pm
by nanovivid
Matt, look at my code posted above. It detects Growl's presence without the "Where is GrowlHelperApp" box popping up.

Posted: Wed Sep 07, 2005 8:39 pm
by MattPat
I never copied the code verbatim (and actually, still haven't) and my scripts weren't working, but that's because I was missing the key piece that made it work. I too tried putting the app name in a variable, but never surrounded it in the try block. Kinda' weird that that actually works... but who's complaining, since it does work? :wink:

Sorry about that, and thanks!

-Matt

Posted: Thu Aug 23, 2007 10:39 pm
by soj

Code: Select all

try
	set growlFound to false
	set growlSubPath to "PreferencePanes:Growl.prefpane"
	set growlLocalPath to (path to library folder from local domain as string) & growlSubPath
	set growlUserPath to (path to library folder from user domain as string) & growlSubPath
	
	-- look for growl in computer's library and user's library 
	if exists file growlLocalPath then
		set growlFound to true
		open file growlLocalPath
	end if
	if exists file growlUserPath then
		set growlFound to true
		open file growlLocalPath
	end if
	if growlFound is false then error
	
on error errMsg number errNum
	
	(* whatever you want the script to do if growl is not installed. *)

	
end try

Posted: Thu Aug 23, 2007 10:48 pm
by soj
The_Tick wrote:Hey nanovivid. When you guys get this figured out, can you add this and http://trac.growl.info/trac/ticket/324 to the applescript docs?
What are the applescript docs?

Posted: Thu Aug 23, 2007 10:49 pm
by The_Tick
They're documents describing how to use our applescript api. It's up on the website under the documentation area.