[英]Get usable description for Apple Event
So I have my code to get the current song that is playing by making a AppleScript object and using the returned value from AppleScript as the info that is sent to the user. 因此,我有了代码,可以通过制作一个AppleScript对象并使用AppleScript返回的值作为发送给用户的信息来获取正在播放的当前歌曲。 Sadly it throws in a bunch of other junk that I need to get rid of.
可悲的是,它抛出了一堆我需要摆脱的其他垃圾。
Here is my code: 这是我的代码:
-(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
}
The returned value of currentTrack is: currentTrack的返回值为:
<NSAppleEventDescriptor: 'utxt'("track name here")>
So I need to get rid of <NSAppleEventDescriptor: 'utxt'(" and the ")> at the end
所以我需要在最后摆脱
<NSAppleEventDescriptor: 'utxt'(" and the ")> at the end
[getTrack executeAndReturnError:nil] return a NSAppleEventDescriptor [getTrack executeAndReturnError:nil]返回NSAppleEventDescriptor
To get a NSString from a NSAppleEventDescriptor : 要从NSAppleEventDescriptor获取NSString:
NSAppleEventDescriptor *resultDescriptor = [getTrack executeAndReturnError:nil];
return [resultDescriptor stringValue];
I hope this works: 我希望这可行:
-(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
}
It should return track name here
using the <NSAppleEventDescriptor: 'utxt'("track name here")>
example you gave me, however I escaped the "
manually, as I created a fake response like so: currentTrack = @"<NSAppleEventDescriptor: 'utxt'(\\"track name here\\")>";
它应该使用您给我的
<NSAppleEventDescriptor: 'utxt'("track name here")>
示例<NSAppleEventDescriptor: 'utxt'("track name here")>
返回track name here
,但是我手动逃脱了"
,因为我创建了一个伪造的响应,如下所示: currentTrack = @"<NSAppleEventDescriptor: 'utxt'(\\"track name here\\")>";
Please tell me if it worked, -Joseph Rautenbach (you know me lol) 请告诉我它是否有效,-Joseph Rautenbach(你知道我大声笑)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.