AppleScript behaves strangely when certain actions are present in the KM Editor

Not always, but often, an Applescript fails (German error message “Keyboard Maestro” hat einen Fehler erhalten: Das Programm läuft nicht.), when the following actions are present in the editor:

Set Variable to Front Browser
Set Safari Field to Text

Interesting: The same setting causes problems in ScriptDebugger when calling up the dictionaries for Keyboard Maestro. The display flickers constantly.

The script terminates as soon as it reaches this statement:

**tell** *application* "Keyboard Maestro"
	**set** theSelection **to** (**get** **the** selection)
	…
**end** **tell**

The action is not executed; it exists only in the editor.

Actions in the editor will potentially be running some level of scripts for displaying their results or condition values.

For example, the Set Safari Field to Text action shows the current value of the field at the bottom of the action when displayed in the editor. To get that requires running an AppleScript.

Most scripts in Keyboard Maestro that refer to web browsers include a clause to check if the browser is running to avoid launching them.

There may be more concise ways of solving this, but try:

tell application "Keyboard Maestro"
	set flag to false
	repeat until flag
		try
			set theSelection to (get the selection)
			set flag to true
		on error
			-- do nothing
		end try
	end repeat
	
	-- do stuff with selected Actions
	
end tell

I see. Could it be that there are side effects causing

- my AppleScript to terminate with an error and

- ScriptDebugger to malfunction as well?

That identifies the problem, but doesn't solve it. I want my AppleScript to run even if one of the mentioned actions is open in the editor.

Did you actually try it?

The problem seems to be that if the Editor is busy evaluating a Token using AppleScript (so it can preview the result in an Action), the Editor will be unresponsive to incoming AppleScript commands.

The snippet I posted is, quite simply, "No response? Keep trying until successful". Works 100% here.

FYI @ArturAs I also asked this question in this thread and received a similar answer.

I need to apologize. I didn't read your script carefully and missed the loop.

Thanks for the tip!

The problem remains: I have a lot of scripts that very often call “tell application ‘Keyboard Maestro’”. Adding your query to every single one of them is very time-consuming.

The solution remains, too -- at least until a "Do not evaluate AS-dependent Tokens" toggle is added to the Editor's preferences. You might be able to get close to that by collapsing any Actions that contain those Tokens and turning off "Evaluate Condition Results" -- no preview of evaluations, no need to query an app, nothing blocking your AS from interacting with the Editor.

That seems to be the best way for now.

Thanks again!

Maybe you could consider adding a short delay to these scripts to allow other scripts to run?

They are run every second I believe (or more likely, there is a second gap between them).