【发布时间】:2012-04-26 01:02:33
【问题描述】:
有没有办法向 iTunes 发送通知,以便它告诉我当前的歌曲。我知道它会在歌曲更改时发送通知,但我想在我的应用程序打开时获取当前歌曲。我试图避免使用脚本桥。
【问题讨论】:
标签: cocoa itunes nsnotifications
有没有办法向 iTunes 发送通知,以便它告诉我当前的歌曲。我知道它会在歌曲更改时发送通知,但我想在我的应用程序打开时获取当前歌曲。我试图避免使用脚本桥。
【问题讨论】:
标签: cocoa itunes nsnotifications
很遗憾,您无法通过通知执行此操作,您唯一的选择是脚本桥或其他 Apple 事件相关方法。
【讨论】:
NSAppleScript 发布原始 Apple 事件,但 Scripting Bridge 是最简单且推荐的方式。
您可以使用 NSAppleScript 来运行 AppleScript。
示例:获取当前曲目的标题和艺术家
NSAppleScript* theScript = [[NSAppleScript alloc] initWithSource:
[NSString stringWithFormat:
@"tell application \"iTunes\" to if running then\n"
@" try\n"
@" tell current track to return name & return & artist\n"
@" end try\n"
@" return \"no track\"\n"
@"end if\n"
@"return \"iTunes is not open\"\n"]];
NSDictionary *errorDict;
NSAppleEventDescriptor *resultDescriptor = [theScript executeAndReturnError:&errorDict];
NSString *title_artist = [resultDescriptor stringValue];
// do something with title_artist
示例:使用“资源文件夹”中的 AppleScript 文件
NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"theScriptName" ofType:@"scpt"];
NSAppleScript *theScript = [[NSAppleScript alloc] initWithContentsOfURL: [NSURL fileURLWithPath:scriptPath] error: nil];
另一种解决方案:您可以使用 NSTask 通过 osascript 运行 AppleScript
【讨论】: