私は、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
何かが Scripting Bridge を通過するときに何かが変な形でキャストまたは変換されているというのが私の本能ですが、何をチェックすればよいか、他にどこを見ればよいかについてはまったく考えがつきません。[SBObject *get]
オブジェクトが Finder から取得され、さまざまな SB 関連の割り当てステートメントの最後に呼び出しをタグ付けするときに、途中でクラス名を出力しようとしましたが、役に立ちませんでした。
何か案は?
アップデート
OK、上記のコードでエラーが生成されている場所を発見しましたが、問題の修正にそれほど近づいているようには感じません. Scripting Bridge の遅延評価が問題を覆い隠していたことが判明しました。FinderWindow への参照を取得した後、次の 2 行のコードを挿入するとします。
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