MACROS: "Get Selected Action Info v1.1" and "Select Action by Id v1.1"

"Get Selected Action Info v1.1" and "Select Action by ID v1.1"

Requires:

  • macOS Ventura or higher
  • Keyboard Maestro v11 or higher

Select Action by ID:

VERSION HISTORY

1.1 - Uses %TriggerValue% if Local_ActionId is empty.
1.0 - Initial Release

Pass it an Action ID, and the KM Editor selects that Action.

If the Action isn't in the current Macro being edited, the editor switches to the Macro first.

So where do you get the Action ID from? Read on.

Get Selected Action Info

PURPOSE:

This action does exactly what it's name says. It returns information about the Action that's currently selected in the Keyboard Maestro editor.

SIMPLICITY:

If all you want is the Action ID of the selected Action, just call this macro without specifying any parameters. It will return the Action ID if one action is selected, otherwise it'll return an empty string.

SIMPLICITY, With Error Handling:

If the user does something you didn't want, like not selecting an action, you can have the macro display an error message and abort the macro by setting the parameter AbortIfError to 1.

ACTION DETAILS:

Here's what you can find out the currently-selected Action:

  • Type, like "Comment"
  • Action ID
  • Macro UUID
  • Macro Name
  • Group UUID
  • Group Name

MORE FEATURES

Read the Documentation (below) for details.

Click for a preview

image

JSON Simplicity

Depending on the parameters you supply, this macro can return results in JSON format. If you're unfamiliar with JSON, or just uncomfortable with it, here's some tips to make it really, really simple:

Click for JSON Tips

Instead of trying to decipher JSON fields, try using the Set Variables to JSON action.

For example, turn this:

{
  "actionType": "Comment",
  "actionId": "15802844",
  "macroUUID": "291E40FE-F4D0-45BD-887B-EF49E75EAD31",
  "macroName": "Example Macros",
  "groupUUID": "C913CB41-43FF-4375-99C4-EE925739BE7D",
  "groupName": "KM"
}

Into these variables:

  • Local_Result_actionType
  • Local_Result_actionId
  • Local_Result_macroUUID
  • Local_Result_macroName
  • Local_Result_groupUUID
  • Local_Result_groupName

by using this one action:

image

VERSION HISTORY

1.1 - Small bug fix, and tries to always return macroUUID even if there's an error.
1.0 - Initial Release

INSTALLATION

Click to expand

UPGRADE INSTRUCTIONS:

  1. Delete the old macros, or just delete the entire group.
  2. Install the new macros.
  3. There should be no breaking changes.

Get Selected Action Info.v1.1.kmmacros.zip (8.2 KB)

Download the macros, unzip them, and double-click the .kmmacros file. It will import these macros:

  • Get Selected Action Info
  • Select Action by ID

DOCUMENTATION

I'm trying something new here. I normally include the documentation in a large Comments action in the macro. But it turns out that can significantly increase the size of the macros.

So for the macros with large documentation comments, I'm putting the documentation here, and the comments in the macros include a link to this page.

Get Selected Action Info

image

 
Name: Get Selected Action Info
Version: 1.1
Updated: 2024/07/26 06:30 PT
By: Dan Thomas

PURPOSE:

This macro returns information about the KM Action that is currently selected, if any.

PARAMETERS:

NOTE: In the following descriptions, UserError refers to things like the user not selecting anything, or not selecting an action, etc. I'm using this term to differentiate between those types of errors, and actual JXA errors which always abort the macro.

See the last two parameters for an explanation of what happens with UserErrors

Parameter Description
ReturnDetails Default 0. By default, this macro returns the Action ID of the currently selected Action. If you set this to 1, more detailed information is returned (explained below).

If this is not 1 and AllowMultiple is 1, the first Action's ID is returned.
ExpectedActionType Optional. If specified, then the selected Action must be of this type or you get a UserError. If AllowMultiple is 1, then all selected Actions must be of this type.
AllowMultiple Default 0. By default, if multiple Actions are selected, you get a UserError. If you set this to 1, then multiple Actions can be selected, and if ReturnDetails is 1, the resulting JSON is an array.
AbortIfError Default 0. By default, if there's a UserError, this macro returns an empty string, or more details in JSON format, depending on ReturnErrorInfo.

If you specify 1 here, a message will be displayed and the macro will be aborted.
ReturnErrorInfo Default 0. This controls what happens if a UserError happens and AbortIfError is not 1. By default, an empty string is returned if there's a UserError. If you set this to 1, details about the error will be returned in JSON format (see below).

RETURN VALUE:

If ReturnDetails is not 1, the return value is a simple string. Otherwise, a JSON value is returned. A JSON value may also be returned if a UserError happens, depending on other parameters.

We'll use examples to show the possible JSON return values.

Success, one Action selected:

{
  "actionType": "Comment",
  "actionId": "15802844",
  "macroUUID": "291E40FE-F4D0-45BD-887B-EF49E75EAD31",
  "macroName": "Example Macros",
  "groupUUID": "C913CB41-43FF-4375-99C4-EE925739BE7D",
  "groupName": "KM"
}

Success, multiple Actions selected:

[
  {
    "actionType": "SetClipboardToText",
    "actionId": "15812084",
    "macroUUID": "291E40FE-F4D0-45BD-887B-EF49E75EAD31",
    "macroName": "Example Macros",
    "groupUUID": "C913CB41-43FF-4375-99C4-EE925739BE7D",
    "groupName": "KM"
  },
  {
    "actionType": "SetVariableToText",
    "actionId": "15813215",
    "macroUUID": "291E40FE-F4D0-45BD-887B-EF49E75EAD31",
    "macroName": "Example Macros",
    "groupUUID": "C913CB41-43FF-4375-99C4-EE925739BE7D",
    "groupName": "KM"
  }
]

Fail:

{
  "errorCode": "Multiple",
  "errorMessage": "You must select only 1 Action",
  "macroUUID": <if available>
}

Error Codes:

Code Description
NoActions No actions were selected
Multiple Multiple actions were selected, and AllowMultiple is not 1
WrongActionType One or more selected actions did not match ExpectedActionType

VERSION HISTORY:
1.1 - Small bug fix, and tries to always return macroUUID even if there's an error.
1.0 - Initial version.

3 Likes