Keyboard Maestro Macros for URL's

October 24, 2013

Keyboard Maestro Macros for URL's → via @_patrickwelker

In my quest for the perfect plain text list management workflow I ended up with a few lists that only have the plain URL as list items (like * <http://check-that-out.jp>). For certain lists this was good enough since the URL slug usually tells me the story behind the link. In addition, having just an URL looked more concise in most editors, c.f. TaskPaper.

However, on the Mac I find myself using solely FoldingText for my list management. It has the nice feature to hide most of the Markdown markup and instead of [Polar Bears wearing kilts](http://thetraditionalwhitebear.com/tartan-lovers) it presents me just an underlined Polar Bears wearing kilts. Well, that’s how I like my polar bears and that’s why I love FoldingText.

After realizing that all my links could look like that I decided to switch back to Markdown inline links. The problem was that I already had over 200 list items which were formatted as self-links. D’oh! What to do?2

Probably no one needs what I came up with, but I was in need for something that cleaned up the mess I made.

The brief: a script that is capable of spitting out nicely formatted Markdown links with the title of the web page when all it gets is a lousy URL.

In the process of coming up with the solution I ended up with three small macro’s that you can import into your version of Keyboard Maestro™.

example-palette

Get all URL’s in the selection

Description: This macro copies the selected text and attempts to find all URL’s in it. You’ll end up with the list of fetched URL’s in the clipboard.

get-all-urls-in-the-selection

Function:

  1. Copies the selected text to the clipboard.
  2. Creates a temp file with the clipboards content.
  3. A shell script which strips everything else that isn’t a URL and generates a new variable (Linklist) which contains a list of all URL’s in the selection.
  4. Clean up → Delete the clipboard with the selection (we don’t need it anymore).
  5. Set the variable Linklist to the clipboard.
  6. Send a notification that all links are in the clipboard.
  7. Clean up → Delete the variable so it doesn’t show up in your library.

Disclaimer: The regular expression used in step 3 isn’t the approved & bulletproof “Accurate Regex Pattern for Matching URLs” by John Gruber. His one is clearly better, but I failed to get it to work with my simple shell script so I went with one from the RegExr Community.

DOWNLOAD

Description: This macro is intended to use with just a single URL. It copies a selected URL, fetches the title of the web page from the Internet and puts a Markdown inline link in your clipboard.

convert-selected-url-to-md-link

Function:

  1. Copies the selected text to the clipboard.
  2. Makes a variable (URL) out of the clipboard.
  3. Initiates an IF condition that checks if the selection is an URL.
  4. If it is an URL it fetches the title of that URL’s web page and puts it in a variable (Title).
  5. Decode all HTML entities in Title to have a nice and readable title in the Markdown link.
  6. A small regular expression to lazy delete the leading and trailing whitespace.
  7. Strips the pipe character (|) from the title and replaces it with a dash. This is done because Kramdown - the Markdown parser in my Jekyll blog - always has trouble with the pipe character and assumes it belongs to a Markdown table.
  8. Deletes the trailing which the shell script created.
  9. Format the actual Markdown link with the two variables and put it in the clipboard.
  10. Send a notification that the link is ready and waits in your clipboard.
  11. If step 3 returns false, then output an error message that the selection didn’t contain an URL.
  12. Clean up → Delete the variables so they don’t show up in your library.

DOWNLOAD

Description: This macro tries to grab all URL’s in the text selected. It grabs the title of the web page for each URL from the Internet and creates a list of Markdown inline links in your clipboard.

convert-all-urls-to-md-links

Function:

  1. Copies the selected text to the clipboard.
  2. Create a temp file with the clipboards content.
  3. Clean up → Delete the clipboard with the selection (we don’t need it anymore).
  4. A shell script which strips everything else that isn’t a URL and generates a new variable (Linklist) which contains a list of all URL’s in the selection.
  5. Time Intensive Process Warning Pt. 1 - Duplicates Linklist and names this variable CountURLs.
  6. Time Intensive Process Warning Pt. 2 - Counts how many URL’s are in the selection.
  7. Time Intensive Process Warning Pt. 3 - If statement for “when there are more than 9 URL’s in the selection”.
  8. Time Intensive Process Warning Pt. 4 - Send out a notification that the process will take a bit longer.
  9. Create an empty temporary file for appending the newly generated Markdown links to.
  10. If condition to parse every line (aka URL) in the Linklist variable and run step 11 to 17 repeatedly until all lines got their treatment.
  11. Fetches the title of an URL’s web page and puts it in a variable (Title).
  12. Decode all HTML entities in Title to have a nice and readable title in the Markdown link.
  13. A small regular expression to lazy delete the leading and trailing whitespace.
  14. Strips the pipe character (|) from the title and replaces it with a dash (see explanation in former macro).
  15. Deletes the trailing which the shell script created.
  16. Format the actual Markdown link with the two variables and put it in a variable (newLink).
  17. Append each newLink to the temp file created in step 9.
  18. Read all the new Markdown inline links and put them in the clipboard.
  19. Send a notification that the link is ready and waits in your clipboard.
  20. Clean up → Delete the variables so they don’t show up in your library.

DOWNLOAD

DOWNLOAD COMPLETE PACK

comments powered by Disqus

Related Posts