This is the script. It won't allow me to set an AppleScript trigger - with this display, when I close the editor the AppleScript trigger disappears.
Honestly I'd rather do without the AppleScript, but I can't figure out another way of doing what I want: in KM, to be aware of if a specific window opens or closes, and if it does either, to send a midi message.
What is an AppleScript trigger? I looked through the complete list of triggers and saw nothing with that name. Can you show the trigger in a screenshot? When I load your macro, I see no trigger.
In that case the original question doesn't make sense to me, because that field is supposed to disappear when you close the editor. It's not a trigger in the normal sense of the word.
You have set your macro group to "Available when the focused window contains "Key Editor:"
It sounds like you're now looking for a way to run your macro when the macro group becomes available.
As far as I know there is no way to autorun a macro when a macro group becomes available.
Deselect the group "Available when a focused window title contains" Macro Group option.
Use the macro trigger "the focused window title changed" instead. Then, with an If/Then action, select "Front Window Condition" to check if the title of the front window is "Key Editor:". If you want to check if any window title of the front application contains "Key Editor:" select "Any Window Condition". Then decide what you want to do.
(Alternatively, use the text token "%FrontWindowName%" to check if the window with the title is front or the text token "%WindowName%All%" if any window title of the front application contains title)
If you want your macro to run only once when the window is opens or closes, you'll need to find some way to track its status yourself. KM will not detect whether a specifically titled window gains or loses focus because it open or closes or because it comes to the foreground or goes to the background. It only knows that a window or titled window focus is changed.
If, however, the focused window changes/title changes/title changed triggers are good enough, you might want to check out this post:
The trouble is, it’s not always the focused window. I’m going to wrap my semi-feverish brain around these posts and see what I can figure out. Maybe setting a variable when a window opens containing Key Editor.
If it's the open and closed status of a particular window you are looking for, I believe @Airy has been quite clever at checking menu items to determine status of otherwise unscriptable things (Firefox text fields!). He might have something up his sleeve to use when the focused window trigger fires.
It might help to confirm or reclarify under what conditions you want the MIDI action to run and not run.
I presume you have an Apple Silicon based Mac. I presume Cubase is always the frontmost app, but the window you want to locate is not always the focussed window. I presume that the window is never partially covered when it appears. I can't test any of these presumptions because I don't have your app. And I can't look for any menu item tricks for the same reason.
But I do have a simple solution. It's a horrible solution if you don't like using your CPU, but it's a solution that should work nonetheless. Give it a try, then measure how much CPU it uses. Until someone comes up with a better solution, it's your only solution.
At the top level, my solution looks like this, but you will have to replace the word "Finder" with "Cubase" in the trigger and in the loop.
Sorry to send people barking up the wrong tree on checking menu item. (It turns out a window title continues to exist in the "Open Recent" menu for many applications.)
I do hope that it is indeed the opened/closed events that you want to trigger your action.
If so, what we need--but DO NOT have--is Window Title Will Change and Window Title Did Change, so that we can see a change in window status.
So:
Yes!
That way we can detect the change in a target window status by comparing its before-and-after status when "the focused window title changes"
When the "focused window title changes" trigger fires:
We check if the value of a WindowStatus variable is empty or "open".
Then, use the result of an AppleScript that returns true if the title-to-watch is among any of the titles of the target application's windows to check the window's status after the "focused window title changes event fires". If it isn't there, the script lets us kmow by erroring.
We can detect a titled window "Did Open" event if we see empty variable plus a true result. We then set the WindowStatus to "open"
We detect a titled window "Did Close" event if we see "open" variable plus an error result. We then set the WindowStatus to "" (empty).
We detect No Change in Titled Window Status if we see "open" variable plus true result. Leave the WindowStatus variable alone.
(See the macro screenshot in the following post.)
Here is a macro to demo this with the TextEdit application.
Enable the macro group and the macro.
Make a text document with the name "Key Edit" in the title.
Open the document in TextEdit. The "Blow" sound plays.
Open or switch to another document. No sound plays.
Close the document. The "Submarine" sound plays.
If this works for you , change Macro Group option "available in these appications" from TextEdit to your application.
In the AppleScripts replace "TextEdit" with the name of your application.
Here is a simpler more complete demo without any AppleScript. This version clears the WindowStatus variable when TextEdit quits. It plays a sound when a named window in TextEdit opens or closes.
Change TextEdit in Macro Group's "Available in these applications" to your application.
Change the TextEdit in the macro's "This application Quits" trigger to your application.
Replace the text in the variable "localWatchedTitle" to the text in the title of the window that you want to trigger your action when it opens and closes.
I am not sure I understand what you are trying to do since Step-Input is a button and not a window. What window in Cubase 14 are you trying to have it be aware of and then send this MIDI message. I presume that MIDI message is something you have programmed as a MIDI Remote?
If you are just trying to have it send that MIDI message once when the Key Editor is opened I often will make hybrid commands and label my groups as such. Use the same command you use to open the Key Editor in Cubase (if you use one) then when you do that assign the same keyboard shortcut in Keyboard Maestro for that macro and do a pause until.
I have a virtual control surface which has a button that toggles Step Input on and off. I want to ensure that the button status on the control surface reflects the status of Cubase. So I now have a KM Macro that on opening the window "Key Editor" looks to see if the Step Edit button is on or off (with Found Image), and depending on its state it sends a MIDI command out. The challenge here is getting the virtual control surface to see the MIDI out from KM. Sure wish I could dedicate it to a port. I could conceivably create a Generic Remote or write a script for MIDI Remote that receives the message from KM and sends it to the control surface, but that hasn't worked yet.
Actually here's my issue. I would like to be able to set a sort of loop that when a specific window is open, looks to see if the "on" image is found or the "off" image is found and when one of them is found, it sends a MIDI message corresponding to the on/off state(say Ch.16 CC101 value 2 for on); then waits until the other image is found and then sends a different MIDI message corresponding to that state (say Ch.16 CC101 value 1 for off), then waits until the on image appears again and then restarts the loop. Does that make sense?