Re: Check if app exists
Re: Check if app exists
- Subject: Re: Check if app exists
- From: has <email@hidden>
- Date: Fri, 21 Sep 2007 14:41:24 +0100
Jeremy Matthews wrote:
However, when I ADD the growl example code (link to sample code
noted in the script above), it still prompts the user to find the
application.
Ideas?
Yep, AS just loves to throw up these involuntary "Where is
application X?" dialogs on the flimsiest of pretexts, whether they're
actually wanted or needed or not. Definitely one of it's more
annoyingly "helpful" features. Personally I'd recommend Python with
PyObjC, appscript and py2app as a much nicer and refreshingly non-
addlebrained development platform, but I'm guessing you probably want
an AppleScript-based solution for now:
The problem here is due to the way that the AppleScript compiler
treats 'application "AppName"' literals as a special case, and a run-
time side-effect of this is seems to be that even if that expression
is never actually executed, AppleScript still checks if the
application is installed and throws up that stupid dialog if it can't
find it.
The workaround is to avoid using 'application "AppName"' literals in
your code, so instead of writing:
tell application "GrowlHelperApp"
...
end tell
use something like:
using terms from application "GrowlHelperApp"
tell application (get "GrowlHelperApp")
...
end tell
end using terms from
Replacing the literal specifier with one that has to be evaluated at
runtime prevents the AS compiler recognising it as a special case,
avoiding the run-time side-effect described above. Of course, this
also means that the AS compiler won't know to obtain additional
terminology from that application when compiling code within that
'tell application ...' block, so wrap it in a 'using terms from'
compiler directive. Pretty ugly, but it gets the job done.
...
Incidentally, since you're in Studio, here's a quick-n-dirty ObjC
wrapper for LSFindApplicationForInfo that you can invoke via 'call
method':
/* findapp.h */
#include <Cocoa/Cocoa.h>
#include <ApplicationServices/ApplicationServices.h>
@interface ASSFindApplication : NSObject
+ (NSAppleEventDescriptor *)findByID:(NSString *)bundleID;
@end
/* findapp.m */
#include "findapp.h"
@implementation ASSFindApplication
+ (NSAppleEventDescriptor *)findByID:(NSString *)bundleID {
OSStatus err;
FSRef fref;
err = LSFindApplicationForInfo(kLSUnknownCreator,
(CFStringRef)bundleID,
NULL,
&fref,
NULL);
if (err) [NSException raise: @"FindApplicationError" format: @"%i",
err];
return [NSAppleEventDescriptor descriptorWithDescriptorType: typeFSRef
bytes: &fref
length: sizeof(fref)];
}
@end
I only bothered implementing it for bundle IDs, but it wouldn't be
hard to add support for finding by name or creator type as well.
Include this class in your Studio project and call it as follows:
call method "findByID:" of class "ASSFindApplication" with parameter
"com.Growl.GrowlHelperApp"
The result is a file ref to the application file, or an error if it's
not found.
Note: the one problem with this code is that AppleScriptKit's error
bridging is rather cack and ObjC headers and documentation non-
existent (Have I mentioned how much nicer PyObjC is?), so you only
get a generic NSInternalScriptError (8) instead of a meaningful error
message/number. But I suppose you could return a descriptor
containing the error number instead of the file ref and let the
script sort it out rather than throwing an exception if you
preferred. Or perhaps someone out there knows the magic incantation
for raising more meaningful errors?
HTH
has
--
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden