Duplicating and renaming files

Hi,

I have found a script that allows me to duplicate and file and then rename it but I keep getting an error (error number -10010) the person who made the code just had a dialogue box show up saying an error occurred.

Here is the code:

set TheFile to POSIX file "/Users/server/Desktop/backup_info/Backup log full/full_backup_log.rtf" as text
set DestinationPath to POSIX file "/Users/server/Desktop/backup_info/Backup log full/October/9/" as text
set destinationFilename to ((current date) as text) & ".rtf"
with timeout of 600 seconds
	tell application "Finder"
		try
			set theDupe to duplicate TheFile to folder DestinationPath
			set name of theDupe to destinationFilename
		on error
			display dialog "Oops, a problem occurred duplicating the file."
		end try
	end tell
end timeout


This is going into a script that creates folders based on the month and day it will either create the folders or move and rename a backup log file.

is there something I am doing wrong? I have tried removing the POSIX file and replacing “/” with “:” and removing as text and I still seem to get the error.

Thanks,
Bruce

Hello bruce.

You should be able to see an error message in the events pane in Applescript Editor if you run your script from there, that may give you a clue about what is wrong. :slight_smile:

To me it seems like it may work if you remove the coercions to text, (as text).

You may also insert various log statements at various places, in order to figure out what is wrong, or where it did go wrong.

Hi,

here is the events/replies from the log at the bottom:


tell current application
	current date
		--> date "Thursday, 9 October 2014 10:43:50"
end tell
tell application "Finder"
	copy "Server:Users:server:Desktop:backup_info:Backup log full:full_backup_log.rtf" to folder "Server:Users:server:Desktop:backup_info:Backup log full:October:9:"
		--> error number -10010
	display dialog "Oops, a problem occurred duplicating the file."
		--> {button returned:"OK"}
end tell
Result:
{button returned:"OK"}

Thanks,
Bruce

The try-on error block is eating the error message.
Remove it, and you should see the actual error.

Basically, you should not have try blocks in your code until it is debugged.

ah right sorry didn’t realise that.

here is the actually error:

 Result:
error "Finder got an error: Handler can't handle objects of this class." number -10010

and highlights:

duplicate TheFile to folder DestinationPath

Finder cannot duplicate text. It duplicates Finder items - files, folders, whatever.
Check its dictionary.

Hi,

I recommend to use HFS paths


set backupLogFullFolder to (path to desktop as text) & "backup_info:Backup log full:"
set theFile to backupLogFullFolder & "full_backup_log.rtf"
set destinationPath to backupLogFullFolder & "October:9:"

set destinationFilename to ((current date) as text) & ".rtf"
with timeout of 600 seconds
	tell application "Finder"
		try
			set theDupe to duplicate file theFile to folder destinationPath
			set name of theDupe to destinationFilename
		on error e
			display dialog "Oops, a problem occurred duplicating the file." & return & e
		end try
	end tell
end timeout

Hi,

@MCUsrLL

I still get the error

error "Finder got an error: Handler can't handle objects of this class." number -10010

and
@StefanK

I get this error:

tell current application
	path to desktop as text
		--> "Server:Users:server:Desktop:"
	current date
		--> date "Thursday, 9 October 2014 11:12:45"
end tell
tell application "Finder"
	copy file "Server:Users:server:Desktop:backup_info:Backup log full:full_backup_log.rtf" to folder "Server:Users:server:Desktop:backup_info:Backup log full:October:9:"
		--> error number -1728 from file "Server:Users:server:Desktop:backup_info:Backup log full:full_backup_log.rtf"
	display dialog "Oops, a problem occurred duplicating the file.
Finder got an error: Can't set folder \"Server:Users:server:Desktop:backup_info:Backup log full:October:9:\" to file \"Server:Users:server:Desktop:backup_info:Backup log full:full_backup_log.rtf\"."
		--> {button returned:"OK"}
end tell

and
@alastor993
I am copying an .rtf file that shouldn’t affect it should it?

Thanks,
Bruce

the error occurs if the source file (or the destination folder) does not exist

This is a version which creates the folder of current month/day automatically


set backupLogFullFolder to (path to desktop as text) & "backup_info:Backup log full:"
set theFile to backupLogFullFolder & "full_backup_log.rtf"
tell (current date) to set {destinationFilename, currentMonth, currentDay} to {(it as text) & ".rtf", its month as text, its day}
set destinationPath to backupLogFullFolder & currentMonth & ":" & currentDay & ":"

do shell script "/bin/mkdir -p " & quoted form of POSIX path of destinationPath

with timeout of 600 seconds
	tell application "Finder"
		try
			set theDupe to duplicate file theFile to folder destinationPath
			set name of theDupe to destinationFilename
		on error e
			display dialog "Oops, a problem occurred duplicating the file." & return & e
		end try
	end tell
end timeout


I seem to still get the same error.

Do I need to change any accessibility settings possibly?

 tell current application
	path to desktop as text
		--> "Server:Users:server:Desktop:"
	current date
		--> date "Thursday, 9 October 2014 11:34:01"
	do shell script "/bin/mkdir -p '/Users/server/Desktop/backup_info/Backup log full/October/9/'"
		--> ""
end tell
tell application "Finder"
	copy file "Server:Users:server:Desktop:backup_info:Backup log full:full_backup_log.rtf" to folder "Server:Users:server:Desktop:backup_info:Backup log full:October:9:"
		--> error number -1728 from file "Server:Users:server:Desktop:backup_info:Backup log full:full_backup_log.rtf"
	display dialog "Oops, a problem occurred duplicating the file.
Finder got an error: Can't set folder \"Server:Users:server:Desktop:backup_info:Backup log full:October:9:\" to file \"Server:Users:server:Desktop:backup_info:Backup log full:full_backup_log.rtf\"."
		--> error number -128
Result:
error "Finder got an error: User canceled." number -128

Thanks,
Bruce

Are you sure that the file /Users/server/Desktop/backup_info/Backup log full/full_backup_log.rtf really exists?

I tested my script successfully

PS: For me it’s a bit confusing to use folder and file names with and without underscore characters

PS: the shell command ditto is able to create intermediate directories, duplicate and rename in one line


set backupLogFullFolder to POSIX path of (path to desktop) & "backup_info/Backup log full/"
set theFile to backupLogFullFolder & "full_backup_log.rtf"
tell (current date) to set {destinationFilename, currentMonth, currentDay} to {(it as text) & ".rtf", its month as text, its day}
set destinationPath to backupLogFullFolder & currentMonth & "/" & currentDay & "/"

do shell script "usr/bin/ditto " & quoted form of theFile & space & quoted form of (destinationPath & destinationFilename)


Doh!
sorry the file was placed a folder up and was in backup_info

I didn’t realise it works great now, sorry for the confusion.

Thanks,
Bruce