Duplicate and Myfile

Trying to have the following code change the name of a file in a chosen folder and then “duplicate” the newly named file to a new directory. There are jpg and txt files in the directory and I want to “duplicate” the txt files while renaming and duplicating the jpg files. All the code works fine for what I want to do except the “duplicate” command in the second “repeat” loop. The first “repeat” loop “duplicate” works fine. However, I want to use “duplicate” in the second “repeat” for both branches of the “if” statement and I want to use “myfile” instead of the clumsy (item i of a_list) and the incrementing of “i” while eliminating the “duplicate” in the first “repeat” which simply determines the variable I need to change the names of the jpg files? Any other pointers on code efficiency would be greatly appreciated. Needless to say, I am a rookie at this.

set this_folder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "System Events"
	set a_list to every file of folder this_folder
end tell

tell application "Finder"
	set destinationfolder to ((path to desktop as Unicode text) & "aaa:") as alias
	set extension_sep to "-"
	set word_end to ""
	set i to 0
	repeat with myfile in a_list
		set i to i + 1
		set n to name of myfile
		set file_extension to last word of n
		if file_extension = "txt" then
			set word_end to word 1 of n
			--this duplicate works okay, but I want it in the next loop while using myfile
			duplicate (item i of a_list) as alias to folder destinationfolder with replacing
		end if
	end repeat
	set i to 1
	repeat with myfile in a_list
		i = i + 1
		set n to name of myfile
		set the file_extension to the last word of n
		if the file_extension is "txt" then
			--duplicate something here using myfile
		else
			set n to (text 1 through ((length of n) - 4) of n) & extension_sep & word_end & ".jpg"
			set name of myfile to n
			--this duplicate always gives me an error 
duplicate file (item i of a_list) to folder destinationfolder with replacing
		end if
	end repeat
end tell

Model: iMac G-4
AppleScript: 2.2.1 (100.1)
Browser: Firefox 3.0.4
Operating System: Mac OS X (10.5)

Would you mind posting a sample of file names and an example of the finished result?

Cheers,

Craig

This is the original format of a directory of files I am dealing with:
76-email.txt
154328 TA-4AR 30SEP06 Argentina C-901 Area Material Río Cuarto Horacio J. Clariá DSC_3272.jpg
154328 TA-4AR 30SEPG06 Argentina C-901 Area Material Río Cuarto Horacio J. Clariá DSC_3256.jpg
158165 A-4AR 01OCT06 C-917 Area Material Río Cuarto Horacio J. Clariá DSC_3522.jpg
158178 A-4AR 30APR06 C-907 BAM Río Gallegos Horacio J. Clariá DSC_0400.jpg
158178 A-4AR ddMAY06 C-907 158429 C-910 BAM Río Gallegos Horacio J. Clariá DSC_1231.jpg
158429 A-4AR 01MAY06 C-910 BAM Río Gallegos Horacio J. Clariá DSC_0599.jpg
158429 A-4AR 30APR06 C-910 BAM Río Gallegos Horacio J. Clariá DSC_0329.jpg
159472 A-4AR 30APR06 C-915 159470 C-923 159483 C-933 BAM Río Gallegos Horacio J. Clariá DSC_0271.jpg
159472 A-4AR 30SEP06 Argentina C-915 Area Material Río Cuarto Horacio J. Clariá DSC_3280.jpg
159483 A-4AR 04MAY06 Argentina C-933 Horacio J. Clariá DSCN0016.jpg
159486 A-4AR 02JUN06 C-934 over Posadas city outskirts Horacio J. Clariá DSC_1518.jpg

This is how I would like it to look, and it does once I run the program above, but I need to be able to duplicate the following files into another directory which will be on the desktop, but not in the same directory of the original files:

159486 A-4AR 02JUN06 C-934 over Posadas city outskirts Horacio J. Clariá DSC_1518-76.jpg
159483 A-4AR 04MAY06 Argentina C-933 Horacio J. Clariá DSCN0016-76.jpg
159472 A-4AR 30SEP06 Argentina C-915 Area Material Río Cuarto Horacio J. Clariá DSC_3280-76.jpg
159472 A-4AR 30APR06 C-915 159470 C-923 159483 C-933 BAM Río Gallegos Horacio J. Clariá DSC_0271-76.jpg
158429 A-4AR 30APR06 C-910 BAM Río Gallegos Horacio J. Clariá DSC_0329-76.jpg
158429 A-4AR 01MAY06 C-910 BAM Río Gallegos Horacio J. Clariá DSC_0599-76.jpg
158178 A-4AR ddMAY06 C-907 158429 C-910 BAM Río Gallegos Horacio J. Clariá DSC_1231-76.jpg
158178 A-4AR 30APR06 C-907 BAM Río Gallegos Horacio J. Clariá DSC_0400-76.jpg
158165 A-4AR 01OCT06 C-917 Area Material Río Cuarto Horacio J. Clariá DSC_3522-76.jpg
154328 TA-4AR 30SEPG06 Argentina C-901 Area Material Río Cuarto Horacio J. Clariá DSC_3256-76.jpg
154328 TA-4AR 30SEP06 Argentina C-901 Area Material Río Cuarto Horacio J. Clariá DSC_3272-76.jpg
76-email.txt

Since I’ve already sent the code with the “76” attached to the end of the “jpg” files, I’ll keep it that way for now, but the final product will have the “76” at the front of the file name as it is with the “txt” file.

Thanks for taking a look Craig.

I made one small change so that you will choose the txt file instead of the folder containing it.
Let me know how this works for you.


set destinationFolder to alias ((path to desktop as Unicode text) & "aaa:") 

-- Choose the text file to parse the name and also get the parent directory
set textFileToProcess to (choose file with prompt "Pick the the text file:") as alias
tell application "Finder"
	set folderToProcess to parent of textFileToProcess as string
	set theFiles to every file of alias folderToProcess
	
	-- Get number from name to prepend to new name
	set textFileName to name of textFileToProcess
	set prefixName to item 1 of my tidStuff("-", textFileName)
	
	repeat with i from 1 to count of theFiles
		set thisFile to item i of theFiles
		set theName to name of thisFile
		set extName to text -3 thru -1 of theName
		if extName is "jpg" then
			set newName to (prefixName & "-" & theName) 
			set name of thisFile to newName
		end if
	end repeat
end tell

-- Copy all the files in the originating folder to the destination folder
set theCommand to quoted form of POSIX path of folderToProcess & space & quoted form of POSIX path of destinationFolder
do shell script "cp -R " & theCommand

on tidStuff(paramHere, textHere)
	set OLDtid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to paramHere
	set theItems to text items of textHere
	set AppleScript's text item delimiters to OLDtid
	return theItems
end tidStuff

Have a Merry Christmas!

Craig

Craig,

FNA, bubba, it woiked right out of the box!

Do you have a link I can visit to learn more about shell commands? It also works from a remote directory on my laptop (where the files are stored and now negates the need to copy them to the desktop) and properly copies to the desktop from which I am running the program now. Way Cool!

Watering my eyes, Craig, and thanks a bunch.

Feliz Navidad to you.

Gene

You are very welcome. :slight_smile:

I started with a book, “Unix for Mac OS X Tiger” by Dave Taylor. There
may be one for Leopard now. Doesn’t matter though, the commands work
in both.

Here is a pretty good pdf / tutorial: http://steve-parker.org/sh/sh.shtml

Other than that do a search here on MacScripter for shell and start
looking at other peoples’ code. Performing shell scripts from AppleScript
can sometimes be much harder than doing them from Terminal or simply
writing a shell, perl, python, ruby, etc script and calling that
script from AppleScript.

When handling a lot of files or items in a repeat loop this would be preferable
over putting a do shell script inside an AS repeat loop. Each do shell script
call spawns a new shell instance. In a repeat loop this can be costly, requiring
more memory and time to perform.

Cheers,

Craig

Craig,

Just in case you are interested in seeing how your help has been used, take a look at this web site: a4skyhawk.org/ We are a group who worked on, built, model, are interested in or flew the plane you see on our home page.

We have literally thousands of photo files sent to us that will need to be uploaded and each will be given an individual page along with a thumbnail directory once the files have been standardized for the web. I’ve been automating the processing of the photos and I estimate your help just saved us about 24 man hours of work renaming the photos. I already have scripts written (taken from examples on macscripter.net) to work with the resizing, watermarking and thumbnailing of the photos and my next task will to be to find a way to automate the production of the HTML files using applescript to place the photos into the HTM file along with some text fields.

Wish me luck and, sincerely, thanks ever so much!

Gene