【发布时间】:2011-03-31 23:43:36
【问题描述】:
我一直在 StackOverflow 和其他地方浏览大量与 Scripting Bridge 相关的线程,但似乎无法在弄清楚为什么 Cocoa 代码块对 Finder 进行 Scripting Bridge 调用没有任何进展在 10.6 下,longer 可以正常工作。 (类似版本的代码在 10.5 下似乎可以正常工作,我不知道是什么导致了行为变化。)
基本上,我正在尝试访问 Finder 窗口的一些显示选项。我有以下代码块作为我的测试用例。我将它指向一个 is 显示为图标的文件夹,当我运行代码时,没有任何错误块跳闸,但我总是在结束。
// Set up the Scripting Bridge
FinderApplication *finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
// Get an HFS-style reference to a specified folder
// (folderPath is an NSString * containing a POSIX-style path to a folder)
NSURL *folderURL = [NSURL fileURLWithPath:folderPath];
NSString *folderPathHFS = (NSString *)CFURLCopyFileSystemPath((CFURLRef)folderURL, kCFURLHFSPathStyle);
// Get the Finder-native folder reference
FinderFolder* folder = [[finder folders] objectAtLocation:folderPathHFS];
if (folder == nil) {
NSLog(@"folder error: %@", [[folder lastError] localizedDescription]);
return;
}
// Get the Finder-native container window associated with the folder
[folder openUsing:finder withProperties:nil];
FinderFinderWindow *folderWindow = [[folder containerWindow] get];
if (folderWindow == nil) {
NSLog(@"folderWindow error: %@", [[folderWindow lastError] localizedDescription]);
return;
}
// Retrieve the view preferences for the folder
FinderIconViewOptions *ivo = [folderWindow iconViewOptions];
if (ivo == nil) {
NSLog(@"ivo error: %@", [[ivo lastError] localizedDescription]);
}
// Get the current icon size
int iconSize = (int)[ivo iconSize];
// Display the icon size in our label
if (iconSize > 0) {
NSLog(@"successfully retrieved icon size: %d", iconSize);
} else {
NSLog(@"couldn't retrieve icon size");
}
此代码的纯 AppleScript 版本运行良好,即使指向同一个文件夹:
tell application "Finder"
set aFolder to the folder "<HFS path to folder in question>"
set aFolderWindow to the container window of aFolder
set aIVO to the icon view options of aFolderWindow
return the icon size of aIVO
end tell
我的直觉是某些东西在通过脚本桥时被奇怪地转换或转换,但我完全不知道要检查什么或在哪里查看。在从 Finder 检索对象并将 [SBObject *get] 调用标记到各种 SB 相关赋值语句的末尾时,我尝试打印出类名,但无济于事。
有什么想法吗?
更新
好的,所以我发现在哪里在上面的代码中生成了错误,尽管我不觉得我离解决问题更近了。事实证明,Scripting Bridge 的惰性评估掩盖了这个问题。如果在检索到 FinderWindow 的引用后,插入以下两行代码:
NSString *test = [folderWindow name];
NSLog(@"Return value == %@; error message == %@", test, [[folderWindow lastError] localizedDescription]);
然后,Scripting Bridge 尝试实际执行名称检索,但失败了,并返回一条更具建设性的错误消息:
Return value == (null); error message == The operation couldn’t be completed. (OSStatus error -1700.)
这太棒了(进步?!),但仍然没有让我更接近解决问题。该错误消息似乎表明某处存在 AEcoercion 问题,但我不确定如何继续解决它。生成的 Finder.h 文件(和 Finder 的 AppleScript 字典)都非常清楚我应该取回对 FinderWindow 对象的引用,并且打印出 folderWindow 对象似乎可以验证一切正常,直到name 电话。
【问题讨论】:
-
我不确定这是否有用,但我也确定发生的任何问题都可能在
[folderWindow iconViewOptions]检索调用之前发生(即,这不仅仅是问题抓住IVO结构)。如果我在获得文件夹窗口的引用后尝试更简单的方法,例如NSLog(@'Finder window id: %@", [folderWindow id]);,我会得到相同的无用(和不正确)零结果。
标签: objective-c cocoa macos applescript scripting-bridge