Search code examples
objective-ccocoaapplescriptiphotoscripting-bridge

Creating iPhoto albums using Cocoa Scripting Bridge


I'm tearing my hair out trying to create a new album from a Cocoa Application. In applescript it's a nice simple procedure:

tell application "iPhoto"
    new album name "Album"
end tell

But I can't work out how this is done in Cocoa via the Scripting Bridge. I've tried this:

iPhotoApplication *iPhoto = [SBApplication applicationWithBundleIdentifier:@"com.apple.iPhoto"];
iPhotoAlbum *newAlbum = [[[[iPhoto classForScriptingClass:@"album"] alloc] initWithProperties:[NSDictionary dictionaryWithObject:@"Album" forKey:@"name"]] autorelease];
[[iPhoto albums] addObject:newAlbum];

But that had no effect.

Please help!


Solution

  • I haven't bothered to check, but I suspect there is a bug in either sdp or Scripting Bridge where commands that have keyword parameters are targeted at the main application object are given one method name by sdp (e.g. -newAlbumName:) and a different method name by SB (-newAlbum:name:). Since you can't hack SB, you'd need to patch the sdp-generated header to use the latter method and pass nil as the first argument.

    Alternatively, you could use appscript, which is more capable and less prone to application compatibility problems than SB. It also provides better development tools and support. e.g. Running your AppleScript through the accompanying ASTranslate tool produces the following objc-appscript code:

    #import "IPGlue/IPGlue.h"
    IPApplication *iphoto = [IPApplication applicationWithName: @"iPhoto"];
    IPNewAlbumCommand *cmd = [[iphoto newAlbum] name: @"Test"];
    id result = [cmd send];