MacPowerUsers

Latest

  • Auto-Save Microsoft Word with Keyboard Maestro

    by 
    TJ Luoma
    TJ Luoma
    04.16.2014

    In Mac Power Users Episode 187, David Sparks and Katie Floyd talked about word processing apps, which meant talking a great deal about Microsoft Word, as well as Pages and others. Around the 38m40s mark, Katie mentioned that she uses Word a lot in her day job, and has a lot of issues with the app freezing up periodically. That led her to make a comment about intending to write a Keyboard Maestro macro to save every minute when using Microsoft Word. Having recently finished my D.Min. thesis in Microsoft Word[1], I already had a macro for that. As I was writing this article, it also occurred to me that I would like to save when Microsoft Word deactivates (is no longer the front-most app). However, even if you don't use Keyboard Maestro, I encourage you to read on, because I'm going to offer another suggestion at the end. Auto-Save every X minutes (or seconds) This probably is not what you want, although there is no harm in setting it. Microsoft Word does have an auto-recovery setting, but nothing beats the security of manually saving. Unlike modern apps, Microsoft Word does not support the versioning and auto-save features in OS X. Keyboard Maestro can run that save command every X seconds or minutes. The macro is designed to run only when Microsoft Word is active, and I set mine to run every minute. However, with Keyboard Maestro I don't have to just blindly save every sixty seconds, I can check specific criteria. I chose two: The front window of Microsoft Word exists (this means that there is an open document window in Word) A menu item "Save" is enabled (you'd find it under the "File" menu) Note that when the "Save As" window is open in Microsoft Word, the "Save" menu is disabled. That is not true in all apps, so if you implement this macro for another app, you may want to check that a button "Save" does not exist. Here is how those two conditions appear in Keyboard Maestro: Next we get to the actions section, and I included two here, although many people might choose to delete one of them. The first is a notification which will appear and inform the user that the macro is running. After awhile, that might get annoying, but I suggest using it until you get used to the idea, as it will help reinforce the fact that the macro is actually running. The second is the action which actually saves the document. You can do this one of two ways in Keyboard Maestro: Choose the menu item "Save" under the "File" menu, or Simulate ⌘ +S Most times I prefer to have Keyboard Maestro use the file menu, rather than keyboard shortcuts, because it's easier to understand what the macro is doing when I'm reviewing them in Keyboard Maestro. However, ⌘ +S is perfectly clear, and it should prevent the macro from causing any problems if I happen to be typing when it runs. So I recommend having Keyboard Maestro use keyboard shortcuts when it makes sense to do so. The only downside to this is that if you are navigating through Microsoft Word's menus at the same time that the macro runs, the menu will close, but the same problem will occur if we tell our macro to use the menu item File » Save. Auto-Save Microsoft Word when you switch to another app Saving your document while you are using Word is great, but what happens when you switch to another app? If you have been editing that Word document for at least a minute, it should have saved a recent copy, but wouldn't it be great if you could save every time you switched away from Word? But how? Obviously once we have switched away from Word, pressing ⌘ +S isn't going to save the current word document. Couldn't we tell Keyboard Maestro to use the menu item for Word, even after Word is no longer the front-most app? Nope. I learned this one the hard way with another app. The reason it won't work is obvious once you think about it: you switch away to another app, then Keyboard Maestro switches back to Word to save... then what happens? Well, either a) Keyboard Maestro leaves you in Word, which isn't what you wanted, or b) it switches you to away to another app... which will trigger the Keyboard Maestro macro to run again. We have now discovered something called an infinite loop, and I don't mean the address in Cupertino. So how can we do this? The answer is AppleScript: During the Mac Power Users episode, David mentioned several times that Microsoft Office has great AppleScript support, and he even mentioned Ben Waldie whose article is where I found this simple AppleScript command which tells Microsoft Word to save its current document.[2] This AppleScript command is nearly magic for three reasons: It does not require Microsoft Word to be active (front-most) in order for it to work, nor does it bring Microsoft Word to the front when it runs. If there is no active document, the AppleScript command will not cause any noticeable errors: no system beep, nothing. If you are navigating Word's menus when the AppleScript command is triggered, it will wait until the menu closes before running. Remember how I said that ⌘ +S had one downside? Well, this AppleScript command doesn't have it. Remember those conditions that we had to check for ("Does Microsoft Word have an existing front window? Is the 'Save' menu enabled?")? Now we don't need to worry about any of that. In fact, we no longer need two separate macros, all we need is one macro which will run: Every minute when Microsoft Word is active Any time that Microsoft Word deactivates Since we no longer need to check for any more conditions, all we need is one action, that AppleScript command. So now the whole thing just looks like this: Of course I added some additional comments and notifications, which you can delete if you don't want them. Installation To install this: download the macro, double-click on it, and it will be imported into Keyboard Maestro. Bonus Tips As mentioned above, Microsoft Word does not support OS X versions; however, if you save your Word documents to Dropbox it will save all of your changes for 30 days. Don't want to use Dropbox, but want locally saved versions? Take a look at ForeverSave 2 which not only gives you "versions" (even for apps that don't support OS X's implementation) but also can simulate ⌘ + S every X seconds (which means you wouldn't necessarily even need my Keyboard Maestro macro, although it's a more elegant and complete solution). There is a free demo version of ForeverSave 2 available, and a license is US$20. I have been using it and think it is quite useful. Because I had to, that's why. David went to great lengths to say that he doesn't think Word is as bad as its reputation, and a lot of it is leftover anti-Microsoft sentiments or something. I disagree. For a big project, MSWord was a giant PITA. Besides that, even with The Ribbon, by default what you get when you create a new document in Word just looks... gross. The good news is you can make it look much better. ↩ The only change I made was to add the check to see if Word is running, because if you quit Word that counts as deactivation which would trigger the AppleScript which would launch Word. ↩

  • Launch your favorite apps as needed with Keyboard Maestro

    by 
    TJ Luoma
    TJ Luoma
    03.14.2014

    On Mac Power Users 181, I mentioned that I have a keyboard shortcut for OmniFocus' "Quick Entry" which works even if OmniFocus isn't running. Tom Siko asked if I'd be willing to share it, so here it is. Even if you don't use OmniFocus, this idea can be re-used for any application that you run via keyboard shortcut, such as Skitch (or, my preference, Skitch 1). First, here's the general idea... The idea behind this is fairly simple: Imagine you have some keyboard shortcut in your head, such as ⌘+⇧+5 for Skitch, or ⌘+⇧+O for "Quick Add" to OmniFocus. Over time, you learn that whenever you want to do {thing} you press {this keyboard shortcut}. But there's one problem: those keyboard shortcuts only work if the app is running. So what do you do? The usual solution is to have those apps (or some kind of "helper app") launch at login. But why have something launch before you need to use it? Especially with an SSD, launching an app only when it's needed will only take a second or two. Likewise, why keep an app running once you are done using it? Rather than have a bunch of these apps launch at login, I created several Keyboard Maestro macros to watch for the keyboard shortcuts, and then check to see if the related app is running. If it is, then Keyboard Maestro does nothing. If it isn't, then Keyboard Maestro will launch the app and trigger the action that was associated with the keyboard shortcut. The Keyboard Maestro "workflow" for this is very straightforward: When a certain keyboard shortcut (or "hot key" in Keyboard Maestro terminology) is pressed, have Keyboard Maestro run an "If Then Else" test: If the desired app is not running, then: Launch it Pause until it is running Activate YourApp Trigger the desired action else (that is, if YourApp is running): {This section intentionally left blank} The "Else" section is left empty, because if the app is running, we don't want Keyboard Maestro to do anything when the keyboard shortcut is pressed, because YourApp will. "OK, I get the idea, show me how you do it..." Some of you may already understand how to do this, but some of you may want to see how it actually looks in Keyboard Maestro. So here is a screenshot of my Keyboard Maestro macro for Skitch: Note: a larger version of this image is available on Github. Each one of those "blocks" in Keyboard Maestro is just something that I have clicked and dragged from Keyboard Maestro's list of available actions, and selected from the various drop-down menus. Building something like this is really not much more difficult than writing a Mail.app rule. With any multiple-step macro like this, the key to making it work is to make sure that "Step 2" doesn't try to run before "Step 1" has a chance to finish, and so on. Keyboard Maestro lets you choose between either waiting for a certain number of seconds, or waiting until certain conditions are met. For example, if I could tell Keyboard Maestro to launch (or "activate") Skitch, and then tell it to pause for 5 seconds before going on to the next step. I tend to prefer using conditions because they are a little less error-prone. If your computer is doing something else which makes it a little slower, it may take longer than 5 seconds, or it may take fewer than 5 seconds, especially if your computer is idle and has an SSD. So in the macro I told Keyboard Maestro to launch Skitch and then wait until there is a menu item called "Crosshair Snapshot" (which is what Skitch calls the command I associate with ⌘+⇧+5). When Keyboard Maestro sees that menu item available, I know that Skitch is now ready, so I tell Skitch to select the menu item "Crosshair Snapshot" from the "Capture" menu in Skitch. (Note: I didn't have to fill in the menu title or menu item names either. Keyboard Maestro did that when I clicked on the "Menu" button near the right. It shows me all currently available apps and all of their menus, so I just have to select the right one, and it fills in the exact details for me.) At the bottom of that macro window you see that the "otherwise execute the allowing actions" section is blank ("No Action"). This is the "Else" part of the "If Then Else" which started with "If Skitch is not running" so logically this section will only match if Skitch is running, in which case Skitch will see that I have pressed ⌘+⇧+5 and respond accordingly. After I have used Skitch I can either leave it running if I think I might use it again, or I can just quit it, knowing that Keyboard Maestro will launch it again later if necessary. Note! Here's one important potential "gotcha" for this: Skitch has an option in its preferences which will allow it to run only in the menu bar and not in the Dock. If you choose not to have it run in the dock, it will not have menu items that Keyboard Maestro can access! What do you do in that scenario? Just change Keyboard Maestro's conditional so that (instead of pausing until it finds a menu item) it will "Pause until Skitch is running" and then tell Keyboard Maestro to simulate the keyboard shortcut ⌘+⇧+5! Did I just blow your mind a little? You press ⌘+⇧+5 and then having Keyboard Maestro press ⌘+⇧+5 again. It's like having a sandwich that can make you another sandwich. OmniFocus My keyboard shortcut for OmniFocus Quick Entry is ⌃ + ⌥ + ⌘ + O, which might sound crazy, but as I explained on MPU, I have set my right ⌥ key and Caps Lock to equal ⌃ + ⌥ + ⌘, so when I want to add something to OmniFocus, I press Caps Lock + O or Option_R + O. The key is to make sure that whatever keyboard shortcut you have in OmniFocus' preferences here: matches whatever you use in the associated Keyboard Maestro macro under "This hot key": As before, a larger version of this image is on Github. Same idea as before: if OmniFocus is not running, activate it, pause until it is running and a "Show Quick Entry" menu item exists, and then select the menu item. If OmniFocus is running, Keyboard Maestro does nothing, and the Quick Entry window will simply appear as usual. These are only two examples, but the same idea applies to any application that you invoke via a keyboard shortcut. It is a handy way to avoid having a lot of apps launch at login, and allows you to only have them running when you need them. Download and Install If you would like to use my Keyboard Maestro macros as shown above, download the zip file from Github, unzip it, and inside you will find both .kmmacros files. Double-click on them to import into Keyboard Maestro, and you're ready to go! Any updates to these macros will be posted on the Github page, and if you have any problems getting these to work, you can send them to me there or ask me on Twitter. If you haven't already listened to Mac Power Users 181, I share a whole host of other nerdy automation tips there too.

  • Toggle JavaScript in Safari with Keyboard Maestro

    by 
    TJ Luoma
    TJ Luoma
    03.09.2014

    I was very pleased to be a guest on Mac Power Users Episode #181. We had a great conversation about all things automation on the Mac, but Katie and David both seemed to like one Keyboard Maestro macro in particular. Three years ago, I wrote about how to Quickly enable or disable JavaScript in Safari, but had not yet come up with the Keyboard Maestro macro. After we finished recording the show, I decided to "flesh it out" a little with some additional error checking. In the course of doing so, I learned a new Keyboard Maestro feature thanks to its developer, Peter Lewis. The macro itself is fairly simple: it is designed to be used in Safari, a simple press of ⌘ + J will toggle Safari's JavaScript setting on or off, and then reload the current page (if any). Although the macro is simple, it is very convenient for those sites which are filled with all sorts of annoyances and distractions. The second macro uses ⌥ + J to only display the current JavaScript status in Safari, without changing it. If you are impatient, you can jump over to the Github page for these two macros where you can download the macros. If you want to learn more about how it works (and the new 'trick' I learned in Keyboard Maestro), read on. Building the macros Most Keyboard Maestro macros fall into one of two categories: "global" macros, which should work anywhere, and "local" macros, which are only meant to work in a specific app (or group of apps). For example, most people are familiar with the keyboard shortcut ⌘ + space to start a Spotlight search. That keyboard combination should work in any application. That's what I mean when I say "global" –- it works at any time, in any application. Contrast that with something like ⌘ + Y which may or may not do something, depending on which app you are in, and it will most likely do something different in different applications. That would be an example of a "local" macro. The reason I mention this is that these two macros which I have created are meant to be local macros, in the sense that they are only meant to be used when you are using Safari. For example, if you press ⌘ + J while you are using the Finder, you don't want Keyboard Maestro changing your JavaScript setting in Safari! To limit these to just Safari, the macros need to be placed in a Keyboard Maestro "Group" which has been limited to just be active in Safari. Don't get overwhelmed! I'm telling you this detail to help you understand how it works, but if you use my macros you won't have to worry about creating this Keyboard Maestro Group, it will be done for you. But it's important to understand how it works and why it is important. It's really not as complicated as it sounds; hopefully, this screenshot will help: A larger version of this screenshot is available on Github. In the left column, you can see a group called "Safari Only" Macros has been created. This group is like a folder which can hold any number of macros. In the second column you can see that I have 4 macros in that group (the 2 mentioned here and 2 others which are unrelated). In the right-most part of the window you can see that the "Safari Only" Macros have been set as only available in Safari. (If you wanted to rename the Group to something other than "Safari Only" Macros you would do that here.) When you import the macros I have created, Keyboard Maestro automatically creates the "Safari Only" Macros group, and it knows that these macros are only to be used in Safari. I mention it here only as explanation; you should not need to do anything in Keyboard Maestro to get them into this group. Once you have imported the macros into Keyboard Maestro (either by double-clicking on the .kmmacros file or by choosing File » Import... in Keyboard Maestro) you can look through the macros to see how they were put together. Do not be overwhelmed! These macros may look complicated, but that's mostly because I was very thorough, and added a lot of error-checking that I will admit I don't always include. What these macros do is really quite simple. Look at this menu item, and notice that in one there is a checkmark, and the other there isn't: I previously suggested you could go into System Preferences and assign a keyboard shortcut to "Disable JavaScript." The reason that Keyboard Maestro is better than that is that Keyboard Maestro's macros will sync via Dropbox, which means that if I create a macro once I will have it on all of my Macs, instantly. Maybe you're thinking, "But isn't a keyboard shortcut easier?" Well, maybe, but at its core, the macro to toggle this setting simply chooses that menu item, then it looks to see if the "Reload Page" item (under Safari's "View" menu) is enabled. If it is (meaning Safari has already loaded a page), it reloads the page, otherwise it stops. Now, already I have improved upon a keyboard shortcut, because I'm getting two actions for one keyboard shortcut. With a few minutes of extra work, the macro can be easily expanded, and the benefit of those extra minutes will benefit me every time I use this macro in the future. So here are the extra features I added into the macro: If there is no menu item 'Disable JavaScript' then chances are that the user has not turned on the "Show Develop menu in the menu bar" option in Safari's "Advanced" preferences. Oops! But we don't have to just give up; we can use Keyboard Maestro to open Safari's preferences (using the menu item), then select the "Advanced" preferences (telling Keyboard Maestro to press the 'Button' labeled "Advanced"), and then press button "Show Develop menu in the menu bar" which should have the effect of checking the box and enabling the menu. Just in case, we leave the Preferences window open, and open an alert window using a shell script set to 'Display results in a window' telling the user what should have happened, and what to do next. The macro is then canceled. It is easy enough for the user to press ⌘ + J again, if desired. Now if I reinstall this macro on a new Mac (remember, it syncs via Dropbox, so I can do that very easily) it will help me customize Safari even if I have forgotten to turn this option on! It is important to notice that, unlike some menu items, the Disable JavaScript menu does not change when it is toggled. For example, Safari's "Show Status Bar" menu changes to "Hide Status Bar" when the setting is changed. Ideally, "Disable JavaScript" would change to "Enable JavaScript" but, alas, all that happens is that a check mark is added or removed. Fortunately, Keyboard Maestro is smart enough to be able to tell the difference between "Disable JavaScript" and "Disable JavaScript with a check mark" and we can test for it by looking to see if the menu item "is marked" as shown here: If you were to click on "is marked" you would see another option for "is not marked" which means that now we can tell whether JavaScript is currently enabled or disabled! (This is the "new Keyboard Maestro trick" I learned while writing this.) With a little bit of effort we can not only tell the user (using OS X's built-in notification settings) whether they have just enabled or disabled JavaScript (rather than the generic "toggled"), and if the user uses the new companion macro (⌥ + J instead of ⌘ + J) we can use a notification to tell the user whether JavaScript is enabled or disabled without changing the setting. What problem did we solve, and how much effort did it take? JavaScript has a lot of good and beneficial uses, but it can also be used to have parts of a web page move around while you scroll, or have something jump up and block what you were trying to read. If you have been annoyed by something on a website, chances are good that disabling JavaScript would make it stop. Apple has made it very simple to disable JavaScript in Safari, but it's not very convenient. After all, once you disable JavaScript, you'll probably want to (or even need to) reload the page. Being able to do that with a keyboard shortcut is much more convenient. (Oh, and if you'd prefer to be able to trigger this macro using your mouse, Keyboard Maestro can do that as well.) So we've solved a real problem that can be a daily annoyance when browsing the web by making it convenient to turn JavaScript on or off, and making it even easier to check to see if it is off or on. Creating this macro probably took me about 10 minutes, but I will benefit from it every single day that I use Safari, on every Mac that I own, into the foreseeable future. Every time I press ⌥ + J or ⌘ + J, I'm going to be just slightly happier than I would have been without it, and I'm going to be slightly happier just after a moment of frustration ("Why is this site doing that?! Well, I can fix this easily enough.") This is the one of the benefits of doing these bits of automation on your Mac: it may not change the world, but it makes your part of it a little better and more enjoyable. What may be the best part of this particular effort, for me, is knowing that Katie Floyd and David Sparks are probably going to be using this too, as well as some listeners to the Mac Power Users podcast, and some people reading this article. So I've made their part of the world just a little better too.