Given the applescript below in a Run Applescript action, the next action in the sequence runs before the applescript is completed and my variable is set to null. Is there a way to make sure that the applescript has completed before moving to the next action?
thanks.
tell application "Evernote"
tell application "Keyboard Maestro Engine"
make variable with properties {name:"xSelect", value:the clipboard}
end tell
end tell
AppleScripts run synchronously, so your problem is likely something else.
An easier way to do such a thing would be to just return “the clipboard” from the Execute AppleScript action and have it stored in a variable.
But regardless of that, the AppleScript executes synchronously, and so it will have completed by the end of the Execute AppleScript action and before the next action runs, unless:
- You have configured a short timeout and to not abort the macro for that action.
or
- You have explicitly configured the Execute AppleScript action to run asynchronously.
That’s interesting, because if I put a notification after the script, tack a 5 second delay in the script just to make things obvious, the notification pops up immediately.
I was, in fact, stuffing the clipboard into a KM variable. The question originated because the variable was often, but not always, coming back empty. Putting a delay after the script action fixed it.
Is it possible that the tell statements are the issue rather than the script itself. They don’t run synchronously, I believe.
Sorry, you’re right. I misread my notifications.
It has been my experience with many scripting tools that many mysteries may be solved by inserting a very short Pause (0.2 seconds, e.g.) between the problematic steps. This was much more the case with QuicKeys than KM, and maybe I qm deluded, but it does seem to help sometimes in KM too. I’m sure it’s not supposed to, but it’s one of the first things I try when things when steps don’t execute or things happen out of sequence.
Of course, if you need a meaningful pause there are plenty of options in Pause Until.
Pauses are indeed necessary in various places.
When Keyboard Maestro can, it waits for the action to complete before progressing - but it does not always know to wait.
For example, if you use the Copy File action, Keyboard Maestro will copy the file and the macro will not progress until the copy is completed. So there would be no need for a pause after that before accessing the destination file.
On the other hand, if you’re in the Finder with a file selected and a macro typed Command-D or selected the File ➤ Duplicate menu, Keyboard Maestro does not know that that process leads to copying a file/folder, or have any idea how long the process will take. So you will have to figure out how to wait until the duplication is completed before accessing the destination file.
The most common places you need a pause are:
- After doing any action that changes the keyboard focus (especially things like opening a new window/dialog or switching applications or the like).
- After doing any action that causes some sort of animation.
- Before any action that is going to look for an image on the screen (using a Pause Until the image appears on the screen is a good solution here).
- Periodically if you are going to type lots of text. In particular, at the end of a long loop to ensure the system does not get further and further behind. The event queue is only so big and will eventually fill up and lose events.