I know this question has probably been asked and answered elsewhere in these forums (probably multiple times!) but I’m going to throw it out there anyway because I can’t find exactly what I’m looking for. I’m posting in this one because, even though this is more of a basic AS question, I’m going to be implementing whatever I get working in Xcode and I imagine there may be some gotchas once I get to that point. I’d just like to get it running in ASEditor first so that I can test it more easily.
Here’s what I’m trying to do. I need to create a directory tree in a location a user chooses, any part of which may or may not already exist. I have no problem creating the entire tree from scratch wherever (it’s typically going to be on a mounted network share, btw). There are 2 problems I keep running into. The first is that you can’t use “exists” on aliases for folders that don’t exist yet (of course!) How do I get around that? The second is that I need to be able to skip folders where the value for the name is empty (“”). For example, say the first tree that needs to be created is (I’m not going to bother with the code at this point.)
rootdir (does not exist)
school1 (does not exist)
department1 (does not exist)
2011 (does not exist)
jobNum1 (does not exist)
but the next time the app is run the tree needs to be
rootdir (exists)
school1 (exists)
department1 (skipped - field left empty, name set to “”)
2009 (does not exist)
jobNum1 (does not exist)
then the third time
rootdir (exists)
school1 (exists)
department1 (skipped - field left empty, name set to “”)
2009 (exists)
jobNum2 (does not exist)
Then at the end I’d like to display a dialog showing whatever the full path created was, whether parts of it already existed or not.
I’ve been trying to do this without using POSIX paths or shell scripts, simply because I’m most comfortable with straight AS, but if there’s no way around it I’m open to using one or both of those. I’m just not familiar with them.
in ObjC/ASOC there is the method createDirectoryAtPath:withIntermediateDirectories:attributes:error: or createDirectoryAtURL:withIntermediateDirectories:attributes:error: of class NSFileManager. These methods can create intermediate directories regardless whether they already exist.
Alternatively the shell command mkdir with the -p switch can do the same thing
You can check a given string path for an empty path component with this ASOC code
Cool! Could you post a basic example of the code for either or both of the first 2 methods you mention so I can see how the path needs to be formed when passed to them? It would really help! Thanks!!
C
I think this did what you want (filling in your user name):
set fm to current application's NSFileManager's defaultManager()
set thePath to "/Users/<your user name here>/Desktop/Root/School1/Department1/2011/job1"
set thePath2 to "/Users/<your user name here>/Desktop/Root/School1/2009/job1"
fm's createDirectoryAtPath_withIntermediateDirectories_attributes_error_(thePath,1,missing value,missing value)
fm's createDirectoryAtPath_withIntermediateDirectories_attributes_error_(thePath2,1,missing value,missing value)
One more question - I was getting the path of the root folder like this:
on chooseRoot_(sender)
set theRoot to current application’s NSOpenPanel’s openPanel()
theRoot’s setCanChooseFiles_(0)
theRoot’s setCanChooseDirectories_(1)
theRoot’s setTitle_(“Choose a Folder”)
theRoot’s setPrompt_(“Choose a Folder”)
theRoot’s runModal()
set thePath to item 1 of theRoot’s URLs()
end chooseRoot_
The URL returned always starts with “file://”. Is there a way to there a way to easily convert the URL to a POSIX path? Or get the POSIX path in the first place? It would save a lot of time!
the method |path|() of NSURL returns the path portion of an URL but it’s much easier to
use createDirectoryAtURL_withIntermediateDirectories_attributes_error_() method instead
set fm to current application’s NSFileManager’s defaultManager()
set thePath to “/Users//Desktop/Root/School1/Department1/2011/job1”
set thePath2 to “/Users//Desktop/Root/School1/2009/job1”
fm’s createDirectoryAtPath_withIntermediateDirectories_attributes_error_(thePath,1,missing value,missing value)
fm’s createDirectoryAtPath_withIntermediateDirectories_attributes_error_(thePath2,1,missing value,missing value)
end on makeNewFolder_
I did that so that it would happen when a user clicks the “Make New Folder” button in the UI.
I pasted your code into my app and it worked fine (after putting in my user name of course). There shouldn’t be an “on” in the end makeNewFolder line, but I’m assuming you don’t actually have that, because it won’t even compile with that there.
The way Stefan wrote above is if you want to call it from the code, but your way is correct if you’re calling it from a button.
I figured it out. I had put “set fm to current application’s NSFileManager’s defaultManager()” inside of “on applicationWillFinishLaunching_(aNotification)”. Moving it to “on awakeFromNib()” fixed it.
Hmmm… That shouldn’t have made any difference. If your definition of fm is in a different method than makeNewFolder, it would have to be a property (I assume you did that), but whether it’s defined in awakeFromNib or applicationWillFinishLaunching, or in makeNewFolder (as you posted) shouldn’t make any difference.
Are you sure that your applicationWillFinishLaunching method actually ran, because if you didn’t get an NSFileManager instance, that’s the error message you would get. Are you sure that your script actually is the app delegate of your project? I noticed that your error message started: [FolderMakerAppDelegate makeNewFolder:]: If you’re using Xcode 4, it should just say [AppDelegate makeNewFolder:]: unless you changed the name of the file (or put FolderMaker in the Class prefix field at project creation which would be OK), which may have screwed things up.