thislooksfun

AppDelegate.m

Jul 17th, 2014
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  AppDelegate.m
  3. //  NowPlaying
  4. //
  5. //  Created by thislooksfun on 7/14/14.
  6. //  Copyright (c) 2014 thislooksfun. All rights reserved.
  7. //
  8.  
  9. #import "AppDelegate.h"
  10. #import "iTunes.h"
  11.  
  12. @implementation AppDelegate
  13.  
  14. @synthesize statusBar = _statusBar;
  15. @synthesize scrollingText;
  16. @synthesize currentData;
  17.  
  18.  
  19. - (id) init
  20. {
  21.     self = [super init];
  22.    
  23.     if (!self) return nil;
  24.     NSDistributedNotificationCenter *dnc = [NSDistributedNotificationCenter defaultCenter];
  25.     [dnc addObserver:self selector:@selector(onItunesChange:) name:@"com.apple.iTunes.playerInfo" object:nil];
  26.    
  27.     self.iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
  28.    
  29.     return self;
  30. }
  31.  
  32. - (void) awakeFromNib
  33. {
  34.     self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
  35.    
  36.     self.statusBar.menu = self.statusMenu;
  37.     self.statusBar.highlightMode = YES;
  38.    
  39.     [self.scrollingText setText:@"Hello world!"];
  40.     [self.scrollingText setSpeed:0.03];
  41.     self.statusBar.view = self.scrollingText; //Here
  42.     [self onItunesChange:nil];
  43. }
  44.  
  45. - (void) onItunesChange:(NSNotification *)notification
  46. {
  47.     iTunesEPlS state = [self.iTunes playerState];
  48.     switch (state) {
  49.         case iTunesEPlSStopped: {
  50.             currentData = nil;
  51.             //[self.scrollingText setText:@"No song playing"];
  52.             //[self.scrollingText setSpeed:0.03];
  53.             self.statusBar.view = self.scrollingText;
  54.         } break;
  55.         default: {
  56.             [self setSong];
  57.         } break;
  58.     }
  59. }
  60.  
  61. - (void) setSong
  62. {
  63.     NSString *str = @"";
  64.    
  65.     iTunesTrack *track = [self.iTunes currentTrack];
  66.    
  67.     if ([track podcast]) {
  68.         str = [str stringByAppendingString:@"[Podcast] "];
  69.     } else if ([[track kind] isEqualToString:@"Internet audio stream"]) {
  70.         str = [str stringByAppendingString:@"[Radio] "];
  71.     } else {
  72.         str = [str stringByAppendingString:@"[Song] "];
  73.     }
  74.    
  75.     NSString *title = [self.iTunes currentStreamTitle];
  76.     NSString *trName = [track name];
  77.     NSString *trArtist = [track artist];
  78.     NSString *trAlbum = [track album];
  79.    
  80.     if (title != nil) {
  81.         str = [str stringByAppendingString:title];
  82.     }
  83.     if (trName != nil && ![trName isEqualToString:@""]) {
  84.         if ([str length] > 0) {
  85.             str = [str stringByAppendingString:@" "];
  86.         }
  87.         str = [str stringByAppendingString:trName];
  88.     }
  89.     if (trArtist != nil && ![trArtist isEqualToString:@""]) {
  90.         if ([str length] > 0) {
  91.             str = [str stringByAppendingString:@" — "];
  92.         }
  93.         str = [str stringByAppendingString:trArtist];
  94.     }
  95.     if (trAlbum != nil && ![trAlbum isEqualToString:@""]) {
  96.         if ([str length] > 0) {
  97.             str = [str stringByAppendingString:@" — "];
  98.         }
  99.         str = [str stringByAppendingString:trAlbum];
  100.     }
  101.    
  102.     return;
  103.     if (![str isEqualToString:currentData]) {
  104.         [self.scrollingText setText:str];
  105.         [self.scrollingText setSpeed:0.03];
  106.         self.statusBar.view = self.scrollingText;
  107.         currentData = str;
  108.     }
  109. }
  110.  
  111. @end
Advertisement
Add Comment
Please, Sign In to add comment