Wednesday, August 31, 2005

 

Need the Name of a File?

Sometimes, when you're writing scripts, you need a quick and dirty way to get the name of a file in your file system. A way I have used (particulary in CS days when ESTK wasn't available) was to run a script like this:
myFile = File.openDialog("Pick a file");
prompt("File name is:",myFile.fsName);
The first of these statements throws up a dialog that lets you choose a file from the filing system -- notice that that's all it does; the file is not opened or disturbed in any way.

The second statement displays the file-system specific name of the file in a text box in the prompt dialog allowing you to copy it and thus paste it into your script.

Warning


This approach doesn't work so well on a Windows machine because of the backslash character serving the double purpose of separating folder names in a file path and serving as an escape character in strings. When you copy the file path out of the prompt, you get single backslashes (on Windows) which need to be converted to double backslashes in order that the string be interpreted properly by the JavaScript interpreter.

Works fine on a Mac, though.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?