|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 | |
|
How do I use objc-appscript to create a new playlist in iTunes?
I am writing an app in Xcode and need to create a new playlist in iTUnes, and add specific tracks to it.
Here is the code I have so far: Code:
-(ITunesError *) savePlaylist:(Playlist *) playlist {
ITunesError * rv = [[ITunesError new]retain];
//Get name of playlist we want to create
NSString * name = [playlist name];
ITReference *playlistRef = [[itunes playlists] byName:name];
/* Find a unique name for playlist we are creating */
if([[[playlistRef exists]send] boolValue]){
int i = 1;
NSString * n = @"";
do{
n = [NSString stringWithFormat:@"%@_%d", name, i];
playlistRef = [[itunes playlists] byName:n];
i++;
}while([[[playlistRef exists]send] boolValue]);
name = n;
}
//Create new playlist in itunes
[[[[itunes make] new_: [ITConstant playlist]] withProperties: [NSDictionary dictionaryWithObject: name forKey: [ITConstant name]]] send];
//Returns the main music library for iTunes
ITReference *main = [self getRefToMainPlaylist];
ITReference * tracks = [main tracks];
//NSArray * trackNames = [[tracks name] getItem];
//NSArray * artistNames = [[tracks artist] getItem];
NSArray * locations = [[tracks location ] getItem];
NSMutableArray *files = [NSMutableArray array];
for(int j = 0; j < 10; j++){
ASAlias * alias = (ASAlias *) [locations objectAtIndex:j];
NSLog([alias path]);
NSURL *file = [NSURL fileURLWithPath: [alias path]];
[files addObject: file];
}
/* At this point here is what we have:
playlistRef - Reference to playlist we just created in itunes
files - Array of NSURL objects pointing to files on disk.
*/
NSError * error = 0;
[[[itunes add: files] to: playlistRef] sendWithError:&error];
if(error){
[rv setErrorObject:error];
NSLog([rv getErrorMessage]);
}
return nil;
}
When I run the code above the error message contained in NSError is: Quote:
Any help here would be greatly appreciated. |
||
|
|
0
|
|
|
#2 | |
|
Quote:
1. Take care when casting values returned by application commands, in this case, the (ASAlias*) cast. Some application commands may return different types at different times, e.g. getting a track's location property will return an alias if the file exists, or a 'missing value' constant if it doesn't. If you want to specify the return type, do it when you're constructing the application command, e.g. in this case use [[tracks location] getListOfType: typeAlias error:&error], which will return a coercion error if it encounters a missing track. Alternatively, use -[NSObject isKindOfClass:] to check the returned value's class before using it. 2. You shouldn't need to re-pack ASAliases as NSURLs if you're only passing them back to iTunes; iTunes should accept either. OTOH, using ASAliases in other Cocoa APIs is a pain as they're not API-compatible with NSURLs (I need to work on this), but you can at least get an NSURL directly via -[ASAlias url], or else you can tell appscript to coerce the values returned by the application command to the desired type for you (in this case typeFileURL). 3. If you're importing files that aren't already in iTunes, use its 'add' command. In this case though you're wanting to add tracks that already exist in your library playlist to a user playlist, and for that you should use iTunes' 'duplicate' command. Don't have sample code to hand (ask if you need some), but it should be pretty straightforward. iTunes' 'duplicate' command appears to accept either a single-track reference or a multi-track reference (most apps do), though not a list of references (most apps don't), e.g. these work: Code:
tell application "iTunes" duplicate (track 1 of library playlist 1) to playlist "test" end tell tell application "iTunes" duplicate (tracks 1 thru 5 of library playlist 1) to playlist "test" end tell Code:
tell application "iTunes" set tracksList to (get tracks 1 thru 5 of library playlist 1) duplicate tracksList to playlist "test" end tell Tips: - You can use ASTranslate (available from the appscript website) to convert the above iTunes commands from AppleScript to ObjC syntax, which is handy if you've got an AppleScript-based example and need some help with translating it into ObjC. - Doug's iTunes (http://dougscripts.com/) has tons of iTunes-related AppleScripts, many of which can be viewed in Script Editor; also very handy when you're trying to work out how to perform a particular task. As you've probably noticed, iTunes' own scripting documentation (like that of most apps) is very thin and doesn't tell you half the stuff you actually need to know in order to script it effectively - so other folks' scripts can be a valuable source of hints and solutions.
__________________
Learn AppleScript, 3rd edition, Sanderson & Rosenthal: http://apress.com/book/view/9781430223610 Control AppleScriptable applications from Python, Ruby and ObjC: http://appscript.sourceforge.net |
||
|
|
0
|
|
|
#3 | |
|
Can't create new Playlist
Thank you for mentioning ASTranslate, wish I would have found out about it when I started this app instead of when I was almost done.
Using it, I have figured out how to add existing tracks to an existing playlist, but I am still having problems creating a new playlist. So i've created this method to simply create a playlist. I got the code from ASTranslate and the only modification I made was to sendWithError. Applescript Code Code:
tell application "iTunes"
make new user playlist with properties {name:"DUH"}
end tell
Code:
-(NSError *) createPlaylist:(NSString *) playlist{
ITMakeCommand *cmd = [[[itunes make] new_: [ITConstant userPlaylist]] withProperties: [NSDictionary dictionaryWithObject: @"Playlist" forKey: [ITConstant name]]];
NSError * rv = 0;
[cmd sendWithError:&rv];
return rv;
}
Quote:
|
||
|
|
0
|
|
|
#4 | |
|
Quote:
Code:
#import "ITGlue/ITGlue.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ITApplication *itunes = [ITApplication applicationWithName: @"iTunes"];
ITMakeCommand *cmd = [[[itunes make] new_: [ITConstant userPlaylist]]
withProperties: [NSDictionary dictionaryWithObject: @"DUH"
forKey: [ITConstant name]]];
NSError *error;
id result = [cmd sendWithError:&error];
NSLog(@"Result = %@", result);
NSLog(@"Error = %@", error);
[pool drain];
return 0;
}
Code:
2008-11-04 18:23:29.561 itunes-newplaylist[23202:10b] Result = [[[[[ITApplication applicationWithName: @"/Applications/iTunes.app"] sources] byID: [NSNumber numberWithInt: 41]] userPlaylists] byID: [NSNumber numberWithInt: 56496]] 2008-11-04 18:23:29.563 itunes-newplaylist[23202:10b] Error = (null) A couple things to try: - Build the Appscript framework using the latest objc-appscript trunk, regenerate your glue using ASDictionary 0.11.0 and do a clean build of your project to check there isn't a version mismatch between them. - Run iTunes with AEDebug enabled and email me the trace for 'make' event sent by AppleScript and the trace for the 'make' event sent by appscript. Also include your OS version, iTunes version and hardware type.
__________________
Learn AppleScript, 3rd edition, Sanderson & Rosenthal: http://apress.com/book/view/9781430223610 Control AppleScriptable applications from Python, Ruby and ObjC: http://appscript.sourceforge.net |
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| thread | Thread Starter | Forum | Replies | Last Post |
| How do I create a smart playlist in iTunes that adds full albums? | SGMD1 | Mac Applications and Mac App Store | 0 | Oct 17, 2011 09:14 AM |
| How long should it take to create a new 80GB partition in my free space? | oregon2 | Mac OS X | 4 | Jul 18, 2009 01:30 AM |
| How to create a compilation album in iTunes to be used on an ipod Touch??? | doihaveto | iPod | 3 | Sep 8, 2008 10:55 PM |
| how do I use iPodRip to take songs from iPod to new Mac? | darbswerdba | iPod | 4 | Jul 8, 2007 01:15 PM |
| How do I use Airport to wirelessly send Itunes music to my HI FI? | Marky | Macintosh Computers | 4 | Jan 4, 2005 08:29 PM |
All times are GMT -5. The time now is 07:44 AM.







Linear Mode

