• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: notifications
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: notifications


  • Subject: Re: notifications
  • From: Julio Cesar Silva dos Santos <email@hidden>
  • Date: Mon, 21 May 2007 13:36:48 -0300

You don't need to use an AppleScript to get the current song's name. Just use notification object of updateNow:

[currentTrackField setStringValue:[[notification userInfo] objectForKey:@"name"]];

Of course you have to check if the userInfo dictionary is available.


Julio Cesar Silva dos Santos email@hidden Skype: jcssantos01 Blogjective-C http://www.jcs.santos.nom.br/wp FetchYourLyrics http://www.jcs.santos.nom.br/fyl/fyl.html


On May 21, 2007, at 11:24 AM, julien ricard wrote:

hi,

I'm doing a simple program with a window containing the name of the
currently played track in itunes. I can do it with some "update"
button" but I'd like the textfield in my window to be updated
automatically when the track changes. I could also have the
notification function in the controller class, but I'd like to keep it
as a separate module.

My controller class shown below initialize an object iTunesConnection
(iTunesConnection.h is also shown below), which has a method updateNow
to update its member currentSong.

What's the best way to get the notification from the iTunesConnection
object within the controller class?

thanks
julien

************************************************************
/* miPluginController.h */

#import <Cocoa/Cocoa.h>
#import "iTunesConnection.h"

@interface miPluginController : NSObject
{
   IBOutlet NSTextField *currentTrackField;
}

@end

@implementation miPluginController

- (void)awakeFromNib
{
[currentTrackField setStringValue:@"None"];
iTunesConnection *connection;
connection = [[iTunesConnection alloc] init];
}

@end
************************************************************

************************************************************
//
//  iTunesConnection.h
//  MI_Plugin
//
//  Created by jul on 5/21/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <mach-o/dyld.h>
#import <Foundation/NSDistributedNotificationCenter.h>
#import <Foundation/NSString.h>
#import <AppKit/NSAppleScriptExtensions.h>


@interface iTunesConnection : NSObject { NSString *currentTrack; } - (id) init; - (void) updateNow:(NSNotification *)notification; - (void) updateCurrentTrackName; - (NSString *) getcurrentTrack; - (void) setCurrentTrack:(NSString *) curTrack;

@end

@implementation iTunesConnection

- (id) init {
   [self setCurrentTrack: @"None"];
   NSDistributedNotificationCenter *nc =
[NSDistributedNotificationCenter defaultCenter];
   [nc addObserver:self
          selector:@selector(updateNow:)
              name:@"com.apple.iTunes.playerInfo"
            object:nil];
   return self;
}

- (void) updateNow:(NSNotification *)notification {
   [self updateCurrentTrackName];
   //NSLog([self getCurrentTrack]);
}

-(void) updateCurrentTrackName {

   NSString *updateScript = [NSString stringWithFormat:@"tell
application \"iTunes\"\nname of current track\nend tell"];
   NSAppleScript *iChatGetStatusScript =
       [[NSAppleScript alloc] initWithSource: updateScript];
   NSAppleEventDescriptor *resultDescriptor =
       [iChatGetStatusScript executeAndReturnError:Nil];
   NSData *rawData = [resultDescriptor data];
   [self setCurrentTrack:[NSString stringWithCharacters:[rawData
bytes] length: [rawData length] / sizeof(unichar)]];

}

-(NSString *) getCurrentTrack {
   return currentTrack;
}

- (void) setCurrentTrack:(NSString *) curTrack {
   currentTrack=curTrack;
}

@end
************************************************************
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
40gmail.com


This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >notifications (From: "julien ricard" <email@hidden>)

  • Prev by Date: Re: OSAKit, specifically OSAScriptView
  • Next by Date: Re: Encoding infinite loops
  • Previous by thread: Re: notifications
  • Next by thread: OSAKit, specifically OSAScriptView
  • Index(es):
    • Date
    • Thread