Toggle JavaScript in Safari with Keyboard Maestro

Sponsored Links

Toggle JavaScript in Safari with Keyboard Maestro

Keyboard Maestro app iconI 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:

Keyboard Maestro group for Safari

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:

Safari Menu for Disable JavaScript, Checked

Safari Menu for Disable JavaScript, Unchecked

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:

Keyboard Maestro condition equals Is Marked

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.

Popular on Engadget