使用 Cocoa Scripting Bridge 创建 iPhoto 相册

发布于 2024-09-01 03:08:57 字数 557 浏览 5 评论 0原文

我正在绞尽脑汁地尝试从 Cocoa 应用程序创建一张新专辑。在 applescript 中,这是一个很好的简单过程:

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

但我无法弄清楚这是如何通过脚本桥在 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];

但这没有效果。

请帮忙!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱我好吗 2024-09-08 03:08:57

我曾经尝试过使用 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];

I tried once to use Cocoa Scripting bridge, with no success. My workaround was to use NSAppleScript class:

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

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

[exportScript compileAndReturnError:NULL];
[exportScript executeAndReturnError:NULL];
山有枢 2024-09-08 03:08:57

我没有费心去检查,但我怀疑有 a sdp 或 Scripting Bridge 中的 bug,其中具有关键字参数的命令针对主 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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文