Optimisation of user input selection

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

Is there a way to make the user input box go away once the OK button is pressed?

set MacroNames to {}
set MacroUUIDs to {}

tell application "Keyboard Maestro"
repeat with i in (every macro in macro group “Template Mutiple DSARs”)
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

As far as I can tell, this appears to be an issue when running the script from certain apps, like KM and Alfred. If you run it from Script Editor, Script Debugger, or as a compiled script file, the input box goes away upon pressing OK. Here's the script in compiled form:

Multiple Macro Selector.zip (2.6 KB)

Try running that from LaunchBar, or use the free trial version of FastScripts, or use Script Editor/Debugger to make an applet from the script, and the input box should go away upon making a selection.

1 Like

The problem probably is that all AppleScript dialogs must be in some app tell block to function properly. So you need this:

set frontApp to path to frontmost application as text

tell application frontApp
  choose from list MacrosAndUUIDs with multiple selections allowed
end tell

  set selectedmacros to result
2 Likes

Thank you, here is the final working version

set MacroNames to {}
set MacroUUIDs to {}

tell application "Keyboard Maestro"
   repeat with i in (every macro in macro group "*******")
      set end of MacroNames to name of i
      set end of MacroUUIDs to id of i
   end repeat
end tell

set frontApp to path to frontmost application as text

tell application frontApp
   choose from list MacroNames with multiple selections allowed
end tell

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

Hey Folks,

@gglick has come up with a pretty good solution.

One thing to note is that you DON’T have to run a do script AppleScript command using a UUID.

You can simply use the name. (Of course you have to be certain that name is unique.)

It’s as simple as this:

tell application "Keyboard Maestro Engine"
   do script "Dropbox path"
end tell

So — if you can be certain your macro names are unique you don’t have to go through the fuss of managing macro UUIDs — saving time and effort.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/11/10 23:10
# dMod: 2017/11/12 22:36
# Appl: Keyboard Maestro
# Task: Run one or more macros from a given macro group from a pick-list (no UUIDs).
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Keyboard_Maestro, @Run, @Macros, @MacroGroup, @Pick-list
------------------------------------------------------------------------------
property macroGroupName : "Global Macro Group"
------------------------------------------------------------------------------

tell application "Keyboard Maestro"
	tell macro group macroGroupName
		set macroNameList to name of macros
	end tell
end tell

tell application (path to frontmost application as text)
	set theChosen to (choose from list macroNameList with title "Macro List" with prompt "Select a macro to run:" default items {item 1 of macroNameList} ¬
		with multiple selections allowed)
end tell

if theChosen = false then return

tell application "Keyboard Maestro Engine"
	repeat with theMacroName in theChosen
		do script theMacroName
	end repeat
end tell

------------------------------------------------------------------------------

On the other hand — if you NEED to make use of UUIDs then here’s an alternative method that uses matched lists of macro-name and macro-id.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/11/10 23:10
# dMod: 2017/11/10 23:26
# Appl: Keyboard Maestro
# Task: Run one or more macros from a given macro group from a pick-list.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Keyboard_Maestro, @Run, @Macros, @MacroGroup, @Pick-list
------------------------------------------------------------------------------
use AppleScript version "2.4" # requires Yosemite or higher
use framework "Foundation"
use scripting additions
------------------------------------------------------------------------------
property macroGroupName : "Global Macro Group"
------------------------------------------------------------------------------

tell application "Keyboard Maestro"
   tell macro group macroGroupName
      set {macroNameList, macroIdList} to {name of macros, id of macros}
   end tell
end tell

tell application (path to frontmost application as text)
   set theChosen to (choose from list macroNameList with title "Macro List" with prompt "Select a macro to run:" default items {item 1 of macroNameList} ¬
      with multiple selections allowed)
end tell

if theChosen = false then return

tell application "Keyboard Maestro Engine"
   repeat with i in theChosen
      do script (item (my indexOf:(contents of i) inList:macroNameList) of macroIdList)
   end repeat
end tell

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
# Find offset of an item in a list.
------------------------------------------------------------------------------
on indexOf:aValue inList:theList
   local currentApp, theArray, theIndex
   set currentApp to current application
   set theIndex to (currentApp's NSArray's arrayWithArray:theList)'s indexOfObject:aValue
   if theIndex = currentApp's NSNotFound then
      return "null"
   else
      return (theIndex + 1) # +1 because ASObjC counts starting from 0
   end if
end indexOf:inList:
------------------------------------------------------------------------------

This method is handy, because you don’t need to concatenate anything.

It will present you with a choose-from list of macro-names and then run the associated UUID(s) of the macros you pick.

-Chris

[Edited 2017/11/12 22:37 CST to remove a little cruft from the first AppleScript.]

4 Likes

This is a great script to start the macros @ccstone :ok_hand::clap: I am a beginner with Applescript and wanted to ask if you can not only start the macros with such a list but also deactivate them when you call up the list again?

Hey @appleianer,

Are you asking me if you can terminate running macros via this mechanism?

-Chris

yep, that's what I'd like to do with the macro. Select and then exit multiple macros @ccstone :+1:

Hey @appleianer,

I don’t believe there’s a way to get a list of running macros, so I think what you want to do is not currently possible.

-Chris

Well, there is one way, but not a very good way, nor is it really scriptable.
Running macros are shown in the KM Status Menu > Cancel list: