Question Regarding “Retry This Loop” Used in a “Try/Catch” Action

Howdy Folks,

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.

Thanks in advance!
-Chris

A loop is any of the enclosing looping action, For Each, Repeat, While, Until, or failing that the containing macro as a whole.

image

Make sure the AppleScript action is configured not to abort the macro:

image

4 Likes

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
1 Like

@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.

Hmm...

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.

2 Likes

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.

1 Like

I'd put a short delay in the loop to keep AppleScript from running away with itself.

I'd also make certain the loop had a viable exit condition like a counter. You don't want to get caught up in an endless loop.

I usually give these things a 0.1 to 0.25 second delay and use a counter to indicate x number of seconds it can run before exiting.

1 Like

Yep, I usually do both in the on error block. Of course, the other way would be to set a timeout on the action and handle it within the macro.

1 Like

@Nige_S and @ccstone thanks for the additional input.

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).

Thanks again!

1 Like