所以我有我的代码通过制作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
答案 0 :(得分:1)
[getTrack executeAndReturnError:nil]返回NSAppleEventDescriptor
从NSAppleEventDescriptor获取NSString:
NSAppleEventDescriptor *resultDescriptor = [getTrack executeAndReturnError:nil];
return [resultDescriptor stringValue];
答案 1 :(得分: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(你知道我大声笑)