Keyboard Maestro Macro: Alphabetize a List

September 11, 2011 by Gabe | [mmd] |

Here's another quick macro that comes in handy. I often will create a list of items and later I will want the list to be in alphabetical or numerical order. I do it enough that I created a Keyboard Maestro macro for the function.

Given a list like this:

  • Item 3
  • Item 1
  • Something borrowed
  • Something blue
  • Something Blue
  • Alphabetical would be better

I can select the items, trigger the macro, and get back this list:

  • Alphabetical would be better
  • Item 1
  • Item 3
  • Something Blue
  • Something blue
  • Something borrowed

The real meat of the macro is an AppleScript that uses a single line of Shell Script to do the alphabetization. This also works great for ordered lists in Markdown as well.

Alphabetize Macro

 

[cc lang="AppleScript"]tell application "Keyboard Maestro Engine" set the_list to process tokens ("%Variable%myList%") set old_delims to AppleScript's text item delimiters set AppleScript's text item delimiters to {ASCII character 10} -- always a linefeed set list_string to (the_list as string) set new_string to do shell script "echo " & quoted form of list_string & " | sort -f" set new_list to (paragraphs of new_string) set new_list to new_list as text set AppleScript's text item delimiters to old_delims end tell return new_list[/cc]

blog comments powered by Disqus