Keyboard Maestro Clipboard and Scripts

Keyboard Maestro Clipboard and Scripts
Mac Tip #451, 25 August 2010

Keyboard Maestro can make your work quicker and easier. As a writer, I use the Filter Clipboard actions all the time to clean up text, and I love the way it can run Applescripts and even Shell scripts for me. Here’s how.

Please read Turn 100 steps into 1 with Keyboard Maestro, Point and Click with Keyboard Maestro and Keyboard Maestro Triggers and Actions for more tips on how to use Keyboard Maestro.

Use the coupon MacTips for a 20% discount on Keyboard Maestro. [Updated] This offer expires at the end of October 2010. It’s free to test the software.

Filter Clipboard

Tech Universe headline.

Tech Universe headline.

When I compile my daily Tech Universe column for the NZ Herald I have to write the headline in all caps, and the rest of it in ordinary sentence case. Somehow I almost always forget to use Caps Lock and my headline ends up lower case.

I have several choices to make it upper case, such as retyping it, or using 4 clicks to choose a suitable menu item to do the job. Instead Keyboard Maestro does it for me, with a Filter Clipboard action.

My uppercase macro.

My uppercase macro.

Clipboard filters do many handy things with text you copy, such as removing styles, making text upper or lower case, counting words, or making an HTML list.

Here’s how to make a macro to Uppercase some text.

  1. Make a macro, as explained in previous Tips.
  2. Set a Trigger.
  3. Add an action to Copy selected text.
  4. Add a Filter Clipboard action and select Uppercase from its options.
  5. Add an action to Paste selected text.

Note that Keyboard Maestro offers a dozen or more options for how to handle what’s on the Clipboard, including such handy items as counting words and characters, removing styles and encoding HTML entities.

Keyboard Maestro Filter Clipboard options.

Keyboard Maestro Filter Clipboard options.

When you use the macro on selected text it copies the text, makes it all upper case and then pastes the modified text over the original.

Restore a previous Clipboard

The problem with some macros is that if you use a Copy or Paste action you might wipe out something you’d copied previously to the Clipboard and had intended to keep.

For example, let’s say I copy the text because I want to paste it in somewhere. Before I can paste it, and as I continue writing, I may use my Uppercase action on a different piece of text, perhaps the word test. When I go to paste in it’s no longer on the Clipboard and instead I get the word test.

To get around this problem add a final action that restores a previous Clipboard: Set Clipboard To Past Clipboard 2, where 2 is a number you choose.

I chose 2 specifically because I needed to ignore both the Copy (step 1) and Paste (step 2) actions and get my original text back.

Incorporate Applescripts and Shell scripts

I use Applescript a lot in my work, and know enough about Shell scripts to be dangerous. To learn more about Applescript, see my series:

  1. Applescript 101
  2. Applescript 102
  3. Applescript 103

Shell scripts are instructions you send directly to your Mac through the Command Line Interface. Take a look at Mac Tip #126/05-Nov-2003, A Historic Terminal for a glimpse into that world, and also because I base an example below on that one.

Use Applescripts

First create your Applescript. For example, this simple script speaks the text Hello Miraz. Most Applescripts would be a lot longer and more complex.

say "Hello Miraz."

Either save your Applescript as a script, or copy all the code, ready to paste into Keyboard Maestro.

Keyboard Maestro script options.

Keyboard Maestro script options.

  1. Make a macro, as explained in previous Tips.
  2. Set a Trigger.
  3. Add an action to Execute Applescript.
  4. Choose to either Execute text scripts or Execute script file from its options. If you choose the ‘text script’ option you then paste your script into the text box, otherwise you then choose the saved script file from your computer.
  5. Choose how to handle the results, such as display results in a window or type results. How you handle results depends on what your script does.

One of the strengths of Keyboard Maestro is that you can add actions before or after the script, or even ‘chain’ scripts together.

When you trigger the macro, Keyboard Maestro runs the script and carries out any other actions you added.

Use Shell scripts

Keyboard Maestro execute a Shell script.

Keyboard Maestro execute a Shell script.

Sometimes when you want to get your Mac to do something for you helpful people on forums and elsewhere will say That’s easy with this Shell script and then produce a bunch of code like this:
cat /usr/share/calendar/calendar* | grep `date +"09/11"`
or like this: pmset -g batt.

Except usually it’s a whole lot longer. One I use is about 100 lines long.

The first example above tells you what happened on a particular date. In the example, the date is 11 September.

The second example shows me how my MacBook Pro’s battery is doing: how much charge is left and how long I can expect to keep using it before the battery runs out.

Keyboard Maestro results of a Shell script.

Keyboard Maestro results of a Shell script.

To use a Shell script you take the same steps as for an Applescript, except that the Action you need is Execute shell script.

I use the option to display results briefly, and because I have Growl installed the info I want shows in a small transparent window that disappears after a few moments.

The window display options are configured inside Growl.

Last in the Series

This is the last in this series of Keyboard Maestro tips, and Peter Lewis, the developer has kindly extended the 20% discount until the end of October 2010.

The About window tells me I’ve saved around 7 hours with Keyboard Maestro. That’ll be for the 4 months or so that I’ve been using this particular Mac.

There’s a lot more to explore in the program, and the Documentation will help you with that. Go and give it a try.

Use the coupon MacTips for a 20% discount on Keyboard Maestro. [Updated] This offer expires at the end of October 2010.

Have you used Keyboard Maestro yet? Tell us about your experiences in the Comments. Have you found any great Tips? Share them with us.

Related posts

[wpzon keywords="pressure cooker" sindex="PCHardware" snode="1232597011" sort="salesrank" listing="8"]