0

所以我有我的代码来获取当前正在播放的歌曲,方法是创建一个 AppleScript 对象并使用 AppleScript 的返回值作为发送给用户的信息。可悲的是,它抛出了一堆我需要摆脱的其他垃圾。

这是我的代码:

-(NSString *)getCurrentTrack {
    NSString *currentTrack = @"";
    NSAppleScript *getTrack = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\" to get the name of the current track"];


    currentTrack = [getTrack executeAndReturnError:nil];

    return currentTrack;

    //tell application "iTunes" to get the name of the current track
}

currentTrack 的返回值为:

<NSAppleEventDescriptor: 'utxt'("track name here")>

所以我需要摆脱<NSAppleEventDescriptor: 'utxt'(" and the ")> at the end

4

2 回答 2

1

[getTrack executeAndReturnError:nil] return a NSAppleEventDescriptor

To get a NSString from a NSAppleEventDescriptor :

NSAppleEventDescriptor *resultDescriptor = [getTrack executeAndReturnError:nil];
return [resultDescriptor stringValue];
于 2012-04-29T20:16:44.983 回答
0

我希望这有效:

-(NSString *)getCurrentTrack {
    NSString *currentTrack = @"";
    NSAppleScript *getTrack = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\" to get the name of the current track"];


    currentTrack = [getTrack executeAndReturnError:nil];

    currentTrack = [currentTrack stringByReplacingOccurrencesOfString:@"\"" withString:@"'"];
    NSArray *left = [currentTrack componentsSeparatedByString:@"('"];
    NSString *text2 = [left objectAtIndex:1];
    NSArray *right = [text2 componentsSeparatedByString:@"')"];

    return [right objectAtIndex:0];


    //tell application "iTunes" to get the name of the current track
}

它应该track name here使用<NSAppleEventDescriptor: 'utxt'("track name here")>您给我的示例返回,但是我手动转义了",因为我创建了一个像这样的假响应:currentTrack = @"<NSAppleEventDescriptor: 'utxt'(\"track name here\")>";

请告诉我它是否有效,-Joseph Rautenbach(你认识我,哈哈)

于 2012-04-29T16:43:58.247 回答