Applescript fails in KM when using variable to specify application

I originally tried using tell application "System Events" to set frontApp to name of first application process whose frontmost is true to define the frontApp variable, but when I run just this script from KM the result displayed is firefox, which is why I then tried using KM to define the varable instead (the result of which is Firefox). Both methods of defining the variable result in the same error when running the full script in the macro. However, Script Editor executes the script as expected when passing the frontApp variable defined in KM.

The full script
tell application "Keyboard Maestro Engine"
	set windowNamed to getvariable "Search"
	--	set frontApp to getvariable "frontApp"
end tell

tell application "System Events" to set frontApp to name of first application process whose frontmost is true

tell application frontApp
	if window windowNamed is miniaturized then
		set miniaturized of window windowNamed to false
	else
		return
	end if
end tell
The error

If I hardcode the application to be "Firefox" then the script executes as expected when running the macro with the script. I'm wondering if this is a quotation mark issue because when executing just tell application "System Events" to set frontApp to name of first application process whose frontmost is true from Script Editor the result is "Script Editor" (with quotes), but the KM results lack the quotation marks. I tried adding quotation marks to the KM variable, but, while the script executed, I don't understand what it did because it launched a dialogue box to select an application...

Even though it's working with the hardcoded application name, I'd much rather a general solution to this because I regularly use three different browsers. I expect I'm missing something painfully obvious, but I just can't seem to figure it out.

macro image

R - find browser window with word.kmmacros (11.8 KB)

This is entirely an AppleScript issue.

AppleScript terminology is evaluated at compile time.

The issue is that AppleScript gets its language definition by reading the dictionary from the application at compile time, which doesn't happen with variable named application.

So you have to tell AppleScript what language definitions to use.

This (unresolved) Stack Overflow covers exactly the same issue.

So you need to tell it what terms to use.

tell application "Keyboard Maestro Engine"
	set windowNamed to getvariable "Search"
	--	set frontApp to getvariable "frontApp"
end tell

tell application "System Events" to set frontApp to name of first application process whose frontmost is true

tell application frontApp
	using terms from application "Safari"
		if window windowNamed is miniaturized then
			set miniaturized of window windowNamed to false
		else
			return
		end if
	end using terms from
end tell

So the terms you use has to have the same expectations as the terms of the target application (for example, the Finder does not seem to have miniaturized in its windows).

3 Likes

Marvellous, cheers!

As a less critical semi-related question, I couldn't seem to pass local variables from KM to the script. Is it not possible to pass local variables, or did I miss something else?

See: Execute an AppleScript

2 Likes

A footnote – both of these are much easier in the JavaScript flavour of osascript

Application(kmvar.local_AppName).activate()