Sun 5th Jun 2005
Notification from AppleScript Studio
Posted early evening, filed under AppleScript.Christoph asked me for some advice on how to get notification on iTunes track changes in a comment, I said I would write a bit of a tutorial on how I got it to work.
First thing was to create an AppleScript Studio program - you will need to have the Developer Tools installed for this.
Now, open the main.m file. It should look something like the following:
#import <mach-o/dyld.h> extern int NSApplicationMain(int argc, const char *argv[]); int main(int argc, const char *argv[]) { if (NSIsSymbolNameDefined("_ASKInitialize")) { NSSymbol *symbol = NSLookupAndBindSymbol("_ASKInitialize"); if (symbol) { void (*initializeASKFunc)(void) = NSAddressOfSymbol(symbol); if (initializeASKFunc) { initializeASKFunc(); } } } return NSApplicationMain(argc, argv); }
The file needs to have the following elements.
At the start, the following imports are required:
#import <Foundation/NSDistributedNotificationCenter.h>
#import <Foundation/NSString.h>
#import <AppKit/NSAppleScriptExtensions.h>
This should be all of the imports that are required - although I also had stdio.h for a while for testing.
After the declaration of the NSApplicationMain, I put the following in:
@interface iTunesConnection : NSObject - (id) init; @end @implementation iTunesConnection - (id) init { NSDistributedNotificationCenter *nc = [NSDistributedNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(updateNow:) name:@"com.apple.iTunes.playerInfo" object:nil]; return self; } - (void) updateNow:(NSNotification *)notification { NSString *updateScript = [NSString stringWithFormat:@"tell application \nend tell"]; NSAppleScript *as = [[[NSAppleScript alloc] initWithSource:updateScript] autorelease]; [as executeAndReturnError:nil]; } @end
Copy and paste the stuff above - there is some that goes over the line in my browser, but it’s all there if you drag out the selection.
You may need to change a couple of things depending on what you want to do. For instance, the @"tell application \nend tell" section needs to be the applescript code that is run whenever a new iTunes track is started, or iTunes is paused. You could fit the entire application into this place, but what I did was have a hidden button that this script “presses”, triggering the update process.
Finally, I added the following declaration just after the first brace in the main function declaration:
iTunesConnection *connection; connection = [[iTunesConnection alloc] init];
This gets everything running.
I would suggest putting the following into the applescript string to test it and see that it works:
@"tell application \"Finder\"\ndisplay dialog \"iTunes Changed\"\nend tell"
You need to have \" whenever your script would have a ", and \n wherever it would have a newline.
A hughe thank you! I’ll try it tomorrow morning - but I’m confidently sure that this will work. I’ve used the AppleScriptStudio some times - however there is no document (even from apple) that describes how to work with these notifications.
Thank you!
1 day, 14 hours after the fact.
Actually, there is some documentation, but it does not deal with AppleScript, and is not all that detailed. I learned most of what to do from the Growl project. And figured out how to do the AppleScript stuff myself.
I’m fairly sure I’ve put the source for iTunesRater up somewhere - I’ll put the latest version up this week sometime.
1 day, 15 hours after the fact.
It works…thank you!
3 days after the fact.