Cocoa 中的 Applescript 错误结果
我在 iTunes 中有 3 首曲目并运行此过程:
-(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];
NSAlert *alert = [[NSAlert alloc]init];
[alert setMessageText:[NSString stringWithFormat:@"%d",total]];
[alert runModal];
[alert release];
}
它总是返回 0 并且不会发生错误。但是,如果我在脚本编辑器中执行脚本,它会返回 3。
有人知道出了什么问题吗?可可内的AppleScript不稳定吗?
谢谢。
PS:我的iTunes版本是8.0.2 (20)
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];
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这一点看起来很可疑;你为什么不使用total = [ed intValue]:
This bit looks suspect; why aren't you using total = [ed intValue]:
我刚刚在基础工具中尝试了您的代码。我对其进行了一些更改:
使用 iTunes 9.0.2(在 Mac OS X 10.6.2 上)。效果很好。它为我的第一个播放列表“图书馆”提供了正确的结果。
I just tried your code in a foundation tool. I changed it a little:
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".