我一直在 StackOverflow 和其他地方浏览大量与 Scripting Bridge 相关的线程,并且似乎无法在弄清楚为什么对 Finder 进行 Scripting Bridge 调用的 Cocoa 代码块不再正常工作方面取得任何进展10.6以下。(类似版本的代码在 10.5 下似乎可以正常工作,我不知道是什么导致了行为变化。)
基本上,我正在尝试访问 Finder 窗口的一些显示选项。我有以下代码块作为我的测试用例。我将它指向一个显示为图标的文件夹,当我运行代码时,没有一个错误块跳闸,但我总是在最后得到一个无意义的响应(iconSize = 0)。
// 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 检索对象并将调用标记到各种 SB 相关赋值语句的末尾时,我尝试打印出类名[SBObject *get]
,但无济于事。
有任何想法吗?
更新
好的,所以我在上面的代码中发现了错误是在哪里产生的,尽管我觉得我离解决问题还差得远。事实证明,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
调用之前一切正常。