Optimisation of user input selection

I have 50 macros all sending a different email.
I have one macro which has the name of all the other macros as a selection.
User then selects 1 of the 50 macros and presses ok (the selection is saved as a macro)
KM then checks each of the 50 UIs to see which one has been selected then triggers that macro.

This process is very slow and takes ages, is there a work around this system?
If the user selected the 49th macro in the list then KM has to check macro 1-48 to see if it has been selected as the variable then execute the macro, this is time consuming.

Hey Ali,

It’s difficult to comprehend your workflow from the given description and images.

What I would probably do is use Dan Thomas’ Spotlight Search Prompt to pick the given email.

(Or Keyboard Maestro 8’s new Prompt with List action.)

I would then use the returned text in a find action. (Personally I’d use the Satimage.osax and AppleScript for this, but Keyboard Maestro’s search will work perfectly well.)

Your pick-list would look like this:

Requesting Email address for correspondence
Requesting Client updates their Details with Bank

Then you would find the first column in a list like this:

Requesting Email address for correspondence				do script "XXXXX01"
Requesting Client updates their Details with Bank		do script "XXXXX02"

Only the do script portion would be returned and then run.

For ease of maintenance I’d only use 1 list, and the macro itself would split it into column 1 for the Prompt with List action.

A workflow like this should be pretty easy to implement and very simple to use and maintain.

-Chris

Here are some untested ideas, but based on my experience of doing similar automations in the past.

I would take a completely different approach, which should need only one macro.

  1. Design a process (set of Actions) that will accept all of the required data for the email:
  • Name, Subject, Contents, etc
  1. Then send the email using that data.

This should be very fast, especially if you store the data in a SQLite database (see below).

Setup: Create a file or database with all of the data

  • Assign an unique ID to each dataset for a specific email
  • Assign a Title that will be used for selection

Your workflow/macro would be:

  1. Read the database/file to get ID and Title to be used in the email
  2. Prompt the user with the list of titles, where each title has a hidden associated ID
  3. Use the user-selected ID to get all of the data for the email
  4. Compose and send the email using this data.

My preference would be to store the data in an SQLite database.

  • The SQL statements to get the two datasets are very simple (if you know SQL).
  • @ShaneStanley has recently released SQLite Lib, an AppleScript Script Library that makes accessing a SQLite database very easy and fast.
  • It is easy to import/export data to/from the database using CSV and tab-delimited data
  • There are free SQLite apps that might do all your need: View, test SQL statements, simple import/export.
  • You can use KM to get/set data in the database by calling an AppleScript, or a Shell Script.

My second preference would be to store the data in JavaScript JSON format in a file.

Finally, you could store all of the data in a text file, to be read by KM and select the data you need using RegEx.

What does this mean? Selects how? How can the selection be saved "as a macro"?

If you want to select a macro using a popup menu, you could use the Prompt for User Input action and the How to Store a Different Value than Displayed technique to store the UUID.

Or you could use a Dictionary to map from names to UUIDs.

Or you could use a text file containing UUID:name, and grep the name, and retrieve the UUID.

macro action has a drop down box (each drop down box is the name of a macro)
user makes a selection
that Selection is saved as a KM variables
the 50 macros are triggered with all containing the Selection macro if variables matches then macro is triggered (please see attached)

Please could you explain these more?

If you want to select a macro using a popup menu, you could use the Prompt for User Input action and the How to Store a Different Value than Displayed technique to store the UUID.

Or you could use a Dictionary to map from names to UUIDs.

Or you could use a text file containing UUID:name, and grep the name, and retrieve the UUID.

Here's an example of this technique:

Optimized Macro Picker.kmmacros (3.8 KB)

This macro automatically lists the names of all macros that start with "SMS Create" in a dropdown box, then, upon making a selection, passes that macro's UUID to an Execute an AppleScript action that only executes that macro. It does so by populating the list with macro names written like this:

[UUID]__[MACRO NAME]

When variables with two underscores like this are used in a Prompt for User Input action, the text after the underscores is displayed in the prompt, but the text before the underscores is stored as the resulting variable value. Once we have the selected macro's UUID in a variable, it's easy enough to use AppleScript to tell the Keyboard Maestro engine to execute that specific UUID (the final action of the macro) as opposed to the list of 50 or so UUIDs you show in your OP.

Please note that using the macro I've shown here will require you to remove the If/Then action from each of these SMS Create macros, since with this method, the Reason for SMS variable now contains a KM UUID and not the macro's name (though honestly, I would recommend doing this anyway since with this method, the If/Then check is no longer needed). Fortunately in KM8, you can quickly remove the If/Then (or any other grouping action like Repeat, For Each, etc.) by selecting all of the actions contained within it and selecting "Degroup" from the Actions menu.

Incidentally, in addition to (or instead of) this prompt method, you could also use a macro consisting of a single Trigger Macro by Name action that is limited to your SMS Create macros, which would enable the kind of pick-by-searching functionality described by @ccstone and @JMichaelTX above. This kind of macro is very easy to set up, so I would encourage you to try this as well:

Please note that using this to trigger macros directly will also require you to remove the current If/Then check from them (though that's just all the more reason to do so, in my opinion).

1 Like

You do not show any action that shows any "drop down box" (whatever that is), so I don't really know what you mean.

Wow! I am amazed! This is a massive work around for me preventing so many bugs in later scripts.

As if I have discovered Fire.

2 Likes

Hah! Your response made my day. I’m glad my example macro proved so useful for you :smile:
Hopefully the macro and its AppleScript actions are self-explanatory enough that you can figure out how to adopt it to other groups of macros, but feel free to ask if you have more questions in the future.

2 Likes

That’s exactly what I have done :), adopting it across 3 other groups and a total 400+ macros :slight_smile:

1 Like

Is it possible if i can search and get the names of the macros in a particular folder by searching the folder name?

If by “folder” you mean “macro group”, then certainly. Just add in macro group "NAME OF GROUP" after every macro in the initial AppleScript action, like this:

repeat with i in (every macro in macro group "Test" whose name starts with "Search")
1 Like

Thank you once again, I just can’t believe how simple the code is in the Applescripts. If I was to write something I would probably write another sentence and it won’t work :slight_smile:

1 Like

Glad I could help! Fortunately (at least in this case) I’m still very much an AppleScript beginner, so this kind of simple script is all I can manage right now :slightly_smiling_face:

1 Like

How can I make the result allow multiple actions?

please see email attached..

Hey Ali,

What action are you using to get this listing?

-Chris

1 Like

Hi Chris,
I am using the same actions in ggclick’s macro above. Rather than a single option how could I make this multiple selection?

Many thanks.

Hey Ali,

The dialog you show above makes no sense in reference to Gabe's macro.

It looks like an AppleScript Choose from List dialog.

Is it?

Or is it not?

-Chris

1 Like

Hi Chris, let’s forget the recent action I uploaded. How could i make the current macro allow me to multi select?

Sorry for the confusion.

KM prompts don't allow for multiple selections (unless you're capable of creating a custom HTML prompt, which I am not) so ironically, an AppleScript "Choose From List" dialog actually seems like one of the simplest ways to go about enabling multiple selections here. With the assumption that you're trying to select multiple macros to run (since the original topic was about optimizing selecting from a list of macros) here's a way of going about that. Please note that this script requires Keyboard Maestro 8.0.4 or later:

set MacroNames to {}
set MacroUUIDs to {}

tell application "Keyboard Maestro"
	repeat with i in (every macro whose name starts with "SMS Create")
		set end of MacroNames to name of i
		set end of MacroUUIDs to id of i
	end repeat
end tell

set MacrosAndUUIDs to {}
repeat with i from 1 to count MacroNames
	set end of MacrosAndUUIDs to (item i of MacroNames) & ": " & (item i of MacroUUIDs)
end repeat

choose from list MacrosAndUUIDs with multiple selections allowed
set SelectedMacros to result

repeat with i from 1 to (length of SelectedMacros)
	tell application "Keyboard Maestro Engine"
		set MacroToRun to search item i of SelectedMacros for ".*: ([A-Z0-9-]+)$" replace "$1" with regex
		do script MacroToRun
	end tell
end repeat

And in macro form (as you can see, the rest of the steps from the other macro are no longer necessary, since the AppleScript now does all the work):

Multiple Macro Selector.kmmacros (2.2 KB)

2 Likes