Please, what's wrong with this code snippet

I know, I’m doing something incredibly silly but, it’s very late here and my head killing me :o
What is wrong with this test code? Why isn’t ‘duplicate’ working?
Note that for this run, I had:
~Desktop:myFile.rtf
~Desktop:myFolder:
~Desktop:myFolder:myFile.rtf
expecting to get
~Desktop:myFolder:myFile1.rtf

tell application "Finder"
	set myFileName to "myFile"
	set myFolderName to "myFolder"
	set myFilePath to ((path to desktop) as text) & myFileName & ".rtf"
	set myFolderPath to ((path to desktop) as text) & myFolderName & ":"
	if exists ((myFolderPath & myFileName) & ".rtf") then
		set theSuffix to ""
		repeat
			if not (exists ((myFolderPath & myFileName) & theSuffix & ".rtf")) then exit repeat
			set theSuffix to theSuffix + 1
		end repeat
		set myFileName to myFileName & theSuffix
		set myFilePath to myFolderPath & myFileName & ".rtf"
		duplicate myFilePath to myFolderPath
		--> error number -1700 from "HD-1:Users:chris:Desktop:myFolder:myFile1.rtf" to item
		--> Result: error "Finder got an error: Can't make \"HD-1:Users:chris:Desktop:myFolder:myFile1.rtf\" into type item." number -1700 from "HD-1:Users:chris:Desktop:myFolder:myFile1.rtf" to item
	else
		duplicate myFilePath to myFolderPath
	end if
	open myFolderPath
end tell

Please somebody put me out of my misery :frowning:

Hi,

the problem is you’re adding the numeric suffix, but then there is no source file with this name.

You should also add always an object specifier for example


if exists file ((myFolderPath & myFileName) & ".rtf") then

or


duplicate file myFilePath to folder myFolderPath

Another suggestion
(path to desktop as text) is a parameter
((path to desktop) as text) is a coercion which is more expensive

Hi,

Also, this works:


tell application "Finder"
	desktop as text
end tell

Editted: I think Stefan meant to write that.

Model: MBP
AppleScript: 2.2.3
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)

desktop is a special case because the desktop folder is the “root” folder of the Finder
Yes, I meant to use the parameter rather than the coercion

I knew Stefan meant ‘desktop as string’ because he wrote that it was a parameter. ‘desktop’ is a parameter of Finder, so you don’t need the ‘path to’ StandardAdditions’s command. You can just coerce the Finder reference to string.

Editted: I meant property. :smiley:

I think there is on the first line in the “if exists .” block:
set theSuffix to “”

I did try both but, it still doesn’t work in the “if … else” block. This is my amended code:

tell application "Finder"
	set myFileName to "myFile"
	set myFolderName to "myFolder"
	set myFilePath to (path to desktop as text) & myFileName & ".rtf"
	set myFolderPath to (path to desktop as text) & myFolderName & ":"
	if exists file ((myFolderPath & myFileName) & ".rtf") then
		set theSuffix to ""
		repeat
			if not (exists ((myFolderPath & myFileName) & theSuffix & ".rtf")) then exit repeat
			set theSuffix to theSuffix + 1
		end repeat
		set myFileName to myFileName & theSuffix
		set myFilePath to myFolderPath & myFileName & ".rtf"
		-- duplicate myFilePath to myFolderPath --> FAILS
		duplicate file myFilePath to folder myFolderPath --> FAILS
		--> error number -1728 from file "HD-1:Users:chris:Desktop:myFolder:myFile1.rtf"
		--> Result: error "Finder got an error: Can't set folder \"HD-1:Users:chris:Desktop:myFolder:\" to file \"HD-1:Users:chris:Desktop:myFolder:myFile1.rtf\"." number -10006 from folder "HD-1:Users:chris:Desktop:myFolder:"
	else
		-- duplicate myFilePath to myFolderPath --> WORKS
		duplicate file myFilePath to folder myFolderPath --> WORKS
	end if
end tell

if myFolder is empty, then the “else / end if” block kicks in and it does copy “myFile.rtf” to “myFolder” with either of the two alternative statements. So, the duplicate command itself does work. Good, something less to worry about.

However, if there is already a copy of myFile.rtf in myFolder, then the “if exists / else” block should do its part but, it keeps failing. From the error message, you can see that it does calculate and add the correct suffix, attempting to copy myFile1.rtf (when I added myFile1.rtf manually, it correctly attempted to copy myFile2.rtf) but it still fails to copy. Why!? to keep me awake in the middle of the night!? This is all supposed to be so elementary and it drives me nuts :mad:
Can you please make that work?

Thanks for that, I didn’t know there is a real difference.

Cheers, Chris

Assuming “myFile.rtf” exists on desktop and “myFolder” is empty this happens

First run of the script:

first if exists evaluates false and the file will be duplicated

Second run of the script:

first if exists evaluates true
¢ then the script changes the file name string “ because the myFile.rtf exists “ to myFile1.rtf
¢ then the script tries to duplicate myFile1.rtf in folder myFolder to the folder myFolder (!)
It fails because there is no file myFile1.rtf in folder myFolder.

Changing the file name string does not change the name of the file on disk

You know what you have to do when you start getting frustrated, take a break.:slight_smile:

I cannot overwrite an existing file. I must copy the new file with an append file name.
So, how should something like this be coded? :confused:

BTW, if you’re adding numeric suffixes to the name, you might want to add leading zeroes to the numbers. That way it remains sorted in the Finder, for later use. If the number of files is less than a thousand, then you would add two leading zeroes max.

The Finder cannot rename and move/copy at the same time
but you could rename the file before duplicating it


set myFileName to "myFile"
set myFolderName to "myFolder"

tell application "Finder"
	
	set myFilePath to (desktop as text) & myFileName & ".rtf"
	set myFolderPath to (desktop as text) & myFolderName & ":"
	if exists file ((myFolderPath & myFileName) & ".rtf") then
		set theSuffix to ""
		repeat
			if not (exists ((myFolderPath & myFileName) & theSuffix & ".rtf")) then exit repeat
			set theSuffix to theSuffix + 1
		end repeat
		set myNewFileName to myFileName & theSuffix & ".rtf"
		set fileToDuplicate to myFilePath as alias
		set name of fileToDuplicate to myNewFileName
		duplicate fileToDuplicate to folder myFolderPath 
	else
		duplicate file myFilePath to folder myFolderPath
	end if
end tell


Many thanks Stefan, you restored my sanity and my belief in human kindness :slight_smile:
I added one line to restore the original file’s name. This avoids a fatal error if one attempts to repeat copying.

set myFileName to "myFile"
set myFolderName to "myFolder"
tell application "Finder"
	set myFilePath to (desktop as text) & myFileName & ".rtf"
	set myFolderPath to (desktop as text) & myFolderName & ":"
	if exists file ((myFolderPath & myFileName) & ".rtf") then
		set theSuffix to ""
		repeat
			if not (exists ((myFolderPath & myFileName) & theSuffix & ".rtf")) then exit repeat
			set theSuffix to theSuffix + 1
		end repeat
		set myNewFileName to myFileName & theSuffix & ".rtf"
		display dialog myNewFileName as text --> myFile1.rtf
		set fileToDuplicate to myFilePath as alias
		display dialog fileToDuplicate as text --> "HD-1:Users:chris:Desktop:myFile.rtf"
		set name of fileToDuplicate to myNewFileName
		display dialog name as text --> Finder
		duplicate fileToDuplicate to folder myFolderPath
		-- Reset the name of the original file to the original name.
		set name of fileToDuplicate to myFileName & ".rtf"
	else
		duplicate file myFilePath to folder myFolderPath
	end if
end tell

Now, I have to think how to convert this to a handler, because it appears that I’ll end up having to repeat the same code in about 4 branches of the script I’m working on. I’ll be back if I get stuck again.

Cheers, Chris

Converting it to a handler is actually quite simple:

-- The Script
-- ==========
-- do thinks ...
set myFileName to "myFile"
set myFolderName to "myFolder"
tell application "Finder"
	-- do other thinks ...
	set myFilePath to (desktop as text) & myFileName & ".rtf"
	set myFolderPath to (desktop as text) & myFolderName & ":"
	
	-- call the handler within a tell block.
	my CopyRenameHandler(myFileName, myFolderName, myFilePath, myFolderPath)
	
	open myFolderPath -- just for testing
	-- do more thinks ...
end tell

-- Copy Rename Handler
-- ====================
on CopyRenameHandler(myFileName, myFolderName, myFilePath, myFolderPath)
	tell application "Finder"
		if exists file ((myFolderPath & myFileName) & ".rtf") then
			set theSuffix to ""
			repeat
				if not (exists ((myFolderPath & myFileName) & theSuffix & ".rtf")) then exit repeat
				set theSuffix to theSuffix + 1
			end repeat
			set myNewFileName to myFileName & theSuffix & ".rtf"
			set fileToDuplicate to myFilePath as alias
			set name of fileToDuplicate to myNewFileName
			duplicate fileToDuplicate to folder myFolderPath
			-- Reset the name of the original file to the original name.
			set name of fileToDuplicate to myFileName & ".rtf"
		else
			duplicate file myFilePath to folder myFolderPath
		end if
	end tell
end CopyRenameHandler