How to pass KM variable to AppleScript?

As incredible as it seems, I read the documentation and forum discussions pertaining to this topic, and it's still not clear.

In the example below, I have the variable KM_input_URL which I want to pass on to the AppleScript below ie replace "https://forum.keyboardmaestro.com/" with the variable KM_input_URL (part of a prompt for user input).

thank you very much

PS: the AppleScript opens the URL, in the same tab (to avoid tab duplications)

KM Variable KM_input_URL

set theURL to "https://forum.keyboardmaestro.com/"

tell application "Google Chrome"
	tell front window
		set URL of active tab to theURL
	end tell
end tell

You need to use the KM Engine to get the variable into AppleScript.

But what you are trying to do with the AppleScript is possible with just native KM actions.

tell application "Keyboard Maestro Engine"
	if variable "Serveraddress" exists then
		set volumeToMount to value of variable "Serveraddress"
	end if
end tell

tell application "Finder"
	mount volume volumeToMount
end tell

1 Like

thank you very much. Which actions are you referring to ?

This is "old style" and the script will error if the KM variable Serveraddress doesn't exist.

New style is:

      set volumeToMount to getvariable "Serveraddress"

...which will either get the value of of Serveraddress or set volumeToMount to an empty string if the variable doesn't exist.

4 Likes

Thanks. Now I learned something new :slight_smile:

1 Like

I edited your topic title since clearly no one is an idiot if they are using Keyboard Maestro.

Replace your first line with:

tell application "Keyboard Maestro Engine"
	set theURL to getvariable  "KM_input_URL"
end tell
6 Likes

Thank you very much @peternlewis !

I frequently need these reminders and always good to read posts like this as many times as I have set values in AppleScript based on variables in Keyboard Maestro. Thank you.

Thanks for making us all feel better about ourselves today Peter. :slight_smile:

2 Likes

You have, I hope, made yourself a Favourite for this?

image

set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set varName to getvariable "Local_varName" instance inst
end tell

(*
do stuff here
*)

tell application "Keyboard Maestro Engine"
	setvariable "Local_varName" instance inst to theResult
end tell

It's a lot quicker to remove what you don't need than to try and remember what to add!

3 Likes

I was thinking about the action "Set Google Chrome URL".
With this you set the URL of the front window in Chrome.

1 Like

@JimmyHartington's solution shows that this was more a question about an attempted solution than about the actual problem (see XY Problem), but on the use of Keyboard Maestro variable values, it is, FWIW, easier in Execute JavaScript for Automation actions than in Execute AppleScript Actions.

For JXA, all you need is a prefix: kmvar.


Or, if you do want a scripted alternative to a Set Google Chrome URL action:

Keyboard Maestro variable values in script actions.kmmacros (2.2 KB)

2 Likes