简体   繁体   English

错误的Applescript结果导致可可

[英]False Applescript result in Cocoa

I have 3 tracks in iTunes and run this procedure: 我在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];
    }

It always returns 0 and error is not occurred. 它始终返回0,并且不会发生错误。 But, if I execute the script inside Script Editor, it returns 3. 但是,如果我在脚本编辑器中执行脚本,它将返回3。

Anyone know what is wrong ? 有人知道怎么了吗? Is AppleScript inside cocoa unstable ? 可可里面的AppleScript不稳定吗?

Thanks. 谢谢。

PS: my iTunes version is 8.0.2 (20) PS:我的iTunes版本是8.0.2(20)

This bit looks suspect; 这看起来有点可疑; why aren't you using total = [ed intValue]: 为什么不使用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). 使用iTunes 9.0.2(在Mac OS X 10.6.2上)。 It worked fine. 工作正常。 It gave me the correct result for my first playlist, "Library". 它为我的第一个播放列表“库”提供了正确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM