Copy selected file and then add extensions

Can anyone help me condense this script?

Maybe there is a better solution entirely, but this does work.

What I am doing is copying a selected file that doesn’t have an extension. (We often get PC files that the mac doesn’t recognize.)

This script copies the file 8 times and adds different extensions. Then Photoshop tries to open all. It would be nice if it stopped on the first file it opens.

As you can see the script is cumbersome. I am still going to do so cleanup in the mean time. But some tidbits of help would be very nice.

-Jeff

on open files_
	try
		tell application "Finder"
			try
				set testFileFolder to folder ((startup disk as Unicode text) & "Applications:Utilities:testFileFolder")
				set filesToDelete to every file of folder ((startup disk as Unicode text) & "Applications:Utilities:testFileFolder")
				repeat with i from 1 to (count of items of filesToDelete)
					set the_path_to_delete_1 to (item i of filesToDelete) as string
					if (the_path_to_delete_1 as string) ends with "" then
						do shell script "rm " & quoted form of POSIX path of the_path_to_delete_1
					end if
				end repeat
				update (files of entire contents of testFileFolder)
			end try
			try
				set destPath to folder ((startup disk as Unicode text) & "Applications:Utilities:")
				set foldName to make new folder at destPath with properties {name:"testFileFolder"}
				set thePath to destPath & foldName
			on error
				try
					set thePath to folder ((startup disk as Unicode text) & "Applications:Utilities:testFileFolder")
				end try
				try
					delete entire contents of thePath
				end try
			end try
			duplicate files_ to folder ((startup disk as Unicode text) & "Applications:Utilities:testFileFolder")
			delay 3
			set selFileName to (name of every file of testFileFolder)
		end tell
	on error errMsg
		display dialog errMsg
	end try
	tell application "Finder"
		set thePathNew to folder ((startup disk as Unicode text) & "Applications:Utilities:testFileFolder")
		set selFileFile to (every file of thePathNew)
	end tell
	
	set myFile to 8
	
	tell application "Finder"
		
		repeat myFile times
			
			duplicate selFileFile
		end repeat
	end tell
	
	--delay 8
	
	tell application "Finder"
		set theFldr to folder ((startup disk as Unicode text) & "Applications:Utilities:testFileFolder")
	end tell
	tell application "Finder"
		----
		------
		try -- because alias list won't work if there is only one file.
			set theFiles to (file 1 of theFldr) as alias list
		on error
			set theFiles to (file 1 of theFldr) as alias as list
		end try
		repeat with aFile in theFiles -- this loop simply adds a .jpg extension to copy.
			set theName to name of aFile
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " copy"
			set newParts to (text items of theName)
			set AppleScript's text item delimiters to TID
			set the name of aFile to newParts & ".jpg" as string
		end repeat
		tell application "Finder"
			update files of folder ((startup disk as Unicode text) & "Applications:Utilities:testFileFolder")
		end tell
		----
		----
		try -- because alias list won't work if there is only one file.
			set theFiles to (file 1 of theFldr) as alias list
		on error
			set theFiles to (file 1 of theFldr) as alias as list
		end try
		repeat with aFile in theFiles -- this loop simply adds a .gif extension to copy .
			set theName to name of aFile
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " copy"
			set newParts to (text items of theName)
			set AppleScript's text item delimiters to TID
			set the name of aFile to newParts & ".gif" as string
		end repeat
		------
		------
		try -- because alias list won't work if there is only one file.
			set theFiles to (file 1 of theFldr) as alias list
		on error
			set theFiles to (file 1 of theFldr) as alias as list
		end try
		repeat with aFile in theFiles -- this loop simply adds a .tif extension to copy.
			set theName to name of aFile
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " copy 1"
			set newParts to (text items of theName)
			set AppleScript's text item delimiters to TID
			set the name of aFile to newParts & ".tif" as string
		end repeat
		
		----
		-----
		try -- because alias list won't work if there is only one file.
			set theFiles to (file 1 of theFldr) as alias list
		on error
			set theFiles to (file 1 of theFldr) as alias as list
		end try
		repeat with aFile in theFiles -- this loop simply adds a .png extension to copy.
			set theName to name of aFile
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " copy 2"
			set newParts to (text items of theName)
			set AppleScript's text item delimiters to TID
			set the name of aFile to newParts & ".png" as string
		end repeat
		
		----
		----
		try -- because alias list won't work if there is only one file.
			set theFiles to (file 1 of theFldr) as alias list
		on error
			set theFiles to (file 1 of theFldr) as alias as list
		end try
		repeat with aFile in theFiles -- this loop simply adds a .pdf extension to copy.
			set theName to name of aFile
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " copy 3"
			set newParts to (text items of theName)
			set AppleScript's text item delimiters to TID
			set the name of aFile to newParts & ".pdf" as string
		end repeat
		----
		-----
		try -- because alias list won't work if there is only one file.
			set theFiles to (file 1 of theFldr) as alias list
		on error
			set theFiles to (file 1 of theFldr) as alias as list
		end try
		repeat with aFile in theFiles -- this loop simply adds a .ai extension to copy.
			set theName to name of aFile
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " copy 4"
			set newParts to (text items of theName)
			set AppleScript's text item delimiters to TID
			set the name of aFile to newParts & ".ai" as string
		end repeat
		----
		-----
		try -- because alias list won't work if there is only one file.
			set theFiles to (file 1 of theFldr) as alias list
		on error
			set theFiles to (file 1 of theFldr) as alias as list
		end try
		repeat with aFile in theFiles -- this loop simply adds a .eps extension to copy.
			set theName to name of aFile
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " copy 5"
			set newParts to (text items of theName)
			set AppleScript's text item delimiters to TID
			set the name of aFile to newParts & ".eps" as string
		end repeat
		----
		-----
		try -- because alias list won't work if there is only one file.
			set theFiles to (file 1 of theFldr) as alias list
		on error
			set theFiles to (file 1 of theFldr) as alias as list
		end try
		repeat with aFile in theFiles -- this loop simply adds a .psd extension to copy.
			set theName to name of aFile
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " copy 6"
			set newParts to (text items of theName)
			set AppleScript's text item delimiters to TID
			set the name of aFile to newParts & ".psd" as string
		end repeat
		
		----
		-----
		
		try -- because alias list won't work if there is only one file.
			set theFiles to (file 1 of theFldr) as alias list
		on error
			set theFiles to (file 1 of theFldr) as alias as list
		end try
		repeat with aFile in theFiles -- this loop simply adds a .bmp extension to copy.
			set theName to name of aFile
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " copy 7"
			set newParts to (text items of theName)
			set AppleScript's text item delimiters to TID
			set the name of aFile to newParts & ".bmp" as string
		end repeat
		
	end tell
	---
	----
	tell application "Finder"
		set allFiles to folder ((startup disk as Unicode text) & "Applications:Utilities:testFileFolder")
	end tell
	
	tell application "Adobe Photoshop CS"
		activate
		repeat with i from 1 to number of items in allFiles
			set this_item to item i of allFiles as string
			try
				open file this_item
			end try
		end repeat
	end tell
	
end open

Hi Jeff,

a suggestion for one part of your script:
The following code duplicates every file in your testFileFolder
as many times as the number of items in extensionList and
adds the extensions automatically

set extensionList to {".jpg", ".gif", ".tif", ".png", ".pdf", ".ai", ".eps", ".psd"}
set thePathNew to ((path to utilities folder as Unicode text) & "testFileFolder:")
tell application "Finder" to set selFileFile to (get files of folder thePathNew)

repeat with i in selFileFile
	set {name:Nm, name extension:Ex} to info for (i as alias)
	if Ex is missing value then set Ex to ""
	if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	repeat with j in extensionList
		do shell script "cp " & quoted form of POSIX path of (i as alias) & space & quoted form of ((POSIX path of thePathNew) & Nm & j)
	end repeat
end repeat

Stefan,
This is like magic. You have added a lot of great things in here”and shaved off sooooo many lines, making this script so much easy to alter if need be!

Excellent.

Thank you very much.

-Jeff

PS: this should do it


...
try
	open file this_item
	exit repeat
end try
...

Yep, that did it. Thank you one more time.

-Jeff