Error -10006 when renaming a file

Hi–

I’m working with some basic code, just trying to get it to rename a file. (Obviously this is part of a larger script, but I can’t get it to do this at all.) Here is the relevant snippet of code.


tell application "Finder"
   set dataFile to (choose file name default name "First File Name") as string
   open for access file dataFile with write permission
   close access file dataFile
   try
       set the name of dataFile to (choose file name default name "New File Name") as string
   on error e number n
       display dialog "Error " & n & ": " & e
   end try
end tell

It works fine through the first part–creates the file, with whatever name you give it. Then it pops up the dialog for you to give it a new name." But when you click “Save” on that box, it dies with an error -10006: Can’t set name of “PATH:First File Name” to “PATH:New File Name”. I’m not trying to save it to a different directory or anything; just want to rename it.

-10006 is, I believe, “errAEWriteDenied”. Which makes it seem to me like the file is locked or something. But if I try to write over the file, rather than rename it, that works fine. So why won’t it let me rename?

For what it’s worth, I’m working on this in Panther, but I’ve tested it in OS 9.2 and the behavior is identical.

Thanks.

–spoko.

You can’t use a path (as returned by “choose file name”) as name for a file, as it includes colons, illegal character in Finder names. You should try something as:

set name of dataFile to text returned of (display dialog "Rename file as..." default answer "new name" with icon note)

I’ll give that a shot. Just out of curiosity, what does the “icon note” bit do?

Will display a nifty icon next to the prompt (there is also “caution” and “stop”)

Nope. I get the same error that way.

Run this to run through the icons in a dialog:

set Icons to {note, caution, stop}
repeat with anIcon in Icons
	display dialog ("Here is the \"" & anIcon as text) & "\" icon" with icon anIcon buttons {"OK"} default button "OK"
end repeat

In all honesty, I couldn’t care less about the icons in the dialog. What I really need is to be able to rename a file from user input (I can’t believe I’m the first person who ever wanted to do this). The dialog doesn’t seem to be helping in that regard. Any idea what will, anyone?

Thanks.

–spoko.

Try this:

set dataFile to (choose file name default name "First File Name") as Unicode text
do shell script "touch " & quoted form of POSIX path of dataFile
tell application "Finder" to set name of file dataFile to text returned of (display dialog "Rename file as..." default answer "new name" with icon 1)

First, it uses the command line tool “touch” to create the file and then, the issue with the text returned not renaming the file is that the path is still a string, not a file reference. Adding “file” before the path string coerces it to a file reference. You could omit the “as Unicode text” from the first line (you originally had it as “string” but Unicode text is better) and then you also wouldn’t need to coerce the path to a reference because it already would be a file reference:

set dataFile to (choose file name default name "First File Name")
do shell script "touch " & quoted form of POSIX path of dataFile
tell application "Finder" to set name of dataFile to (text returned of (display dialog "Rename file as..." default answer "new name" with icon 1))

Jon

I would have used “touch,” but I want this to be OS9-compatible also. I will try your other tips, though, and see how that works. Thanks.

–spoko.

Jon–

You rock. I figured it was a problem with the path somehow, but had no idea how to fix it. That did it, though.

Thanks.

–spoko.