Re: Fetching result from NSAppleScript
Re: Fetching result from NSAppleScript
- Subject: Re: Fetching result from NSAppleScript
- From: has <email@hidden>
- Date: Thu, 19 Jul 2007 22:09:36 +0100
Nirnimesh wrote:
I want to run an applescript and fetch its results in my cocoa app.
The applescript is: tell application \"Finder\" to get every window
[...]
NSAppleEventDescriptor* des = [script
executeAndReturnError:&errorDict];
NSData* rawdata = [des data];
NSString* out = [NSString stringWithCharacters:[rawdata bytes]
length:
[rawdata length]/sizeof(unichar)];
[...]
The above doesn't work. I get:
output `\262\200\240\220
How can I fetch the output?
What output were you hoping to get? The script is returning a list of
object specifiers, e.g.:
#import <Cocoa/Cocoa.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSAppleScript* script = [[NSAppleScript alloc] initWithSource:
@"tell application \"Finder\" to get every window"];
NSAppleEventDescriptor* des = [script executeAndReturnError:NULL];
NSLog(@"%@", des);
[pool release];
return 0;
}
Result:
<NSAppleEventDescriptor: [
'obj '{ 'form':'ID ', 'want':'brow', 'seld':491,
'from':''null''() },
'obj '{ 'form':'ID ', 'want':'brow', 'seld':448,
'from':''null''() },
'obj '{ 'form':'ID ', 'want':'brow', 'seld':490,
'from':''null''() }
]
You need to use the methods provided by NSAppleEventDescriptor to
manipulate that data further. If you want more specific advice, you
need to say a bit more about what you're trying to achieve.
Also check out objc-appscript (link is in my sig), which is a high-
level Cocoa-Apple event bridge that'll allow you to send events
directly from ObjC and provides automatic conversion between ObjC and
Apple event types. Example:
// To make glue: osaglue FN Finder
#import "FNGlue.h"
int main(int argc, char *argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
FNApplication *finder = [[FNApplication alloc] initWithName:
@"Finder.app"];
NSArray *res = [[[finder windows] get] send];
NSLog(@"%@", res);
[finder release];
[pool release];
return 0;
}
Result:
(
[[FNApp FinderWindows] byID: 491],
[[FNApp FinderWindows] byID: 448],
[[FNApp FinderWindows] byID: 490]
)
HTH
has
--
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org
http://appscript.sourceforge.net/objc-appscript.html
_______________________________________________
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