atomikpanda
atomikpanda

Reputation: 1886

NSAppleScript Opens iTunes automatically

This shouldn't get the song name when iTunes is closed because iTunes has to open for it to check and return false.

Please Help.

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

Upvotes: 0

Views: 148

Answers (1)

Wevah
Wevah

Reputation: 28242

You might have better luck with something like:

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

Upvotes: 2

Related Questions