How to create a pick-list?

Since KM does not have a prompt that allows you to select multiple items, AppleScript is a good choice to provide this prompt using the choose from list command.

Here is a generic macro provided to give an example of how to build your own macro. You will need to use it as a guide or modify it to meet your specific needs.

##example Output


###MACRO:   Select Multiple Items From List [Example]

~~~ VER: 1.0    2017-11-13 ~~~

####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/6/2/6273653a8aeab70a59668c379936034dde2e8950.kmmacros">Select Multiple Items From List [Example].kmmacros</a> (15 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

###ReleaseNotes

Author.@JMichaelTX

**PURPOSE:**

* **Select Multiple Items from List**
* and format output as comma-delimted string

**REQUIRES:**

1. **KM 8.0.4+**
  * But it can be written in KM 7.3.1+
  * It is KM8 specific just because some of the Actions have changed to make things simpler, but equivalent Actions are available in KM 7.3.1.
.
2. **macOS 10.11.6 (El Capitan)**
  * KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees.  :wink: 

**NOTICE: This macro/script is just an _Example_**

* It has had very limited testing.
* You need to test further before using in a production environment.
* It does not have extensive error checking/handling.
* It may not be complete.  It is provided as an example to show you one approach to solving a problem.

HOW TO USE:

1. Enter your full list to choose from in the first two Actions
2. Trigger this macro

**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  😉
.
* Assign a Trigger to this maro.  I prefer TBD.
* Move this macro to a Macro Group that is only Active when you need this Macro.
* ENABLE this Macro.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
(all shown in the magenta color)
  * First Two Actions: "Set This to Your List of Items"


TAGS:  @Lists @KM8 @AppleScript

USER SETTINGS:

* Any Action in _magenta color_ is designed to be changed by end-user

ACTION COLOR CODES

* To facilitate the reading, customizing, and maintenance of this macro,
      key Actions are colored as follows:
* GREEN   -- Key Comments designed to highlight main sections of macro
* MAGENTA -- Actions designed to be customized by user
* YELLOW  -- Primary Actions (usually the main purpose of the macro)
* ORANGE  -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions

REQUIRES:

1.  Keyboard Maestro Ver 7.3+ (don't even ask me about KM 6 support).
2.  El Capitan 10.11.6+
  * It make work with Yosemite, but I make no guarantees.

**USE AT YOUR OWN RISK**

* While I have given this limited testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

---

<img src="/uploads/default/original/3X/f/d/fd2016c4a633a939247d309d7174cfb454a195a4.png" width="490" height="1527">

---

###AppleScript 

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

property ptyScriptName : "Select Multiple Items From List"
property ptyScriptVer : "1.0"
property ptyScriptDate : "2017-11-13"
property ptyScriptAuthor : "JMichaelTX"

(*

KM VARIABLES REQUIRED: (must be set before calling this script)
• Local__List

KM VARIABLES SET: (by this script)
• NONE

REQUIRED:

  1. macOS El Capitan 10.11.6+
    (may work on Yosemite 10.10.5, but no guarantees)

  2. Mac Applications
    • Keyboard Maestro 8.0.4+

  3. INTERNAL HANDLERS (Functions):
    • on getKMVar(pKMVarNameStr, pDefaultValueStr) -- Get KM Variable; set to default if doesn't exist

REF: The following were used in some way in the writing of this script.

  1. How to create a pick-list?
*)
--- TO BE RETURNED TO KM ---
--  Will be either actual results, or Error message
set scriptResults to "TBD"

try
  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  set frontApp to path to frontmost application as text -- use for dialogs
  
  --- GET KM VARIABLE VALUES ---
  set myList to paragraphs of (getKMVar("Local__List", "") of me)
  
  ### YOUR CODE HERE ###  
  
  tell application frontApp
    
    set selectedItems to choose from list myList ¬
      with title ptyScriptName with prompt "Select One or More Items" with multiple selections allowed
    
  end tell
  
  if (selectedItems is false) then error "Use did NOT select any items."
  
  set AppleScript's text item delimiters to ","
  set selectedItemsStr to selectedItems as text
  
  set scriptResults to selectedItemsStr -- "OK" as a minimum
  
  --~~~~~~~~~~~~~ END TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
on error errMsg number errNum
  
  if errNum = -128 then ## User Canceled
    set errMsg to "[USER_CANCELED]"
  end if
  
  set scriptResults to "[ERROR]" & return & errMsg & return & return ¬
    & "SCRIPT: " & ptyScriptName & "   Ver: " & ptyScriptVer & return ¬
    & "Error Number: " & errNum
end try
--~~~~~~~~~~~~~~~~END ON ERROR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- RETURN THE RESULTS TO THE KM EXECUTE SCRIPT ACTION ---
return scriptResults

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--    HANDLERS (functions)
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on getKMVar(pKMVarNameStr, pDefaultValueStr) --  @KM @Get @Variable
  (*  VER: 2.1    2017-11-13
---------------------------------------------------------------------------------
  PURPOSE:  Get Value of KM Variable & Set to Default if it doesn't exist
  PARAMETERS:
    • pKMVarNameStr        | text  | Keyboard Maestro Variable Name
    • pDefaultValueStr    | text  | Default value if Variable doesn't exist
  RETURNS:  KM Var Value
  AUTHOR:  JMichaelTX
  —————————————————————————————————————————————————————————————————————————————————
*)
  local kmVarStr, isKMErunning
  
  tell application "System Events" to set isKMErunning to (exists process "Keyboard Maestro Engine")
  
  if (isKMErunning) then
    
    if ((pKMVarNameStr starts with "Local") or (pKMVarNameStr starts with "Instance")) then
      
      --- KM Local or Instance Var ---      
      --  (Requires Keyboard Maestro 8.0.3+)
      
      set kmInst to system attribute "KMINSTANCE"
      tell application "Keyboard Maestro Engine"
        set kmVarStr to getvariable pKMVarNameStr instance kmInst
      end tell
      
    else -- normal, global KM Variable
      --     (Requires KM 7.0.3+)
      
      tell application "Keyboard Maestro Engine"
        set kmVarStr to getvariable pKMVarNameStr
      end tell
    end if -- KM Local or Instance Var
    
    if (kmVarStr = "") then set kmVarStr to pDefaultValueStr
    
  else
    
    set kmVarStr to pDefaultValueStr
    
  end if
  
  return kmVarStr
  
end getKMVar
--~~~~~~~~~~~~~~~ END OF handler getKMVar ~~~~~~~~~~~~~~~~~~~~~~~~~


```