2012-07-14 28 views
0

当iTunes关闭时,不应该得到歌名,因为iTunes必须打开它才能检查并返回false。NSAppleScript自动打开iTunes

请帮忙。

//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]]; 

回答

2

你可能有更好的运气的东西,如:

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

男人,我可以吻你现在!我一直在努力寻找解决方案,以检查是否打开了一个应用程序,而无需使用com.apple.systemevents授权来检查苹果脚本中的授权。谢谢一堆! – 2012-10-12 01:37:54