Notification from AppleScript Studio

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:

 1     #import <mach-o/dyld.h>
 2         
 3     extern int _NSApplicationMain_(int argc, const char *argv[]);
 4         
 5     int main(int argc, const char *argv[])
 6     {
 7         if (NSIsSymbolNameDefined("_ASKInitialize"))
 8         {
 9         NSSymbol *symbol = NSLookupAndBindSymbol("_ASKInitialize");
10         if (symbol)
11         {
12         void (*initializeASKFunc)(void) = NSAddressOfSymbol(symbol);
13             if (initializeASKFunc)
14         {
15         initializeASKFunc();
16         }
17         }
18         }
19         
20     return _NSApplicationMain_(argc, argv);
21     }

The file needs to have the following elements. At the start, the following imports are required:

1     #import <Foundation/NSDistributedNotificationCenter.h>
2     #import <Foundation/NSString.h>
3     #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:

 1     @interface iTunesConnection : NSObject
 2      - (id) init;
 3     @end
 4     
 5     @implementation iTunesConnection
 6      - (id) init {
 7         NSDistributedNotificationCenter *nc = [NSDistributedNotificationCenter defaultCenter];
 8         [nc addObserver:self
 9                selector:@selector(updateNow:)
10                    name:@"com.apple.iTunes.playerInfo"
11                  object:nil];
12     
13         return self;
14     }
15     
16      - (void) updateNow:(NSNotification *)notification {
17         NSString *updateScript = [NSString stringWithFormat:@"tell application \nend tell"];
18         NSAppleScript *as = [[[NSAppleScript alloc] initWithSource:updateScript] autorelease];
19         [as executeAndReturnError:nil];
20     }    
21     @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:

1     iTunesConnection *connection;
2     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:

1     @"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.

blog comments powered by Disqus