I have looked at many, many threads related to the script on StackOverflow and elsewhere, and it seems that I cannot understand why the Cocoa code block that calls the script script call for Finder no longer works correctly in accordance with 10.6. (A similar version of the code seemed to work fine under 10.5, and I don't know what caused the change in behavior.)
Basically, I'm trying to access some display options for the Finder window. I have the following code code as a test case. I point it to the folder that appears as icons, and when I run the code, none of the error blocks work, but I always get a meaningless answer (iconSize = 0) at the end.
// 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"); }
A clean version of this AppleScript code works fine even when pointing to the same folder:
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
My gut instinct is that something rushes or transforms strangely when it passes through the script bridge, but I am completely unaware of what to check or where else to look. I tried to print class names along the way, since objects are retrieved from Finder and marked with [SBObject *get] calling the end of various SB assignment statements, but to no avail.
Any ideas?
UPDATE
OK, so I found where the error is generated in the code above, although it doesn't seem to me that I'm getting closer to fixing the problem. It turns out that the lazy assessment of the script bridge masked the problem. If, after receiving a link to FinderWindow, you insert the following two lines of code:
NSString *test = [folderWindow name]; NSLog(@"Return value == %@; error message == %@", test, [[folderWindow lastError] localizedDescription]);
Then the Scripting Bridge tries to actually search for the name, fails, and returns with a slightly more constructive error message:
Return value == (null); error message == The operation couldn't be completed. (OSStatus error -1700.)
This is amazing (progress!), But still does not bring me much closer to solving the problem. This error message seems to indicate that there is some kind of AEcoercion problem, but I'm not sure how to proceed with its solution. The generated Finder.h file (and the AppleScript Finder dictionary) are well aware that I need to return a reference to the FinderWindow object, and printing the folderWindow object seems to confirm that everything is okay as long as the name is called.