Control iTunes (including restart)

Send Snippet To: Save this snippet to Code Collector Pro -- view all powidl's snippets
language: Objective-C
license: Other

Code for Snippet:

                
   // iTunes integration
   tmp = nil;
   if ((tmp = [[NSBundle mainBundle] pathForResource:@"iTunesNotifier" ofType:@"bundle"]) &&
        [ud boolForKey:@"SyncChangeWithiTunes"]) {
       NSString *itunesLib = [@"~/Library/iTunes/iTunes Plug-ins" stringByExpandingTildeInPath];
       BOOL isDir = NO, itunesGood = NO;
 
       if ([fm fileExistsAtPath:itunesLib isDirectory:&isDir] && isDir) {
          itunesLib = [itunesLib stringByAppendingPathComponent:[tmp lastPathComponent]];
          if ([fm fileExistsAtPath:itunesLib isDirectory:&isDir]) {
             NSDate *cur, *exist;
             cur = [[fm fileAttributesAtPath:tmp traverseLink:NO]
                objectForKey:NSFileModificationDate];
             exist = [[fm fileAttributesAtPath:itunesLib traverseLink:NO]
                objectForKey:NSFileModificationDate];
             if (isDir && [cur isEqualToDate:exist])
                itunesGood = YES;
             else {
                if (NO == [fm removeFileAtPath:itunesLib handler:nil])
                    ELOG(@"%s:%d Failed to remove old iTunes support.");
             }
          }
 
          if (NO == itunesGood) {
            NSWorkspace *ws = [NSWorkspace sharedWorkspace];
            int itunesPID, ret;
            // Copy bndl -- iTunes doesn't respect sym links or aliases to plug-ins (no cookie!)
            itunesGood = [fm copyPath:tmp toPath:itunesLib handler:nil];
            // Ask user to restart ?
            if (itunesGood && (itunesPID = FindPIDByPathOrName(nil, "iTunes.app", getuid()))) {
                [NSApp activateIgnoringOtherApps:YES];
                NSBeep();
                ret = NSRunInformationalAlertPanel (CDDString(@"Restart iTunes?"),
                    CDDString(@"ChangeDesktop needs to restart iTunes."),
                    @"OK", CDDString(@"Cancel"), nil);
                if (NSAlertDefaultReturn == ret) {
                   // Kill itunes and restart it
                   if (0 == kill(itunesPID, SIGTERM)) {
                      sleep(1); // Give it time to exit
                      if (NO == [ws launchApplication:[ws fullPathForApplication:@"iTunes.app"]])
                         ELOG(@"%s:%d Finder failed to launch iTunes.");
                   } else {
                      ELOG1(@"%s:%d Failed to quit iTunes. Error: %d.", errno);
                   }
                }
            } else if (NO == itunesGood) {
                ELOG(@"%s:%d Failed to copy iTunes support to plug-in folder.");
            }
          }
       }
 
       if (itunesGood) {
           // iTunesNotifier events
           [[NSDistributedNotificationCenter defaultCenter] addObserver:self
              selector:@selector(iTunesTrackChange:) name:@"itunes.play.trackchange"
              object:nil
              suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
           [[NSDistributedNotificationCenter defaultCenter] addObserver:self
              selector:@selector(iTunesTrackChange:) name:@"itunes.play.start"
              object:nil
              suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
       } else {
         goto remove_itunes_observer;
       }
   } else {
       if (nil == tmp)
          ELOG(@"%s:%d iTunes plug-in path does not exist.");
remove_itunes_observer:
       [[NSDistributedNotificationCenter defaultCenter] removeObserver:self
          name:@"itunes.play.trackchange" object:nil];
       [[NSDistributedNotificationCenter defaultCenter] removeObserver:self
          name:@"itunes.play.start" object:nil];
   }
 
comments powered by Disqus

Info

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 936
Date Added: 2009-03-02 11:15:43
Last Modified: 0000-00-00 00:00:00

Web Analytics