NSAppleScript自动打开iTunes

时间:2012-07-14 13:21:12

标签: objective-c if-statement applescript itunes

当iTunes关闭时,不应该获取歌曲名称,因为iTunes必须打开才能检查并返回false。

请帮助。

//run applescript
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"try\n tell application \"System Events\" to set checkIfItunesIsRunning to (name of processes) contains \"iTunes\"\n if checkIfItunesIsRunning is equal to true then\n tell application \"iTunes\" to get name of current track\n else\n return \"No Song\"\n end if\n on error errmsg number errNum\n return \"No Song\"\n end try"];

NSAppleEventDescriptor *theResult = [script executeAndReturnError:nil];
//Set MenuItem
    [song setTitle:[theResult stringValue]];

1 个答案:

答案 0 :(得分:2)

你可能会有更好的运气:

if ([[NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.iTunes"] count] != 0) {
    // run your script
} else {
    // itunes isn't running
}