2012-07-14 57 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