Macro to Assign Macro Group to App

Hello to All,

Is there a way via KM / Applescript to toggle assign / deassign a macro group to an app?
(I don't know if the word "deassign" exists in English :wink:

My goal is to:

  • Temporarily assign a group to KM,
  • Then edit the group's macros names
  • And finally deactivate the KM assignment

Thanks for help.

Hey Larry,

This turns out to be relatively simple to do with AppleScript.

  • You have to get the available application xml of the desired macro group.
  • Replace the relevant bits with the specifications of your desired app.
  • Replace the available application xml.

All I'm showing in the script below is how to replace the available application xml.

I would use either AppleScriptObjC or the Keyboard Maestro Engine's own AppleScript search command to do the find/replace in the XML.

-Chris


cc @DanThomas, @tiffle, @Nige_S, @ComplexPoint, @cdthomer

Of possible interest.


--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/12/12 06:21
# dMod: 2022/12/12 06:21 
# Appl: Keyboard Maestro
# Task: Change Selected Macro Groups App Affiliation to iCab.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro
--------------------------------------------------------

tell application "Keyboard Maestro"
   # Selected Macro Group (you can get the macro group via other means as well).
   set theMacroGroup to (get selection)'s item 1
   tell theMacroGroup
      set available application xml to getXML() of me
   end tell
end tell

--------------------------------------------------------
--ยป HANDLERS
--------------------------------------------------------
on getXML()
   return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
   <key>Targeting</key>
   <string>Included</string>
   <key>TargetingApps</key>
   <array>
      <dict>
         <key>BundleIdentifier</key>
         <string>de.icab.iCab</string>
         <key>Name</key>
         <string>iCab</string>
         <key>NewFile</key>
         <string>/Applications/Applications_Chris/Internet_Apps/Web_Browser_Apps/iCab/iCab 6/iCab.app</string>
      </dict>
   </array>
</dict>
</plist>
"
end getXML
--------------------------------------------------------
2 Likes

Thank you very much, Chris โ€ฆ I'll check it out in the next days โ€ฆ

"The next days" can sometimes last longer than expected :joy:

Based on Chris' script I wrote the attached one which toggles the macro's assigned application, in this case Keyboard Maestro.

I find it useful when working on the layout of a palette which is normally not assigned to KM.
I wanted to share โ€“ maybe someone is interested โ€ฆ

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

tell application "Keyboard Maestro"
	set _mySelMacroGroups to selected macro groups
	if _mySelMacroGroups is not {} then
		repeat with i in _mySelMacroGroups
			set _available_app_xml to available application xml of i
			if _available_app_xml contains _targetApp then
				set theString to _available_app_xml
				set suchen to _targetApp & _deactivateAppKM as string
				set ersetzen to _deactivateAppKM
				tell me to set _new_available_app_xml to austauschen(suchen, ersetzen, theString)
				tell i to set available application xml to _new_available_app_xml
			else
				set theString to _available_app_xml
				set suchen to _deactivateAppKM
				set ersetzen to _targetApp & _deactivateAppKM as string
				tell me to set _new_available_app_xml to austauschen(suchen, ersetzen, theString)
				tell i to set available application xml to _new_available_app_xml
			end if
		end repeat
	end if
	
end tell

property _targetApp : "		<dict>
			<key>BundleIdentifier</key>
			<string>com.stairways.keyboardmaestro.editor</string>
			<key>Name</key>
			<string>Keyboard Maestro</string>
			<key>NewFile</key>
			<string>/Applications/Keyboard Maestro.app</string>
		</dict>
"
property _deactivateAppKM : "	</array>
</dict>
</plist>
"
property _activateAppKM : "		<dict>
			<key>BundleIdentifier</key>
			<string>com.stairways.keyboardmaestro.editor</string>
			<key>Name</key>
			<string>Keyboard Maestro</string>
			<key>NewFile</key>
			<string>/Applications/Keyboard Maestro.app</string>
		</dict>
	</array>
</dict>
</plist>
"

on austauschen(suchen, ersetzen, theString)
	set olddelis to my text item delimiters
	set my text item delimiters to (suchen)
	tell me to set theList to (every text item of theString)
	set x to theList
	set my text item delimiters to (ersetzen)
	set theString to theList as string
	set my text item delimiters to olddelis
	return theString
end austauschen