Drag and Drop Tracks from iTunes in Applescript
I was working on a project in Applescript Studio recently that required tracks from iTunes to be dragged into an image view. It appears as though this isn’t possible to do in plain Applescript, but I did find a way to do it with a combination of Applescript, Objective-C, and the call method command.
Add the files AppHelper.h and AppHelper.m, as shown below, to your Applescript Studio project.
// AppHelper.h #import#import @interface NSApplication (AppHelper) - (NSArray *)tracksFromPasteboard:(NSPasteboard *)pboard; @end
// AppHelper.m
#import "AppHelper.h"
@implementation NSApplication (AppHelper)
- (NSArray *)tracksFromPasteboard:(NSPasteboard *)pboard
{
// Get iTunes track information from the pasteboard as a property list,
// get "Tracks" key from property list dictionary, then return all of
// the values of the "Tracks" dictionary.
return [[[[pboard propertyListForType:@"CorePasteboardFlavorType 0x6974756E"]
objectForKey:@"Tracks"] allValues] retain];
}
@end
Now in your Applescript code, you can call the tracksFromPasteboard: method using the call method command in Applescript. You will get a list of all of the dragged tracks. Each item of the list is a record with keys such as |Name|, |Location|, |Artist|, etc. As far as I can tell, there is no way to drag a playlist and get playlist information.
on drop theObject drag info dragInfo
-- Get a list of the data types on the pasteboard
set dataTypes to types of pasteboard of dragInfo
-- Get the iTunes track list data from Objective-C method
if "CorePasteboardFlavorType 0x6974756E" is in dataTypes then
set tracks to call method "tracksFromPasteboard:"
with parameter (pasteboard of dragInfo)
display dialog |Location| of item 1 of tracks
end if
end drop
