Cocoa 中的 Applescript 错误结果

发布于 2024-08-18 10:46:12 字数 1089 浏览 7 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笔一画续写前缘 2024-08-25 10:46:12

这一点看起来很可疑;你为什么不使用total = [ed intValue]:

    result = [ed stringValue];
    total = [result intValue]; 

This bit looks suspect; why aren't you using total = [ed intValue]:

    result = [ed stringValue];
    total = [result intValue]; 
白首有我共你 2024-08-25 10:46:12

我刚刚在基础工具中尝试了您的代码。我对其进行了一些更改:

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;
}

使用 iTunes 9.0.2(在 Mac OS X 10.6.2 上)。效果很好。它为我的第一个播放列表“图书馆”提供了正确的结果。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文