Optimisation of user input selection

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: