Maxwell Dergosits
Maxwell Dergosits

Reputation: 307

NSDistributedNotificationCenter and iTunes

is there a way to send notifications to iTunes so it will tell me the current song. i know it will send a notification when the song changes but i want to get the current song when my application opens up. I am trying to avoid using the scripting bridge.

Upvotes: 0

Views: 382

Answers (2)

jackjr300
jackjr300

Reputation: 7191

You can use NSAppleScript to run a AppleScript.

Example: to get the title and the artist of the current track

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

Example: to use a AppleScript file in the "Resource folder"

NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"theScriptName" ofType:@"scpt"];
NSAppleScript *theScript = [[NSAppleScript alloc] initWithContentsOfURL: [NSURL fileURLWithPath:scriptPath] error: nil];

Another solution : You can use NSTask to run a AppleScript via osascript

Upvotes: 1

Rob Keniger
Rob Keniger

Reputation: 46020

Unfortunately you can't do this with notifications, your only option is the Scripting Bridge or other Apple Event-related methods.

Upvotes: 2

Related Questions