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:
- 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);
- 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