I need to use an Objective-C method to create a folder in a subdirectory. I’m making some progress, but could use some assistance or advice.
Basically this code is being run within an ‘on drop’ handler, so I can’t target the Finder or System Events (or use a ‘do shell script’) command to create a folder, otherwise my script will hang (see: http://macscripter.net/viewtopic.php?id=20189)
Ideally, I’d like to use the createDirectoryAtPath:withIntermediateDirectories:attributes:error: method, but I’m having some problems with it. So, I’ve decided to try createDirectoryAtPath:attributes:. I’ve gotten it to work, but it throws a “NSInternalScriptError (8)” even though it works successfully. I’m not sure why. Here’s my code:
set theFolderPath to POSIX path of (path to desktop folder) & "test folder"
set R to (call method "createDirectoryAtPath:attributes:" of (call method "defaultManager" of class "NSFileManager") with parameters {theFolderPath, 0})
return R
Again, this works. It creates a folder on the desktop named “test folder”, and if the folder already exists it returns zero. But it throws a “NSInternalScriptError (8)” (an unknown error) after it successfully creates the folder. I’m not sure why, and whether it’s okay to ignore that error? Especially since it works fine otherwise. But I’m confident that zero is the correct choice for “nil” since when I tried other values (null, empty list, etc.) the method did not create a new folder at all.
I’m wondering if it has something to do with the second parameter. That’s supposed to be a list of folder attributes, but sending “nil” is supposed to use the default settings. I’m using a zero for nil, and it creates a folder that has read/write privs for the current user.
I’ve also tried using the createDirectoryAtPath:withIntermediateDirectories:attributes:error: method. I would expect this to work similarly to createDirectoryAtPath:attributes:, but it doesn’t. It doesn’t throw any error at all, and just doesn’t seem to do anything. I’m wondering if it’s a problem with how I’m passing the parameters. I understand that 1 equals “yes”, and 0 equals"no" and “nil”, but I’m not sure exactly what equals “NULL”. I’ve tried “null”, 0, {}, false, missing value. All give the same result. No error, but no new folder is created either…
set theFolderPath to POSIX path of (path to desktop folder) & "test folder/test subfolder/"
return (call method "createDirectoryAtPath:withIntermediateDirectories:attributes:error:" of (call method "defaultManager" of class "NSFileManager") with parameters {theFolderPath, 1, 0, 0})
I also tried eliminating the 3rd and 4th parameter. Same result: no error, but no folder is created.
UPDATE: createDirectoryAtPath:withIntermediateDirectories:attributes:error: appears to only be compatible with Leopard (10.5) and higher. Strange, cause it didn’t specifically mention that at http://developer.apple.com/iPhone/library/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/createDirectoryAtPath:withIntermediateDirectories:attributes:error:
But, I’m using Xcode 2 on 10.4.11 right now and I just checked the NSFileManager.h Foundation.framework header, and that method isn’t listed. So gotta use Xcode 3 on Leopard for that method to work!