keyboard maestro


24
Jan 12

Keyboard Maestro Folder View Macro

Keyboard Maestro has some granular control over window arrangement. Combine that with the ability to trigger specific menu items, I can create a preset view for just about any folder.

For example, this macro opens my Notes folder and displays it sorted by last modification date. I use a minor “If Condition” trick to turn off the toolbar if it is visible. The “If Condition” looks to see if there is a menu item named “Hide Toolbar” available. If it is, then the macro triggers the menu item. The Macro subsequently repositions the window and sets the Arrange By Date Modified option.

Notes View Macro

 


24
Jan 12

Keyboard Maestro Pause Condition — BBEdit Footnotes

Keyboard Maestro 5 contains several new conditional actions. The “Pause Until Conditions Met” actions is modest sounding but contains a lot of power. Fundamentally, the action will pause the currently running macro and watch for a specific condition to be met. What condition? Well, pretty much anything.

 

Wait For Menu

I’m sure there are some very interesting ways to use this, but here’s a somewhat boring yet powerful macro for entering a footnote in Markdown on BBEdit.

Footnote Macro

 

When the macro is triggered, it asks for a footnote title then sets a BBEdit Jump Mark. After jumping to the bottom of the page and inserting a footnote placeholder, the macro waits. It waits for me to finish typing my footnote. When I hit return, the macro continues where it left off and executes the BBEdit “Jump Back” function which places my cursor right back where it was. Then the footnote link is inserted and I can continue typing.

To be clear, other Keyboard Maestro macros can still be run while the first macro is paused and waiting to continue. The Pause action is not pausing KM but rather just pausing the single macro. I don’t think I would let a macro sit paused for days, but certainly for normal use I have not seen any problems.


14
Jan 12

Keyboard Maestro Macro Toggle

Here’s a quick trick. There’s a KM action to toggle a macro group on and off. For me, this is handy while using macros to override some BBEdit functions. If I ever want to get back to the native functionality in BBEdit, I just toggle my BBEdit macro group.


Macro Toggle

The same macro reenables the BBEdit macro group.


8
Jan 12

Stale Mail Rules

Since migrating to Lion, I’ve noticed several of my Mail rules fail to run. I have a number of rules that tag and move incoming messages. Rather than waste time trying to figure out why the rules do not run properly, I just setup a scheduled Keyboard Maestro macro that runs the rules for me. So far, this seems to solve the problem.


Mail Macro

 

 

Edit: Take out the “Activate Mail” step unless you want to drive yourself crazy.


7
Jan 12

BBEdit Reference Link Macro

Quick Keyboard Maestro macro. This one is for inserting a Markdown reference style link at the end of a document. The magic is that it also insert the link at the current position in the line and maintains the insertion position in the current line. It’s all made possible by BBEdit’s “Set Jump Mark” command.

Here’s the macro:


Link Macro

I have a similar macro for inserting footnotes in Markdown.


6
Jan 12

Simpler Affiliate Links

I have a new saying: “If you want something done right, just pique Dr.Drang’s interest.” There’s been constant improvement to this workflow and it’s interesting to see it evolve.

Here’s a bonus macro that creates Amazon affiliate links.[1] For this one, just copy the product link from Amazon and run the macro to extract the product item and create an affiliate link.[2]


Amazon Macro

Here’s the workflow:

  1. Visit an Amazon product page
  2. Copy the page link
  3. Switch back to a document I want the link inserted in
  4. Run the macro to have the affiliate link pasted in

Here’s the AppleScript:

tell application "Keyboard Maestro Engine"
    try
        set oldDelims to AppleScript's text item delimiters -- save their current state
        set myClip to (process tokens "%Variable%tempClip%")
        if myClip does not contain "/dp/" and myClip does not contain "/product/" then
            return "Did Not Find An Amazon Link"
        else
            if myClip contains "/dp/" then
                set AppleScript's text item delimiters to "/dp/"
            end if
            if myClip contains "/product/" then
                set AppleScript's text item delimiters to "/product/"
            end if
            set tempString to text item 2 of myClip
            set AppleScript's text item delimiters to "/"
            set tempString to text item 1 of tempString
            return tempString
        end if
        set AppleScript's text item delimiters to oldDelims -- restore them
    on error
        set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
    end try
end tell

Notes

When I’m logged into Amazon, the product page link has a different structure. The AppleScript needs to determine what kind of link I’ve copied. From what I can tell, Amazon product links have either a path that includes “/product/” or “/dp/” right before the product id. That’s what the AppleScript if-else statements are determining.

This macro also has an example of the Keyboard Maestro flow-control actions. If the AppleScript returns a failure, then KM will display a popup message rather than inserting text.

EDIT: I just realized, after using this macro for so long, that I forgot to include a try-catch block to reset AppleScript’s text delimiters. That’s just dumb.


  1. I’m not posting this to ellicit more work from Dr.Drang and Brett Terpstra. This is working well enough for me. Really guys, thanks for all the work.  ↩

  2. You’ll need to replace my affiliate link with your own, unless you just want to be extra generous to me.  ↩


31
Dec 11

Making Safe URLs

I need to do this often enough, I wrote a macro with some Python for it. The macro converts a url with invalid characters (such as spaces) to a safe encoded url. Importantly, the forward slashes, colons, question marks, equal signs, quotes and hashes that are necessary to pass queries and parameters are untouched by the conversion.

A common example for me is as follows.
Select a URL that has bad actors in it:
http://www.macdrifter.com/wp-content/uploads/2011/12/$$shomer fucking?.jpg

Run the macro and the idealized URL is inserted in its place:
http://www.macdrifter.com/wp-content/uploads/2011/12/%24%24shomer%20fucking%3F.jpg

Here’s the Keyboard Maestro Macro:

Here’s the Python script that does all of the work:

#!/usr/bin/env python
from urlparse import urlparse, urlunparse
import urllib
import os

# Get values from Keyboard Maestro workflow
kmVarClipBoard = os.getenv('KMVAR_tempClip')

# Create a parsed url. See: http://docs.python.org/library/urlparse.html
o = urlparse(kmVarClipBoard)

# Remove unsupported characters. '/' is a safe character
newPath = urllib.quote(o.path, '/')
newParams =  urllib.quote(o.params, '/')

# Create a new tuple with our new quoted path as a replacement
n = o[0:2] + (newPath ,) + o[3:]

# Reassemble the entire url
baseUrl = urlunparse(n)
print baseUrl

Of course, if this is passed something that is not a url, then it will throw an exception. If that’s a concern, then check one or more of the url parse values to make sure there is a domain.


24
Dec 11

What’s So Fun About Fake?

I wrote about the Mac application “Fake”[1] awhile ago. From my very first use, I thought it was an incredibly clever tool. Fake is like a super-charged automator for Webkit. It makes automating interactions with webpages easy and simple. But why would I want to use Fake instead of Automator or curl or even a Python script? Here’s an example that I just started using.

If I want to add an affiliate link for a Mac or iOS app, I need to visit the Apple Link Maker page with my affiliate id as a get-parameter in the link. Then I need to choose iOS or Mac apps and type in the application name. I hit search and get back a list of apps. If I typed the name correctly, then the app I want will be on the top of the list.

Link Maker

applist

I’m not done yet. I want the link, so I click the “App Link” to get a pop-over. I then select the link text and copy it. It’s kind of tedious and therefore, I haven’t used them much. What’s worse, all of the content only exists after the JavaScript has executed and added the content to the page. I can’t just submit a form and get a new page, I have to click the Submit button to generate the new document elements. That’s where most web scraping breaks.

JS Popover

This is a common problem with automating web-interactions. Automator, curl or python’s urllib will not have access to the content I want. That’s because developers are expecting a javascript engine to insert content into the DOM. Without a JS engine, the urllib2, Scrapy or BeautifulSoup functions are not useful.[2] That’s where something like Fake comes in handy.

Hello Fake

With Fake, I can automate the entire process. I build a Fake workflow much like I build an Automator workflow. Drag and drop actions and set variables. Fake has auto-discovery for form fields too. Just ctrl-drag from the action to the location on the page to auto-discover the element id. Fake will automatically determine the proper tag hierarchy to find it in the future.

Fake can also press buttons on the page. Now I’m getting somewhere. It can even extract the content I need from the JavaScript pop-over.

Here’s the workflow:

Fake Workflow

Macro Time

But how can I automate this process? Well, I kind of like Keyboard Maestro for this kind of stuff. Luckily, Fake has good AppleScript support. In my macro, KM pops up a form where I choose an app type[3] and type an app name to query. Next, an AppleScript is executed to launch the Fake workflow.

tell application "Fake"
    activate
    open "Macintosh HD 2:Dropbox:Development:Fake Workflows:getAppLink.fakeworkflow"
    delay 1
    run workflow
    wait until done
    close front document
    quit
end tell

Fake can also run AppleScript so it can also get variables from Keyboard Maestro. Here’s the code from the first Fake action that grabs the input from the form:

 
set kmVarAppLink to ""

tell application "Keyboard Maestro Engine"
    set kmVarAppName to (process tokens "%Variable%AppName%")
    set kmVarAppType to (process tokens "%Variable%AppType%")
end tell
tell application "Fake"
    set variable with name "appName" to kmVarAppName
    set variable with name "appType" to kmVarAppType
end tell

Now fake has all of the information needed to load the page, populate the drop downs, click submit and then load the JavaScript pop-over. Finally, it copies the affiliate link to the clipboard for me.

Fake Macro

The final workflow goes something like this:

  1. cmd-opt-M to open my Markdown palette of macros
  2. Type C to choose the macro for appstore affiliate links
  3. Fill out the popup form and click ok
  4. wait for the alert that the macro is complete
  5. paste the affiliate link

Nice. I’m starting to play with Fake as more than just a way to probe web pages. It has some great potential. I haven’t even touched the more powerful actions that allow custom JS to be run against a page.

I would like to see more options to export a workflow to a stand-alone applet or script. Or perhaps a separate headless engine that could run independent of the application (like Keyboard Maestro). I’d also like integration with 1Password for autofill of passwords. I have a couple other ideas for automation that would require hard-coding login credentials and I’m not a fan of that.


  1. Affiliate Link
  2. There are things like Selenium that provide a JS engine to Python but I find it to be overly difficult and time consuming to use. I’m also not good at Python. So there’s that.
  3. I find it interesting that the form value submitted for iOS apps is “software” and the form value for Mac apps is “macSoftware”

21
Dec 11

Cheat Sheet Macros

Here are a few Keyboard Maestro macros that I find very handy. I call them “cheat sheets” but they are essentially a scratch pad system. This is made possible by the persistent nature of Keyboard Maestro variables. They persistent even through restarts.

The workflow for this system goes something like this:

  1. Browse around and see some text I want to remember
  2. Copy to clipboard and hit opt-cmd-V to bring up my scratch board HUD
  3. Hit “b” to add the content to the cheat sheet

Scratch HUD

  1. Later… hit opt-cmd-V and then “d” to get a pop-up window with the text.

Cheat Sheet

The text can be edited and copied as well. The window can be left open without interfering with other macros. In fact, the window can be left open and the cheat macros used with other content. It’s like little temporary post-it notes.

I run two different macros for adding content to the cheat sheet. One macro appends the clipboard to the top and one replaces the content with the current clipboard.

Cheat Sheet Set

Cheat Sheet Set

And here’s the macro for displaying the text.

Cheat Sheet Display

These macros only work with text. There is no image support. It’s a pretty darn fast way to collect a bunch of text and keep it readily available. The example in the screenshot was while I was working on an AppleScript and I want a quick “snapshot” of the current state.

I have a couple other macros that truly are cheat sheets. One displays the MultiMarkdown tags and another shows some of my less used TextExpander shortcuts. Of course, I keep this stuff in Simplenote too, but it’s much faster to lookup by hitting a couple of macro hotkeys than doing a search in NVAlt.


20
Dec 11

Keyboard Maestro HUD [Link]

Tuesday Tip From Devon Technologies, makers of the excellent DevonThink and DevonAgent. Be sure to follow the link to the forums where there are several tips for using Keyboard Maestro with DevonThink.

 


18
Dec 11

Paste and Return Cursor Position [Macro]

I couldn’t resist. This came up on Twitter and I already had a similar macro running. This Keyboard Maestro macro takes the current clipboard string and pastes it into any application and then simulates the left arrow key to move the cursor back to it’s original position. This macro should be application agnostic since it uses standard keyboard input and basic AppleScript functionality. The same functionality should be possible with TextExpander as well.

Github link to download macro

Enjoy, Eddie.

Paste Macro

 

UPDATE: Here’s a different version that’s much faster. It’s just a bit of cursor finagling through simulating key presses. Probably not as nice as a key binding though.

Github link

 


14
Dec 11

Keyboard Maestro Workflow [Link]

Nice overview of my beloved Keyboard Maestro by Michael Schechter.

 

By Way of Clark’s Tech Blog


1
Dec 11

The Keep ‘em Up Macro

Most people are familiar with the setting a Start Up item on Mac OS X and the new Lion behavior of relaunching apps that were open at the last restart is also great. But I’ve also experienced the problem where I’ve gone away on a trip only to have iTunes crash or shutdown for some reason. That means the AppleTV is no longer able to stream SuperWhy episodes from the media Mac and that’s a problem.

I have a Keyboard Maestro macro the simply checks every 10 minutes and launches specific applications if they are not running.

Keep em up

Macro Detail

Now I could write a script to do the same and then schedule the script through Cron in the terminal, but why hassle. Keyboard Maestro makes it easy. I keep a “Cron” folder of macros that just run at a specific time. It doesn’t get much easier than that.

 


28
Nov 11

Mail Action Macro – Walkthrough

This walkthrough demonstrates how I use Keyboard Maestro to control applications that are not scriptable or have few exposed options.

This example creates some handy macros for quickly filing mail in Mail.app. It’s no where near the power of Mail Act-On but it does provide one of my most used features, which is hotkey triggered filling of Mail messages.

Macro Groups

Before getting into the details of macro creation, I want to describe how I organize my macros. The system is still evolving, but as my collection grows, careful organization and trigger settings become more critical.

I have a system for my macros. I keep related macros together. Not just by function, but by how they are triggered. I use a number of palette triggers that pop-up a palette of actions to select. I do this to reduce the number of keyboard shortcuts that I need to remember. But sometimes I want two palettes that might use the same or similar hotkey[1]. In this case I already have a Markdown palette that uses the opt-cmd-M hotkey and I want a new palette for Mail that also uses opt-cmd-M. Keyboard Maestro will handle this fine by showing an option for one palette or the other, but why waste key presses when I can customize the palette triggers

I don’t use Markdown functions in Mail. I often will compose lengthy emails in NVAlt using Markdown, but with normal Mail use, I just write in plain old text. In this case, I customize the two triggers to only function when appropriate. The Markdown trigger fires in every application except Mail and the Mail trigger on fires in Mail.

Mail Filer Macros

Now I have my two macro groups separated. I need some macros for moving mail items. I typically organize and name my macros according to function and trigger within a single group. I number my macros within a group and assign their hotkey to the matching number. For example, the Mail Filer macro group looks like this:

The number on the right is the macro trigger. Keep in mind that these macros are not available until their group palette is triggered by the opt-cmd-M trigger. That way, there are no conflicts between unrelated macros. When the palette is activated, I get a list and I can select an individual macro by hitting the corresponding number key on my keyboard. I find this setup the most reliable way to keep a lot of macros with only a few key bindings.

The heart of each of these macros is the the Select Menu action. This action provides access to a menu item many levels deep. Here’s an example:

It’s not necessary to manually enter all of the layers in a menu selection. Keyboard Maestro provides a recording functionality that will create the action when selecting a menu. It’s a handy way to build macros but it does record EVERYTHING. After completing the recording I also go back and delete all of the extraneous actions like mouse clicks and application switching.

This was a very simple macro for controlling an application without AppleScript. Keyboard Maestro has the capability to completely simulate human interaction. Anything that I can do with a mouse and keyboard, I can likely script with KM.


  1. I usually pick hotkeys that start with opt-cmd-, where is the first letter of the context. For example opt-cmd-F is my Finder or File palette for manipulating files.


28
Nov 11

MPU Visitors

Hi MPU visitors. Thanks for stopping by.

You can find a bunch of Keyboard Maestro related links on my public Pinboard with the _KM_MPU tag. Otherwise, poke around and try building some cool stuff with Keyboard Maestro. Comments are always welcome.