Copying one file to many others with new name

I have a file that I want copied 10 times. The original file name is name.txt and the new files are name.1.txt, name.2.txt… etc. I have written the following code so that I can enter the starting number (1 in this case) and the ending number (10 in this case).

It should be obvious that the code is a combination of ‘Add to Filenames.scpt’ and ‘Trim Filenames.scpt’ from the finder examples, but I am getting lost in how to use this_item when it comes time to make a new file. I have indicated the point in which I have trouble with ########. I seem to have everything working except this part because the finder gets the error :Can’t make some data into expected type: with each file that I want created.

It assumes that the top most window in Finder contains the one file that is to be duplicated.

This is my first attempt at an apple script so please be nice :). Thanks,

Andy



try
	tell application "Finder" to set the source_folder to (folder of the front window) as alias
on error -- no open folder windows
	set the source_folder to path to desktop folder as alias
end try

set the prefix_or_suffix to ""
set the startNumber to ""
set the stopNumber to ""
set the character_count to 0

repeat
	display dialog "Enter Start Experiment#" default answer the startNumber buttons {"Cancel", "OK"}
	copy the result as list to {the startNumber, the button_pressed}
	display dialog "Enter Stop Experiment#" default answer the stopNumber buttons {"Cancel", "Ok"}
	copy the result as list to {the stopNumber, the button_pressed}
	display dialog "Enter the prefix or suffix to use:" default answer the prefix_or_suffix buttons {"Cancel", "Suffix"}
	copy the result as list to {the prefix_or_suffix, the button_pressed}
	if the prefix_or_suffix is not "" then exit repeat
end repeat

set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string

repeat with i from startNumber to stopNumber
	set this_item to item 1 of the item_list
	set this_item to (source_folder & this_item) as alias
	set this_info to info for this_item
	set the current_name to the name of this_info
	if folder of this_info is false and ¬
		alias of this_info is false then
		if the button_pressed is "Suffix" then
			set the character_count to the number of characters of the current_name
			set the new_name to (characters 1 thru -(the 3 + 1) of the current_name) as string
			
			set the new_file_name to the (the new_name & "." & i & prefix_or_suffix) as string
		else
			set the new_file_name to the (the current_name & the prefix_or_suffix) as string
		end if
		my set_item_name(this_item, the new_file_name)
	end if
end repeat
beep 1

on set_item_name(this_item, new_item_name)
	tell application "Finder"
		--activate
		set the parent_container_path to (the container of this_item) as text
		if not (exists item (the parent_container_path & new_item_name)) then
			try

--#################				
duplicate file this_item of folder parent_container_path to file new_item_name of folder parent_container_path with replacing

-- The following line is the original code that works, but simply renames the file and not copy to new name
				--set the name of this_item to new_item_name
			on error the error_message number the error_number
				if the error_number is -59 then
					set the error_message to "This name contains improper characters, such as a colon (:)."
				else --the suggested name is too long
					set the error_message to error_message -- "The name is more than 31 characters long."
				end if
				--beep
				tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
				copy the result as list to {new_item_name, button_pressed}
				if the button_pressed is "Skip" then return 0
				my set_item_name(this_item, new_item_name)
			end try
		else --the name already exists
			--beep
			tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
			copy the result as list to {new_item_name, button_pressed}
			if the button_pressed is "Skip" then return 0
			my set_item_name(this_item, new_item_name)
		end if
	end tell
end set_item_name

Your problem is that the Finder’s ‘duplicate’ command requires a folder as the destination - you can’t change the name of a file as you duplicate it.

The simple fix is to change to a two-step process. Change the line:

duplicate file this_item of folder parent_container_path to file new_item_name of folder parent_container_path with replacing 

to:

set newFile to duplicate file this_item of folder parent_container_path to folder parent_container_path with replacing 
set name of newFile to new_item_name

So here you duplicate the file first, storing a reference to the file in ‘newFile’ which you then change the name of.

Do I need to say ‘set newFile to “file.txt”’ prior to using it or is it just a pointer to the file that I can change with the second command.

Actually, I have tried your code with and without my ‘set’ command and it doesn’t work. I get the same error: Can’t make some data into expected type. Do I need ( as string) around the parent_container_path variables?

set newFile to duplicate file this_item to folder parent_container_path of startup disk with replacing

I removed the first parent_container_path after this_item and added of startup disk to destination file and I get the following error:‘Can’t set folder"root:path:to:file:" of startup disk to file (alias “root:path:to:file:file.1.txt”)’

It seems what I have is trying to replace a folder with a text file and that the source of the previous ‘Can’t make some data into expected type.’

Don’t know if this helps. Thanks,
Andy

I didn’t analyze your script that deeply to determine how you’re setting your variables, but in general, for the code:

set newFile to duplicate file this_item of folder parent_container_path to folder parent_container_path with replacing 
set name of newFile to new_item_name 

newFile doesn’t need to be preset. It will be a pointer to the duplicate file after the command is done
this_item should be the name of the file as a string, e.g. "File.txt
parent_container_path should be a folder path like: “Macintosh HD:path:to:folder:” – note this is the path to the folder, not the file
new_item_name should be a valid file name as a string, e.g. “new filename.txt”

Here is what I got to work:

set newFile to (duplicate file this_item to folder parent_container_path with replacing) as alias
set name of newFile to new_item_name

removing the ‘of folder’ for source file and adding ‘as alias’ to the destination file…

Here is what I got to work:

set newFile to (duplicate file this_item to folder parent_container_path with replacing) as alias
set name of newFile to new_item_name

removing the ‘of folder’ for source file and adding ‘as alias’ to the destination file…

thanks,
Andy