False Applescript result in Cocoa
I have 3 tracks in iTunes and run this procedure:
-(IBAction)reloadButtonClick:(id)sender;
{
NSAppleScript *script ;
NSString *source ;
NSString *result;
NSDictionary *errorDic ;
NSAppleEventDescriptor *ed;
int total;
source= @"tell application \"iTunes\" to get count of tracks of playlist 1";
script = [[NSAppleScript alloc] initWithSource:source];
ed = [script executeAndReturnError:&errorDic];
if (ed == nil)
{
NSAlert *alert = [[NSAlert alloc]init];
[alert setMessageText:@"Error Occurred"];
[alert runModal];
[alert release];
}
result = [ed stringValue];
total = [result intValue];
开发者_JAVA百科NSAlert *alert = [[NSAlert alloc]init];
[alert setMessageText:[NSString stringWithFormat:@"%d",total]];
[alert runModal];
[alert release];
}
It always returns 0 and error is not occurred. But, if I execute the script inside Script Editor, it returns 3.
Anyone know what is wrong ? Is AppleScript inside cocoa unstable ?
Thanks.
PS: my iTunes version is 8.0.2 (20)
This bit looks suspect; why aren't you using total = [ed intValue]:
result = [ed stringValue];
total = [result intValue];
I just tried your code in a foundation tool. I changed it a little:
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSAppleScript *script ;
NSString *source ;
NSString *result;
NSDictionary *errorDic ;
NSAppleEventDescriptor *ed;
int total;
source= @"tell application \"iTunes\" to get count of tracks of playlist 1";
script = [[NSAppleScript alloc] initWithSource:source];
ed = [script executeAndReturnError:&errorDic];
if (ed == nil)
{
NSLog(@"Error Occurred");
}
result = [ed stringValue];
total = [result intValue];
NSLog( @"result: %d", total );
[pool drain];
return 0;
}
With iTunes 9.0.2 (on Mac OS X 10.6.2). It worked fine. It gave me the correct result for my first playlist, "Library".
精彩评论