Re: Fetching result from NSAppleScript
Re: Fetching result from NSAppleScript
- Subject: Re: Fetching result from NSAppleScript
- From: Matt Neuburg <email@hidden>
- Date: Thu, 19 Jul 2007 17:57:48 -0700
- Thread-topic: Fetching result from NSAppleScript
On Thu, 19 Jul 2007 11:24:05 -0700, Nirnimesh <email@hidden> said:
>I'm quite a n00b to Cocoa.
>
>I want to run an applescript and fetch its results in my cocoa app.
>The applescript is: tell application \"Finder\" to get every window
>
>#import <Cocoa/Cocoa.h>
>
>
>int main(int argc, char *argv[]) {
> NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
>
> NSString* string = @"tell application \"Finder\" to get every window";
> NSLog(string);
> NSAppleScript* script = [[NSAppleScript alloc] initWithSource:string];
> NSDictionary* errorDict = [NSDictionary alloc];
> NSAppleEventDescriptor* des = [script executeAndReturnError:&errorDict];
> NSData* rawdata = [des data];
> NSString* out = [NSString stringWithCharacters:[rawdata bytes] length:
>[rawdata length]/sizeof(unichar)];
> printf("output %s", out);
>
> [pool release];
> return 0; //NSApplicationMain(argc, (const char **) argv);
>}
>
>The above doesn't work.
Quite so. You have received an Apple event descriptor. It is up to you to
parse it. It is not data, so you are parsing it wrong. It is a list (of
object references). It is up to you ask for it as a list, and, if you want
the individual items of the list, to fetch them out yourself.
These object references, however, are not going to be readily useful in
Cocoa, outside the context of the AppleScript world (because they are
stripped of the psn information about what application they belong to), so
it would be better to extract further information - the actual information
that you need - before returning from AppleScript. You will have an easier
time if you start by doing this in pure AppleScript (e.g. in Script Editor
or Script Debugger) and thinking there about what you're asking and what
you're receiving (and what it is you're really after). There are various
solutions but none of them are simple. Things will be easier, I expect, if
you wait for Leopard where you can take advantage of the scripting bridge
and do the whole thing easily in pure Cocoa without recourse to
NSAppleScript.
Perhaps it would be best if you just stated what it is that you are really,
ultimately, hoping to do. m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
One of the 2007 MacTech Top 25: <http://tinyurl.com/2rh4pf>
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden