AppleScript - check if file exists, return dialog, continue repeat

I have a script that takes selected cells in Excel, converts the numbers in those cells to a file path (HFS), then copies the matching text files in a folder to another folder.

I currently have two issues I am trying to overcome:

  1. Check if the file exists to begin with, and if not, both alert me which file or files don’t exist, and then continue the loop with the remaining files (the text file may not yet exist, even though the file number in the Excel workbook does);
  2. Process if only one cell is selected (currently, if only one cell is selected, nothing happens). Admittedly, I have not started looking deeper in to this - I know it has something to do with my “repeat with” language…but I figured I’d toss it in the pile anyway.

With respect to the file existence checking, I’ve tried numerous variations on the current theme, but nothing will tell me if the file does not exist - I get only an error (“File Macintosh HD:~:27882AMSQUERY.in wasn’t found.” number -43 from “Macintosh HD:~:27882AMSQUERY.in”, for example), and the script stops running at that cell. I can get AS to dialog me when a file does exist, but that’s not helpful.

Any insights the old hands can give is greatly appreciated. TIA

tell application "Microsoft Excel"
	set theCells to value of (get selection)
	--problem - does not run if only one item is selected
	repeat with eachFile in theCells
		set fileNum to my number_to_string(eachFile)
		set filePath to "Macintosh HD:Users:bob:Documents:AMS Query Holding File:" & fileNum & "AMSQUERY.in" as alias
		
		tell application "Finder"
			if not (exists file filePath) then --is not producing results
				display alert "File Number " & fileNum & " does not have an existing AMS query."
				exit repeat
				
			else
				copy file filePath to folder "Macintosh HD:Users:bob:CBCCSYS:MQ:IN"
			end if
		end tell
	end repeat
end tell

--Subroutine to force the extracted file# to display properly
--Source http://www.macosxautomation.com/applescript/sbrt/sbrt-02.html
on number_to_string(this_number)
	set this_number to this_number as string
	if this_number contains "E+" then
		set x to the offset of "." in this_number
		set y to the offset of "+" in this_number
		set z to the offset of "E" in this_number
		set the decimal_adjust to characters (y - (length of this_number)) thru ¬
			-1 of this_number as string as number
		if x is not 0 then
			set the first_part to characters 1 thru (x - 1) of this_number as string
		else
			set the first_part to ""
		end if
		set the second_part to characters (x + 1) thru (z - 1) of this_number as string
		set the converted_number to the first_part
		repeat with i from 1 to the decimal_adjust
			try
				set the converted_number to ¬
					the converted_number & character i of the second_part
			on error
				set the converted_number to the converted_number & "0"
			end try
		end repeat
		return the converted_number
	else
		return this_number
	end if
end number_to_string

Hi,

coercing a file path to alias assumes that the file exists anyway, if not, an error will be thrown. Just remove the coercion.

The proper command to copy a file in the Finder is duplicate, not copy.
For portability relative folder paths are used
Any cases of selection in Excel are considered (single cell, multiple cells horizontally, vertically or both).


set queryHoldingFilesFolder to (path to documents folder as text) & "AMS Query Holding File:"
set inFolder to (path to home folder as text) & "CBCCSYS:MQ:IN:"

tell application "Microsoft Excel" to set theCells to value of cells of (get selection)

repeat with eachFile in theCells
	set fileNum to number_to_string(eachFile)
	set filePath to queryHoldingFilesFolder & fileNum & "AMSQUERY.in"
	
	tell application "Finder"
		if (exists file filePath) then
			duplicate file filePath to folder inFolder
		else
			display alert "File Number " & fileNum & " does not have an existing AMS query."	
		end if
	end tell
end repeat
.

Thank you! That seems to be perfect (today’s selection of files didn’t have any to test the dialog for non-existing queries, but it will come soon enough!) I can see the code is cleaner, and some ways it is better than what I had cobbled together. I don’t necessarily understand the differences, yet, but I’ll get there. Thank you so much for the assist!