On May 23, 2008, at 10:06 AM, Jens Alfke wrote:

The biggest issue with the album art is simply getting the image into PICT format, since last I checked, iTunes won't accept any other format. Unfortunately NSImage can read but not generate PICT. The only way I found to do it was to use <ick> QuickDraw APIs to create a GWorld, then draw the NSImage into it, and then create a PICT from the GWorld.


True, though actually the OP showed a clever little technique using NSPasteboard, much easier than a GWorld and works great.

In the past I've used Image Events, it'll convert many formats to a PICT file, then read from offset 513 (skipping the PICT header) to get the data iTunes wants. Works fine.

The difficulty isn't with the PICT data, it's that a script like this, which works under Tiger:

tell application "iTunes"
        tell current track
        try
                set data of artwork 1 to myPICTData
        on error error_msg number error_num
                display dialog error_msg & error_num
        end try
        end tell
end tell

does not work under Leopard.

On Leopard, you get param err instead. Other variants I tried didn't work either, often getting good old "Unknown object type" error, so something sure seems to have changed. Makes it difficult to write the corresponding Scripting Bridge code...

This works up to the -addObject, which throws error -10014: "Handler only handles single objects."

// Make a test image from an icon
NSImage* icon = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAppleLogoIcon)];
NSData *tiffData = [icon TIFFRepresentation];
        
// Convert to PICT
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:@"Sample"];
[pboard declareTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:nil];
[pboard setData: tiffData forType:NSTIFFPboardType];
NSData *pictData = [pboard dataForType:NSPICTPboardType];
[pboard releaseGlobally];

// Prove it's valid PICT data by writing to file with
// 512 bytes header of zeroes. Opens fine in Preview.
NSMutableData *pictFileData = [NSMutableData dataWithLength:512];
[pictFileData appendData:pictData];
[pictFileData writeToFile:@"/test.pict"       atomically:NO];

// Create the new artwork's properties. I also tried a dictionary
// with various combinations 4 artwork properties, but it didn't
// make any difference, and that isn't how it's done in Applescript anyway. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"data", pictData, nil];


// (Note to OP, this line won't work in SB)
//iTunesArtwork *artwork = [[SBObject alloc] init];

// Instead, use this.
// Note the class name "artwork" is the name from the Applescript dictionary, // NOT the classname from the generated SB header file! Sure, it's documented somewhere...
//
iTunesArtwork *artwork = [[[[iTunesApp classForScriptingClass:@"artwork"] alloc] initWithProperties:dict] autorelease];

@try {
// According to SB docs, the new object has to be
// added to its container before it becomes 'real'.
// This looks like the sample code, seems like
// it should work, but throws instead.
/
// Since the analogous Applescript doesn't work
// either (under Leopard, does on Tiger)
// it's hard to know where the problem is.

        [[currentTrack artworks] addObject:artwork];
}
@catch(NSException *e)
{
        NSLog( @"Error:[EMAIL PROTECTED]:[EMAIL PROTECTED]:%@",
                [e name], [e reason], [e userInfo] );
}


NSLog output:
============
Error:NSGenericException

Reason:Apple event returned an error. Event = 'core'\'crel'{ 'kocl':'cArt', 'insh':'insl'{ 'kobj':'obj '{ 'want':'cArt', 'from':'obj '{ 'want':'prop', 'from':'null'(), 'form':'prop', 'seld':'pTrk' }, 'form':'indx', 'seld':'abso'($206C6C61 $) }, 'kpos':'end ' }, 'prdt':{ 0x00000000:'utxt'("data") } }
Error info = {
    ErrorNumber = -10014;
}

Info:{
    ErrorNumber = -10014;
}

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to