Skip to content

Commit

Permalink
Works with plain text string
Browse files Browse the repository at this point in the history
  • Loading branch information
74hc595 committed Jan 7, 2012
1 parent ce6329f commit fd48d1e
Show file tree
Hide file tree
Showing 8 changed files with 792 additions and 781 deletions.
18 changes: 15 additions & 3 deletions Currentsong.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
C9E4B3A514B8C440006D7B1F /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = C9E4B3A314B8C440006D7B1F /* Credits.rtf */; };
C9E4B3A814B8C440006D7B1F /* CurrentsongAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C9E4B3A714B8C440006D7B1F /* CurrentsongAppDelegate.m */; };
C9E4B3AB14B8C440006D7B1F /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9E4B3A914B8C440006D7B1F /* MainMenu.xib */; };
C9E4B3B914B8D469006D7B1F /* ScriptingBridge.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C9E4B3B814B8D469006D7B1F /* ScriptingBridge.framework */; };
C9E4B3BC14B8DAE3006D7B1F /* CurrentsongStatusView.m in Sources */ = {isa = PBXBuildFile; fileRef = C9E4B3BB14B8DAE3006D7B1F /* CurrentsongStatusView.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -21,21 +23,26 @@
C9E4B39714B8C440006D7B1F /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
C9E4B39814B8C440006D7B1F /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; };
C9E4B39914B8C440006D7B1F /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
C9E4B39C14B8C440006D7B1F /* Currentsong-Info.plist */ = {isa = PBXFileReference; path = "Currentsong-Info.plist"; sourceTree = "<group>"; };
C9E4B39C14B8C440006D7B1F /* Currentsong-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Currentsong-Info.plist"; sourceTree = "<group>"; };
C9E4B39E14B8C440006D7B1F /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
C9E4B3A014B8C440006D7B1F /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
C9E4B3A214B8C440006D7B1F /* Currentsong-Prefix.pch */ = {isa = PBXFileReference; path = "Currentsong-Prefix.pch"; sourceTree = "<group>"; };
C9E4B3A214B8C440006D7B1F /* Currentsong-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Currentsong-Prefix.pch"; sourceTree = "<group>"; };
C9E4B3A414B8C440006D7B1F /* en */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; name = en; path = en.lproj/Credits.rtf; sourceTree = "<group>"; };
C9E4B3A614B8C440006D7B1F /* CurrentsongAppDelegate.h */ = {isa = PBXFileReference; path = CurrentsongAppDelegate.h; sourceTree = "<group>"; };
C9E4B3A614B8C440006D7B1F /* CurrentsongAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CurrentsongAppDelegate.h; sourceTree = "<group>"; };
C9E4B3A714B8C440006D7B1F /* CurrentsongAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CurrentsongAppDelegate.m; sourceTree = "<group>"; };
C9E4B3AA14B8C440006D7B1F /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/MainMenu.xib; sourceTree = "<group>"; };
C9E4B3B714B8D42E006D7B1F /* iTunes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = iTunes.h; sourceTree = "<group>"; };
C9E4B3B814B8D469006D7B1F /* ScriptingBridge.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ScriptingBridge.framework; path = System/Library/Frameworks/ScriptingBridge.framework; sourceTree = SDKROOT; };
C9E4B3BA14B8DAE3006D7B1F /* CurrentsongStatusView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CurrentsongStatusView.h; sourceTree = "<group>"; };
C9E4B3BB14B8DAE3006D7B1F /* CurrentsongStatusView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CurrentsongStatusView.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
C9E4B38D14B8C440006D7B1F /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
C9E4B3B914B8D469006D7B1F /* ScriptingBridge.framework in Frameworks */,
C9E4B39514B8C440006D7B1F /* Cocoa.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -64,6 +71,7 @@
isa = PBXGroup;
children = (
C9E4B39414B8C440006D7B1F /* Cocoa.framework */,
C9E4B3B814B8D469006D7B1F /* ScriptingBridge.framework */,
C9E4B39614B8C440006D7B1F /* Other Frameworks */,
);
name = Frameworks;
Expand All @@ -82,8 +90,11 @@
C9E4B39A14B8C440006D7B1F /* Currentsong */ = {
isa = PBXGroup;
children = (
C9E4B3B714B8D42E006D7B1F /* iTunes.h */,
C9E4B3A614B8C440006D7B1F /* CurrentsongAppDelegate.h */,
C9E4B3A714B8C440006D7B1F /* CurrentsongAppDelegate.m */,
C9E4B3BA14B8DAE3006D7B1F /* CurrentsongStatusView.h */,
C9E4B3BB14B8DAE3006D7B1F /* CurrentsongStatusView.m */,
C9E4B3A914B8C440006D7B1F /* MainMenu.xib */,
C9E4B39B14B8C440006D7B1F /* Supporting Files */,
);
Expand Down Expand Up @@ -167,6 +178,7 @@
files = (
C9E4B3A114B8C440006D7B1F /* main.m in Sources */,
C9E4B3A814B8C440006D7B1F /* CurrentsongAppDelegate.m in Sources */,
C9E4B3BC14B8DAE3006D7B1F /* CurrentsongStatusView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 2 additions & 0 deletions Currentsong/Currentsong-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<string>Copyright © 2012 Matt Sarnoff. All rights reserved.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>LSUIElement</key>
<true/>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
Expand Down
8 changes: 4 additions & 4 deletions Currentsong/CurrentsongAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#import <Cocoa/Cocoa.h>

@interface CurrentsongAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
@interface CurrentsongAppDelegate : NSObject <NSApplicationDelegate>
{
NSStatusItem *mStatusItem;
IBOutlet NSMenu *mMenu;
}

@property (assign) IBOutlet NSWindow *window;

@end
119 changes: 117 additions & 2 deletions Currentsong/CurrentsongAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,129 @@
//

#import "CurrentsongAppDelegate.h"
#import "iTunes.h"

@interface CurrentsongAppDelegate ()
- (NSDictionary *)fetchTrackInfo;
- (void)updateTrackInfo:(NSDictionary *)trackInfo;
@end

@implementation CurrentsongAppDelegate

@synthesize window;
@implementation CurrentsongAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}

// Artist - Name - Album
// Stream Title - Name

- (void)awakeFromNib
{
// Install status item
mStatusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[mStatusItem setHighlightMode:YES];

// Get initial track info
NSDictionary *initialTrackInfo = [self fetchTrackInfo];
[self updateTrackInfo:initialTrackInfo];

// Start listening to iTunes notifications
NSDistributedNotificationCenter *dnc = [NSDistributedNotificationCenter defaultCenter];
[dnc addObserver:self selector:@selector(trackInfoDidChange:) name:@"com.apple.iTunes.playerInfo" object:nil];
}

- (void)dealloc
{
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
[mStatusItem release];
[super dealloc];
}

// Handle play state and track changes
- (void)trackInfoDidChange:(NSNotification *)notification
{
[self updateTrackInfo:[notification userInfo]];
}

// Update track info
- (void)updateTrackInfo:(NSDictionary *)trackInfo
{
// If trackInfo is nil, interpret this as iTunes not running or the player stopped
NSString *playerState = [trackInfo objectForKey:@"Player State"];

if (!trackInfo || !playerState || [playerState isEqualToString:@"Stopped"]) {
[mStatusItem setTitle:@"\u266B"]; // musical note icon
return;
}

NSString *artist = [trackInfo objectForKey:@"Artist"];
NSString *name = [trackInfo objectForKey:@"Name"];
NSString *album = [trackInfo objectForKey:@"Album"];
NSString *streamTitle = [trackInfo objectForKey:@"Stream Title"];

// Streaming?
if (streamTitle) {
album = name;
name = streamTitle;
}

NSLog(@"Artist: %@", artist);
NSLog(@"Name: %@", name);
NSLog(@"Album: %@", album);

BOOL haveArtist = ([artist length] > 0);
BOOL haveName = ([name length] > 0);
BOOL haveAlbum = ([album length] > 0);

NSMutableArray *fields = [NSMutableArray arrayWithCapacity:3];
if (haveArtist) [fields addObject:artist];
if (haveName) [fields addObject:name];
if (haveAlbum) [fields addObject:album];

NSString *pausedIcon = ([playerState isEqualToString:@"Paused"] ? @"\u275A\u275A " : @"");

NSString *fullStatus = [NSString stringWithFormat:@"%@%@",
pausedIcon,
[fields componentsJoinedByString:@" - "]];
[mStatusItem setTitle:fullStatus];
}

// Fetch track info directly using Scripting Bridge
// Fetches play state, artist, name, album, and stream title.
- (NSDictionary *)fetchTrackInfo
{
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];

// Application not found or not running
if (!iTunes || ![iTunes isRunning]) {
NSLog(@"itunes not running");
return nil;
}

// Is iTunes stopped or paused?
NSString *playerStateString;
switch (iTunes.playerState)
{
case iTunesEPlSStopped: playerStateString = @"Stopped"; break;
case iTunesEPlSPaused: playerStateString = @"Paused"; break;
default: playerStateString = @""; break;
}

iTunesTrack *currentTrack = iTunes.currentTrack;
NSString *artist = [currentTrack artist];
NSString *name = [currentTrack name];
NSString *album = [currentTrack album];
NSString *streamTitle = iTunes.currentStreamTitle;

NSMutableDictionary *info = [NSMutableDictionary dictionaryWithCapacity:5];
if (playerStateString) [info setObject:playerStateString forKey:@"Player State"];
if (artist) [info setObject:artist forKey:@"Artist"];
if (name) [info setObject:name forKey:@"Name"];
if (album) [info setObject:album forKey:@"Album"];
if (streamTitle) [info setObject:streamTitle forKey:@"Stream Title"];
return info;
}

@end
46 changes: 46 additions & 0 deletions Currentsong/CurrentsongStatusView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// CurrentsongStatusView.h
// Currentsong
//
// Created by Matthew Sarnoff on 1/7/12.
// Copyright 2012 Matt Sarnoff. All rights reserved.
//

#import <Cocoa/Cocoa.h>

typedef enum
{
kCSStylePlain,
kCSStylePlainSmall,
kCSStyleFormatted,
kCSStyleFormattedSmall,
kCSStyleTwoLevel
} CurrentsongViewStyle;

@interface CurrentsongStatusView : NSView
{
NSStatusItem *mStatusItem;

// Properties
CurrentsongViewStyle mViewStyle;
BOOL mScroll;

// Track data
BOOL isRunningAndNotStopped;
BOOL isPaused;
NSString *mArtist;
NSString *mName;
NSString *mAlbum;

NSAttributedString *topRow;
NSAttributedString *bottomRow;
}

@property (nonatomic,retain) NSStatusItem *statusItem;
@property (nonatomic,assign) CurrentsongViewStyle viewStyle;
@property (nonatomic,assign) BOOL scroll;

// Update track info from dictionary provided by iTunes distributed notification
- (void)updateTrackInfo:(NSDictionary *)trackInfo;

@end
31 changes: 31 additions & 0 deletions Currentsong/CurrentsongStatusView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// CurrentsongStatusView.m
// Currentsong
//
// Created by Matthew Sarnoff on 1/7/12.
// Copyright 2012 Matt Sarnoff. All rights reserved.
//

#import "CurrentsongStatusView.h"

@implementation CurrentsongStatusView

@synthesize statusItem = mStatusItem;
@synthesize viewStyle = mViewStyle;
@synthesize scroll = mScroll;

- (void)dealloc
{
[mStatusItem release];
[super dealloc];
}

- (void)drawRect:(NSRect)dirtyRect
{
NSLog(@"%@", NSStringFromRect([self bounds]));

[[NSColor redColor] set];
NSRectFill([self bounds]);
}

@end
Loading

0 comments on commit fd48d1e

Please sign in to comment.