Re: Retrieving data returned by AppleScript
Re: Retrieving data returned by AppleScript
- Subject: Re: Retrieving data returned by AppleScript
- From: Matt Neuburg <email@hidden>
- Date: Wed, 28 Dec 2005 05:01:45 -0800
- Thread-topic: Retrieving data returned by AppleScript
On Tue, 27 Dec 2005 15:56:41 +0100, Eric Morand <email@hidden> said:
>
Hi List !
>
>
I'm currently trying to call an applescript from a COCOA code and am
>
having problems to retrieve the data returned by the script.
>
>
Here is the script (named getEncoder.scpt) :
>
>
tell application "iTunes"
>
set availableEncoders to (format of every encoder)
>
display dialog "It Works !!!"
>
return availableEncoders
>
end tell
>
>
>
And here is the code I use to execute it :
>
>
NSString *appleScriptPath = [[NSBundle bundleForClass:[self class]]
>
pathForResource:@"getEncoders" ofType:@"scpt"];
>
NSURL *url = [NSURL fileURLWithPath:appleScriptPath];
>
NSDictionary *theDict = [[[NSDictionary alloc] init] autorelease];
>
NSAppleScript *theScript;
>
NSAppleEventDescriptor *desc;
>
theScript = [[[NSAppleScript alloc] initWithContentsOfURL:url
>
error:&theDict] autorelease];
>
desc = [theScript executeAndReturnError:&theDict];
>
>
NSArray * avalaibleEncoders = [desc data];
>
>
>
The script is executed correctly (the "It Works" dialog appears) but
>
I'm unable to retrieve the data sent by the script (availableEcoders
>
remains empty).
(1) An NSArray is not an NSData.
(2) From the Apple docs on NSAppleEventDescriptor: "In working with Apple
event descriptors, it can be useful to understand some of the underlying
data types."
(3) Look at the example on p. 35 of my AppleScript book.
(4) Rewrite your code accordingly (you should add error-checking):
- (IBAction)doButton:(id)sender
{
NSString* s = @"tell app \"iTunes\" to format of encoders";
NSAppleScript* scr = [[[NSAppleScript alloc] initWithSource:s]
autorelease];
NSAppleEventDescriptor* desc = [scr executeAndReturnError:nil];
NSMutableArray* arr = [NSMutableArray array];
int i,u = [desc numberOfItems];
for (i=1; i<=u; i++)
[arr addObject: [[desc descriptorAtIndex: i] stringValue]];
}
m.
--
matt neuburg, phd = email@hidden, <
http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide
<
http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden