Wilcox Development Solutions Blog

I love FSRefs

October 28, 2003

I think I’ve got this right. Please correct me if I don’t.

With OS X, Apple recommends everybody use FSRefs to manage their file objects, instead of the old FSSpecs. FSRefs give you long file names, large files, and a slew of other things. Except you can’t use an FSRef to refer to a file that doesn’t exist.

So, right now everybody who programs in real languages (C/C++, Pascal, etc) have to remember this, and if they are referring to a new file, they use FSSpecs (which always could refer to new files.) Very annoying, but one can work around it (there are system calls to go from FSRefs to FSSpecs).

Now, I ran across this limitation in Python once, and I just did in Applescript. Here’s the problem: You write a line of code like this:

set myNewFilePath to "OSX:Users:rwilcox:Temp:filethatdoesntexistyet" tell app "Finder" set myNewAlias to alias myNewFilePath end tell

since filethatdoesntexistyet, well, doesn’t exist yet, the finder will give you an error. Even saying “to file myNewFilePath” errors out the same way. Why? Because the finder (rightly) refers to all files as FSRefs. But, of course, the file doesn’t exist yet. Error.

So I thought about doing what I’ve done in the past - open the file for read/write, then close it. That will create the file for me.

Except DropStuff errors out when I do that. It can’t (or won’t) write to a file that already exists.

Now, being able to tell DropStuff what file to create was part of the point. I want to compress this file, and I want to control where it goes. But, I can’t access a file that doesn’t exist (compile-time error), and I can’t point DropStuff at a file that already exists (run-time error).

It would be great it DropStuff would tell me what file it had created. Then I could do a simple rename and be done with it. However, it does not.

So, I’ve got to ask the finder for the name of every file in the same folder as the file I want to compress, tell DropStuff to compress the file, then get every file in the folder again. Compare the two, and we get the new files (including the file DropStuff created).

If I had one of two things from DropStuff it would be OK.

  • The ability to say “Replacing any existing files to create archive”
  • The ability to specify what name the archive should have
  • DropStuff returning the file it created

As it is right now, I have none of those things. So I spent an hour on a task that should have taken 10 minutes to write.

Thank you, Apple. And Aladdin.