Re: Adding files to iTune library in Cocoa app
Re: Adding files to iTune library in Cocoa app
- Subject: Re: Adding files to iTune library in Cocoa app
- From: has <email@hidden>
- Date: Tue, 26 Jun 2007 10:31:07 +0100
John Stiles wrote:
On Jun 25, 2007, at 12:26 PM, Gregory Weston wrote:
tell application "iTunes" to add (POSIX file "thePath") to (first
library playlist)
Wrap that in an NSAppleScript, with thePath replaced by the full
path of the track you want to add.
Be carefulthis will not work if the path contains any non-MacRoman
characters. (Which means that in many countries it will never work.)
The fix involves replacing "thePath" with a goofy hack/workaround
that looks like this:
(«data utf84142434445464748» as Unicode text)
You plug in hexadecimal numbers corresponding to the path string, in
UTF8, after the 'utf8' bit.
I wish there were a simpler way
Code generation is evil; AppleScript code generation doubly so.
If you want to parameterise an AppleScript, the only safe way is to
pass your data in via NSAppleScript's -executeEvent:error: method.
Here's some sample code to demonstrate:
/*********************************************************************/
#import <Foundation/Foundation.h>
#import "Appscript/Appscript.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Create AEMApplication object for building AEMEvents
AEMApplication *app = [[AEMApplication alloc] init];
// Create AEMCodecs object for unpacking script results
AEMCodecs *codecs = [[AEMCodecs alloc] init];
// Build parameter list for script
NSArray *params = [NSArray arrayWithObjects: @"foo", @"bar", nil];
// Create 'run' AEMEvent
AEMEvent *evt = [app eventWithEventClass:'aevt' eventID:'oapp'];
// Add parameter list to AEMEvent
[evt setParameter:params forKeyword:'----'];
// Get an NSAppleEventDescriptor from AEMEvent
NSAppleEventDescriptor *evtDesc = [evt appleEventDescriptor];
// Compile (or load) AppleScript
NSAppleScript *scpt = [[NSAppleScript alloc] initWithSource:
@"on run {arg1, arg2}\n return arg1 & arg2\n end"];
// Send event to AppleScript
NSDictionary *error;
NSAppleEventDescriptor *resDesc = [scpt executeAppleEvent:evtDesc
error:&error];
// Unpack script result
id res = [codecs unpack:resDesc];
NSLog(@"Result = %@", res); // Result = foobar
[scpt release];
[codecs release];
[app release];
[pool release];
return 0;
}
/*********************************************************************/
Notes:
- The AppleScript contains a 'run' handler that takes a list of
values as its parameter. I've hardwired the AppleScript for
simplicity here, but you can easily adapt the code to load it in from
file, retain it between invocations, etc.
- I've used aem (part of objc-appscript) to pack the ingoing event
and unpack the script's return value as it's simpler than mucking
about with NSAppleEventDescriptors yourself, but you can do
everything yourself if you prefer.
- I've omitted the code for handling script errors for sake of
simplicity, but this is easy enough to add yourself if you need it.
This sort of approach is most useful when you want to implement
attachability in your application - i.e. the ability for users to
attach their own scripts to your application which will then invoke
those scripts when something interesting happens (e.g. folder
actions, Mail rules). OTOH, if you just want to send events to
another application, it'll be simpler just to use aem or appscript to
talk to them directly - see my other post to this thread for an example.
HTH
has
--
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org
http://appscript.sourceforge.net/objc-appscript.html
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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