I have a script that worked before, but seems to get stuck now. The sole purpose of the script is to take a 6 digit job number, look in one network folder for that job and copy it to my desktop. If it that file isn’t present, then the script is to look into a second location, where the file should be located (if it’s not in the first location).
I am using a TRY block for the first location and ON ERROR to look in the scond location. Apparently, if the file isn’t found in the first location, my script just hangs and doesn’t immediately respond to the situation. Any suggestions on how better to achieve this and make it more fool proof. obviously, i’m the fool here.
tell application "Finder"
try
-- display dialog "gonna look in John's folder"
set ThisJob to "Office:1_Docs:JOHN:Public:Backup:template:" & JobNum & ".jlt"
copy file ThisJob to desktop
display dialog "ok"
on error
display dialog "not in Johns"
set ThisJob to "Jobs:4colors:template:" & JobNum & ".jlt"
copy file ThisJob to desktop
end try
end tell
Script code formatting tags added by NG. Markdown Reference (here).
dklucznik. Duplicate is the correct command to copy a file in a Finder tell statement. I would recommend a different approach than the one used in your script. Please note that the paths in my script are to files on my computer, and you will need to change these.
set jobNumber to 123456
tell application "Finder"
set fileOne to "Store:Test:Folder One:" & jobNumber & ".txt"
set fileTwo to "Store:Test:Folder Two:" & jobNumber & ".txt"
if (exists file fileOne) then
duplicate file fileOne to desktop -- with replacing
display dialog "OK"
else if (exists file fileTwo) then
duplicate file fileTwo to desktop -- with replacing
display dialog "File not in Johns"
else
display dialog "File not found"
end if
end tell
BTW, the script should be edited to deal with the situation where the target file exists. One option is to check if the target file exists and to stop the script if that is the case. A second option is to use the duplicate command’s “with replacing” option, which will overwrite the target file.
tell application "Finder"
set ThisJob to "Office:1_Docs:JOHN:Public:Backup:template:" & JobNum & ".jlt"
try
--display dialog "gonna look in John’s folder"
duplicate file ThisJob to desktop --destinationFolder
display alert "ok"
on error errMsg
display dialog "not in Johns"
set ThisJob to "Jobs:4colors:template:” & JobNum & “.jlt"
duplicate file ThisJob to desktop
end try
end tell
Using the exists verb, your script could be more compact and yet more explicit on errors:
property folder1 : "Office:1_Docs:JOHN:Public:Backup:template:"
property folder2 : "Jobs:4colors:template:"
tell application "Finder"
set userAnswer to text returned of (display dialog "Please, enter the 4 digits job number:" default answer "" with title "Copy Job File" with icon 1 giving up after (5 * minutes))
if userAnswer = "" then return
set theFile to (folder1 & userAnswer & ".jlt")
if not (exists theFile) then set theFile to (folder2 & userAnswer & ".jlt") as «class furl»
if not (exists theFile) then display alert "Job «" & userAnswer & "» was not found in both folders." message "Make sure the job number is correct." buttons {"Cancel"} cancel button 1
if (exists ("" & desktop & userAnswer & ".jlt")) then display alert "Job «" & userAnswer & "» is already on the Desktop." message "Choose another job number." buttons {"Cancel"} cancel button 1
duplicate theFile to desktop
end tell
The problem with try/on error/end try blocks is that when you do not report the error message, you have no idea what went wrong. If an error occurrs for an unexpected reason, you don’t realise what has happened.
I would write it like this and have a good look at the errors.
tell application "Finder"
try
-- display dialog "gonna look in John's folder"
set ThisJob to "Office:1_Docs:JOHN:Public:Backup:template:" & JobNum & ".jlt"
copy file ThisJob to desktop
display dialog "Johns ok"
on error errortext1 number errorno1
--display dialog "not in Johns"
try
set ThisJob to "Jobs:4colors:template:" & JobNum & ".jlt"
copy file ThisJob to desktop
display dialog "Not in Johns, Alternate ok"
on error errortext2 number errorno2
display dialog "Something went wrong!" & return ¬
& "Error 1: \"" & errortext1 & "\"(" & errorno1 & ")" & return ¬
& "Error 2: \"" & errortext2 & "\"(" & errorno2 & ")"
end try
end try
end tell