1

我正在努力从 Cocoa 应用程序创建新专辑。在 applescript 中,这是一个很好的简单过程:

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

但我无法通过 Scripting Bridge 在 Cocoa 中解决这个问题。我试过这个:

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

但这没有任何效果。

请帮忙!

4

2 回答 2

2

我曾经尝试使用 Cocoa Scripting bridge,但没有成功。我的解决方法是使用NSAppleScript类:

NSString * scriptSource = [NSString stringWithFormat:
    @"tell application \"iPhoto\" to import from \"%@\"", path];

NSAppleScript * exportScript =
    [[NSAppleScript alloc] initWithSource:scriptSource];    

[exportScript compileAndReturnError:NULL];
[exportScript executeAndReturnError:NULL];
于 2010-05-10T13:27:12.173 回答
1

我没有费心去检查,但我怀疑在sdp 或 Scripting Bridge 中存在一个错误,application其中具有关键字参数的命令以主对象为目标,由 sdp 给定一个方法名称(例如-newAlbumName:),由 SB 给定一个不同的方法名称( -newAlbum:name:)。由于您无法破解 SB,因此您需要修补 sdp 生成的标头以使用后一种方法并将 nil 作为第一个参数传递。

或者,您可以使用appscript,它比 SB 功能更强大,更不容易出现应用程序兼容性问题。它还提供了更好的开发工具和支持。例如,通过附带的 ASTranslate 工具运行 AppleScript 会生成以下 objc-appscript 代码:

#import "IPGlue/IPGlue.h"
IPApplication *iphoto = [IPApplication applicationWithName: @"iPhoto"];
IPNewAlbumCommand *cmd = [[iphoto newAlbum] name: @"Test"];
id result = [cmd send];
于 2010-05-10T20:26:08.487 回答