I have a question regarding the retry loop action. What constitutes a “loop”? How does the executing macro determine where to restart?
What I would like to do is execute an AppleScript, and if it fails attempt to execute it again.
But in some cases, it’s restarting the entire macro from the beginning. I’ve read this post but it’s still not 100% clear for me. Just hoping somebody can shed some light on it for me.
Could you not put the loop into the AS itself? Set a flag before, set the flag to something else on success, loop through the routine until the flag is that "before" value:
set output to "Failed"
set x to 0
repeat while output is "Failed"
set x to x + 1
if x = 5 then
set output to x
else
display dialog "Still failing" giving up after 1
end if
end repeat
display dialog "Completed: x=" & x
@peternlewis Excellent, thank you for the clarification. I’ve adjusted my macro and it appears to work as expected now.
@Nige_S Unfortunately I’m not sure that would work since the AppleScript is failing with error -600, indicating that the application isn’t running. The application in question is Typinator which is always running. Plus, it’s been happening on multiple devices all of the sudden for about a month now where it literally never happened before. So I believe it’s a bug from one of the more recent OS updates. Therefore, I wouldn’t be sure how to embed anything in the AppleScript itself to accomplish what I need since the AppleScript is the issue.
Sounds like a system error, but there's a small possibility its Typinator's fault.
AppleScript can check to see if an app is running, and you can also ask System Events for a double-check:
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/12/20 11:04
# dMod: 2022/12/20 11:04
# Appl: Calculator, System Events
# Task: Check whether an application is running using its name.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Calculator, @System_Events, @Check, @Whether, @Application, @Running
--------------------------------------------------------
tell application "Calculator"
if (it is running) = false then
set appName to its name
return doubleCheckIfAppIsRunning(appName) of me
end if
end tell
--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on doubleCheckIfAppIsRunning(appName)
tell application "System Events"
try
name of first process whose name contains appName
return "Application " & appName & " is running!"
on error
return "Application " & appName & " is NOT running!"
end try
end tell
end doubleCheckIfAppIsRunning
--------------------------------------------------------
Keyboard Maestro also has the %Application%Background% token.
If I were you I'd rebuild the LaunchServices database with Onyx or a similarly appropriate tool.
It's not uncommon for that to get scrambled after a system update.
If it always fails, retrying the loop is unlikely to help. If it fails once or twice but gets there eventually then a retry in KM will work -- but you can also handle that in the AppleScript. Riffing off the previous:
set output to "Failed"
set x to 0
repeat while output is "Failed"
try
tell application "Typinator"
--do something
set output to "Succeeded" -- or the result of doing something
end tell
on error
--do nothing
end try
end repeat
return output
So if the call to Typinator fails an error is thrown, output remains "Failed", and the loop repeats. If Typinator does its thing then output is set to something other than "Failed" and the loop exits.
The AppleScript generally only fails once, so the retry has been working just fine for now. I think I’ll leave it as is for the time being but I am bookmarking yall’s responses because they will be very handy for some other scripts I have (and will no doubt write in the future).