How to do `If this macro was recently ran (< 2 seconds ago)` do one thing, if not, do another thing

I still don't understand what you are trying to do.

Anything from AppleScript will generally not have the macro context (so no local or instance variables or tokens relating to the macro instance). Most AppleScript commands allow you to pass a macro instance, and that will get you access to local and instance variables, and perhaps in the future macro instance tokens.

You can execute an AppleScript action with XML, but it wont have access to any macro instance information.

But I don't really understand what you are trying to do so I don't know if I have answered your question.

Hello Peter

I mean something like this

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions


tell application "Keyboard Maestro Engine"
	
	set kmInst to system attribute "KMINSTANCE"
	
	set actionXML to "<dict>
		<key>ActionUID</key>
		<key>MacroActionType</key>
		<string>SetVariableToText</string>
		<key>Text</key>
		<string>%ExecutingThisMacroUUID%</string>
		<key>Variable</key>
		<string>Local__MacroUUID</string>
	</dict>"
	
	do script actionXML
	
	set macroUUID to getvariable "Local__MacroUUID" instance kmInst
end tell

tell application "Keyboard Maestro" to tell macro id macroUUID to return used date as string

If I were able to use something from this from my iPhone while posting here inn the last days I've had it done like this - but this wasn't possible.

hope this makes everything clear now. Even though the fact that I need many Macrosf programmed like this because I want to reduce the whole amount of macros I have keeping as much as possible of their Functionallity while make them accessible for Older KM Versions like KM 8 & 9 which I still use in VM's with older Versions of MacOS....

Greetings from Germany

Tobias

No, that would not work, the action would run, but it would not have access to the instance specific tokens.

I'm not sure how to make this entirely clear without going in too deep into the details of Keyboard Maestro, but basically:

Keyboard Maestro runs multiple macros all at the same time, slicing time between each of the running macros. This includes all the macros and sub-macros/subroutines, etc. When Keyboard Maestro is specifically executing one of those slices of macro, the code knows what is happening, and can access local/instance specific things like variables and execution instance tokens.

But when outside of Keyboard Maestro, the current instance is not known. And when AppleScript executes things within Keyboard Maestro, the current instance is still not known unless it is explicitly passed. But explicit passing of instance state only gives access to variables, and in any event the do script action has no instance parameter so there is definitely no way for Keyboard Maestro to know which instance is which when executing actions like that - the do script command effectively starts its own new instance.

So no, executing an action via AppleScript will not have any access to the instance specific variables or tokens.

2 Likes

Thanks for clarifying this… now I am understanding this a little better.

Greetings from Germany

Tobias