Regex for macro names? looking for a way to launch a conflict palette macro when input language is not EN

Hi all

im looking for a way without duplicating many of my macros :slight_smile: to launch a conflict palette macro when input language is not EN.

ie lets say 01.macro1 , when the input language is EN will launch in a conflict palette when pressing m yet when in another language it wont run

Any idea guys? i though maybe using regex to choose either or but dont think that is supported as a macro name?

thx so much
Z

I think it is possible to get the results that you want. Individual macros themselves do not let you add "conditionals" to the macro's triggers (that would complicate the design of KM quite a bit) but I think you can achieve the same result as follows....

Whenever the input language changes you can run a macro that will enable or disable macros as you deem appropriate. So in your example, whenever the input language changes to EN, you can enable the macro with this action:

image

And when the language changes to something else, you can do this action:

image

This way, your macros can show up in a conflict palette based on the value of your input language. Perhaps your follow-up question should be "How do I get a macro to run when the input language changes?" My answer would be, "How does your user change the input language?"

thx @Airy !!

i may have not explained well enough :slight_smile:

i dont seek to diable macros when the input changes but rather to keep being able to launch them via the conflict panel
ie

in the above macro when i launch the macro in EN input and press g then finder launches. yet if my input language is non EN (ie greek) then pressing the g wont launch the macro

does that make sense?

im trying to find a way to not duplicate each macro pallete with the appropriate keys for each language input i have (and i have a few :))

thx so much

Z

What happens if you press the L key -- does that work?

thx @Nige_S , sorry for being slow but what is the L key? literally l?

I see Nige is on the case. His answers are usually twice as helpful as mine, so I'll let him take the lead.

However I think I may know the reason. If you are talking about the keyboard language, changing the language from EN to GR will change a few of the keyboard's mappings. In particular, the letter G is changed to the character Gamma. You may be able to fix this by adding hotkeys for the Greek alphabet to your macros. In the case of "g.finder" you probably have to add a second hotkey containing the Gamma character.

Yes, literally the L key. That key on an UK/US keyboard maps to gamma on the Greek keyboard. I'm taking a punt that gamma==g in this situation.

But I suspect the matching is done "properly", by Unicode character. In which case your best bet is to change all your prefixes to things available, and easy to type, on both keyboard layouts. Numbers would be the obvious choice, remembering that the first 1 would show all macros whose name start with "1" -- including "1. Macro", "12. Macro", "134. Macro". A second 1 would reduce the list to "11. Macro", "112. Macro", etc. And you'd select "1. Macro" when "11. Macro" is also showing by hitting .

thx so much again

but the issue is that all macros wont work since all of them are mapped differently, plus as an academic i have many input language changes (arabic, hebrew, etc) :slight_smile: :slight_smile: i was trying to avoid a duplicating the macros per layout but since that there is no solution to that when uisng conflict pallets i guess

thx

Z

But you aren't "remapping" or duplicating macros, merely changing their names so you have consistently typable characters when a Conflict Palette appears. You're already using 1, 2, 3, 4... so you just carry on with that schema.

Using numbers isn't as "obvious" as, say, p for the password macro -- but you've the on-screen cues and you'll soon embed the commonly-used ones into muscle memory.

Here's an unpromising idea that hinges on an untested solution to the known intractable problem of simulating the trigger of a conflict palette.

Summary

How Do I Trigger Macro by Its HotKey From Another Macro? - #13 by peternlewis

Still interested? :wink: Read on.

To test if the solution has a chance of working, use this AppleScript to simulate keystrokes. Replace the examples keystrokes with the keystroke that you use to trigger your conflict palette.

use AppleScript version "2.4"
use scripting additions

tell application id "com.apple.systemevents"
--Examples:
-- key code "98" -- (F7)
-- keystroke "d" using {shift down,command down}-- (⇧⌘d)
end tell

Create a new macro with TWO "Execute an AppleScript" actions and paste it into both.
Only one action will fail. Two might work (as they do on my Mojave system). :crossed_fingers:

Assign an unused trigger that is the same in all languages to the new macro.

Trigger the new macro.

If it fails to trigger your conflict palette, abandon this idea. The path ends here.

If it does trigger the conflict palette, try making a macro to:

  1. Store the current keyboard id to a variable.
  2. set the keyboard to your keyboard for English input.

Simulate the keystroke that triggers the conflict palette.

  1. Wait until any key is typed.
  2. Switch input back to the stored keyboard id.

To implement the macro:

  1. Use the output of the following solution to set a variable named "localKeyboardID":
    How to find active input language? (keyboard, foreign language) - #2 by ComplexPoint

  2. Insert the "Set Keyboard Layout To" action to an English keyboard.

  3. Insert the "Pause until the Key State Changes" action.

  4. Make an Execute an AppleScript that runs the script below.

use AppleScript version "2.4"
use scripting additions

tell application id "com.stairways.keyboardmaestro.engine"
	set inputSetting to getvariable "localKeyboardID" instance system attribute "KMINSTANCE"
	
	do script "<dict>
            <key>KeyboardLayoutID</key>
            <string>" & inputSetting & "</string>
            <key>KeyboardLayoutName</key>
            <string></string>
            <key>MacroActionType</key>
            <string>SetKeyboardLayout</string>
        </dict>"
end tell

Use the new macro to trigger your conflict palette.

Oh, yes, cross your fingers. Good luck!  🍀
1 Like

thanks everyone for your kind help, will give everything a shot over the weekend :slight_smile:

Hey zeltak,

Sorry for the DIY instructions with the bad AppleScript. If you're still needing a solution, here's a fully assembled macro to try out, arriving late as it is. :snail:

If you've moved on, I understand completely.

This macro launches a conflict palette and toggles the keyboard to the language that matches the conflict palette's expected input. When the palette closes, it toggles the input language back to the previous keyboard.

The download comes with 3 macros.

  1. "A Macro" (triggered by F7)
  2. "B Macro" (triggered by F7)
  3. "Toggle keyboard layout for conflict palette" is the controller macro. (triggered by F8)

The first 2 make up the test conflict palette.
THe third controls the launch of the conflict palette, toggles the keyboard to the input layout that works with 2 and 3, and when the conflict palette closes, toggles the keyboard back to the previous keyboard.

To run the test:

  1. Make sure the English U.S. keyboard layout is installed on your Mac.
  2. Set your input keyboard to a keyboard layout that is not U.S. English.
    Press F8.
  3. A conflict palette with A Macro and B Macro will appear and the keyboard will switch to U.S. English.
  4. Type a to play the Submarine sound. b plays the Blow sound. Typing escape closes the palette. In any case, you should see the keyboard switch back to previous keyboard.

To customize the macro's hotkeys, the hotkeys that launch the conflict palette, and the keyboard that get toggled, refer to the instructions included in the second, yellow comment,"Settings Instructions”, of the macro named, "Toggle keyboard layout for conflict palette"

Credits:

@Nige_S For taking the time to show me the correct and only way to trigger the conflict palette.

@ComplexPoint For the sine qua non jxa script that accomplishes what AppleScript cannot: retrieve the keyboard id.

Launch conflict palette and toggle keyboard.kmmacros (18.2 KB)

1 Like

amazing @CRLF !!

will try over the weekend, really appreciate your help!

Z

I'm looking forward to it and am curious to how it goes.:slightly_smiling_face:

I haven't looked closely but I wondered whether, assuming recent builds of Keyboard Maestro, it would work (in this context) to use the token:

%KeyboardLayout%

and the action:

Set Keyboard Layout


For the JavaScript version, in recent builds, with the (now default) Modern Syntax option set behind the small chevron to the left of the code text field, we can simplify a little and write:

ObjC.import('Carbon');
ObjC.bindFunction('CFMakeCollectable', ['id', ['void *']]);

return ObjC.unwrap(
    $.CFMakeCollectable(
        $.TISGetInputSourceProperty(
            $.TISCopyCurrentKeyboardInputSource(),
            $.kTISPropertyInputSourceID
        )
    )
);

1 Like

@ComplexPoint Thank you for taking time out for such a niche task!

I saw this for the the first time and thought for a moment: Oh, man, are there any nuggets that KM hasn't made available to automators, and what else am I overlooking?

I think, however, your script is still the thing without which this macro will brick. It seems so compact and self-contained, I wonder how hasn't it made its way into the KM 11.0.3 token bestiary?

Maybe I'm going about it wrong but what I'm getting out of the %KeyboardLayout% token is the display name, not the id. Instead of producing com.apple.keylayout.Greek, it simply outputs "Greek" or "German". Funny. Just to make sure I wasn't missing out on some hidden KM magic, I tried pasting "Greek" into the xml key, "KeyboardLayoutName", clearing the "KeyboardLayoutID" field, and pasting that action back into the Editor just to see if KM would fill it in somehow. It pasted OK, unsurprisingly errored:"
Failed to select keyboard layout, layout not configured
in macro "Display Text” (while executing Set Keyboard
Layout to "Greek"). Yep, it needs the id all right.

I really appreciate yours and others' help in keeping me current. I am going to post another macro version that replaces polling the accessibility window name with polling the window id after a while. I am really glad to have your solid, updated JXA version to add to it as well.

1 Like

A new version with simplified settings. Palette listening is now done by polling the window id.

  1. Set the trigger of the controller macro to what you want to use to summon the conflict palette.
  2. Set the keyboard layout to the language that is compatible with the conflict palette.
  3. Set Trigger Macros By Hot Key action to what launches the conflict palette. The controller macro will now type be responsible for typing it.

@Zeltak. I envy you. You have so many more keys available to use in making global conflict palettes!

Launch conflict palette and toggle keyboard - simplfied.kmmacros (18.1 KB)