Add a Keyboard Shortcut to Mail’s Load Images Button

by Michael Alderete on 5/6/2013 · 17 comments

I like Apple Mail for Mac OS X, but one thing that irritates me to no end is that the Load Images button, which loads the images in a message on-demand, doesn’t have a keyboard shortcut. Because it’s not a menu item, there’s no easy way to add one. I looked for a way to add a keystroke to Load Images for more than five years before I finally found a way to do it.

The hard part is triggering the Load Images button itself, by anything besides a mouse click. What finally did it was using the GUI Scripting feature of AppleScript to tell the button to click itself. Once I had an AppleScript that worked, it was simple to use FastScripts to trigger the script with a keyboard shortcut.

Why don’t I let Mail load images automatically? Spammers use image loading to confirm the validity of an email address. See this Apple Support Forum post for some details, and how to turn it off.

AppleScript to Load Images in Apple Mail

Download the Load Images script v1.4.1, 2013-11-24

The work of the script can be done by a single line that might look like this:

click button "Load Images" of 
    UI element 1 of row 1 of table 1 of scroll area 1 of front window

(Because Mail.app’s user interface has changed across various versions, and with different preference settings, the actual code is more complex. See the AppleScript for the full, ugly details.)

This user interface elements path, or specifier, tells the GUI Scripting system how to reach a specific user interface element, in this case a button, and what to do with it. It’s far from obvious how to determine one of these UI specifiers just by looking at a Mail window; I used the very interesting developer tool UI Browser to figure it out.

It’s also specific enough to be brittle; it has changed from prior versions of Apple Mail, and will probably change in the future. When it does this script will stop working and start beeping. I’ve written the script to adapt to a few different version and configuration possibilities, and I’ll try to update it if it ever breaks. The current version (v1.4) is compatible with Mac OS X 10.5 (“Tiger”) through Mac OS X 10.9 (“Mavericks”).

The rest of the script is error handling and AppleScript ceremony. It should make sense if you’re familiar with AppleScript, and there are plenty of comments, so I won’t describe it here.

Add a Keyboard Shortcut for Running the AppleScript

Adding a keyboard shortcut for an already-existing menu item is easily done using the Keyboard preference pane. Adding a keyboard shortcut for an AppleScript requires a third-party utility. You may already have a favorite—there are lots to choose from—and so do I. FastScripts from Red Sweater Software does a great job handling application-specific AppleScripts, and it’s free if you don’t need more than 10 keyboard shortcuts. (It’s also a good value at $14.95 for the unlimited version.)

Create the shortcut for the AppleScript

  1. Install and run FastScripts, and then switch to Mail.app.
  2. From the FastScripts menu, choose Open Mail Scripts folder. Open Mail Scripts folder menu item
  3. Move or copy the Load Images script into the Mail Scripts folder.
  4. From the FastScripts menu, choose Preferences…, click the Script Shortcuts tab, and add your preferred keyboard shortcut. Keyboard shortcut for Load Images AppleScript

Note: If you use the same keyboard shortcut I did, Command-Shift-L, you’ll need to change the keyboard shortcut for the existing menu command that uses it, by adding a new one to that command, using the technique at the Mactuts+ article linked above.

{ 17 comments… read them below or add one }

John Waers May 21, 2013 at 8:35 am

THANK YOU! This issue has frustrated me for years, and failing in my own AppleScript attempts, I’d resorted to many fruitless suggestions to Apple via their feedback page. Google searches led nowhere until today, when I found your May 6 post at forums.macresource.com in response to the “Stupid Question™…” with a link to your blog.

Your script works great for an open Mail message but not for messages displayed in a mailbox window. The following modified script works for both cases.

tell application "System Events"
    tell process "Mail"
        if not (window 1 exists) then return beep 1
        try -- message displayed in a mailbox window
            click button "Load Images" of UI element 1 of row 1 of table 1 of scroll area 1 of splitter group 2 of splitter group 1 of window 1
        end try
        try -- message displayed in a separate mail window
            click button "Load Images" of UI element 1 of row 1 of table 1 of scroll area 1 of window 1
        end try
    end tell
end tell

Reply

Michael Alderete May 23, 2013 at 12:37 am

This is great to know, thanks for posting! At first I didn’t get what your problem was, but I forget that not everyone turns back the UI in Mail to 1999 like I do. Of course my UI reference wouldn’t work if you’re using the preview pane or, as @DavidHorne pointed out, if you’re using the new Mail layout. I’ve been working on an updated version of the script to handle multiple versions of Mail, and it should be possible to also handle multiple layouts in the same way. Look for an updated version of this script some time during or after the Memorial Day holiday weekend. Thanks!

Reply

David Horne May 21, 2013 at 11:23 am

Thank you. I wouldn’t have thought of using GUI scripting. Previously I had a script blindly clicking in the right location. But when I upgrade to Mac OS X 10.8.3 it broke. Unfortunately your solution didn’t work out of the box right away. So I download UI Browser and found that the following worked in Apple Mail 6.3 with the message select on the side and not the top:

click button “Load Images” of UI element 1 of row 1 of table 1 of scroll area 1 of splitter group 2 of splitter group 1 of window 1

So yeah, very brittle.

Reply

Michael Alderete May 23, 2013 at 12:34 am

Wow, fabulous, so the script not only needs to care which version of Mac OS X (and thus Mail.app) it’s running on, but also the layout of the window. Great to know, and maddening to learn that Mail is such a PITA. I’ve been working on an updated version of the script that can handle multiple versions of Mail, and @JohnWaers’s idea of using the try several times seems like a good approach. Look for an update some time during or after the Memorial Day holiday weekend.

Reply

Erik June 3, 2013 at 6:50 pm

Awesome. Thank you. Do you think a similar script could enable one to invoke a custom print-to-pdf option using keyboard-only? e.g. http://macscripter.net/viewtopic.php?pid=163967

Reply

Michael Alderete June 6, 2013 at 10:38 pm

@Erik: Hmmm. Yes, I’m sure you can accomplish this, with the right amount of experimentation and tenacity. From the look of the scripts posted at that URL, it will take a bit of both!

But, do you need it to all come from one keyboard shortcut? Would it be OK to press Command-P to open the print dialog, and then another keyboard shortcut (Control-Option-A?) to choose to save it as a PDF to the ACTION PDF option? If that’s OK, you can accomplish that without bothering with scripting. Just add a keyboard shortcut to your PDF workflow, using the instructions in the article Keyboard Shortcut for “Save as PDF…” in OS X at MacSparky.

I just tested it on Mac OS X 10.8 (Mountain Lion), and it worked fine for my Save to Yojimbo workflow.

Keyboard shortcut for PDF Workflow

The same trick should work for any PDF workflow, and this will give you the ability to easily have multiple workflows that you give shortcuts to, instead of having to write many scripts that will probably be fragile…

Reply

Tripp December 3, 2013 at 7:37 pm

Print to PDF is a piece of cake. At least it has been up to and including Mt lion. All you need to do is add a keyboard shortcut in Preferences. Command-P works fine. It seems that once the print dialog is open Command-P becomes an available shortcut. All I do is press and hold the Command key and then press the P key twice, and I am taken immediately to the Save As PDF dialog.

This shortcut works great as I use this several times a day every day in Safari and my girlfriend uses it in all applications with regularity.

It is worth noting that every two or three weeks Safari gets confused and Command-P quits working for the Print dialog. A restart of Safari is all that is need to get things going again.

Reply

Michael Alderete December 3, 2013 at 7:43 pm

@Tripp: What a great tip! I didn’t know that Command-P gets freed up when the Print dialog opens. What a great time-saver for printing to PDF. I think this one is so good, I’ll write it up as a full post! (If you’ve written it up somewhere else, let me know, and I’ll just link there instead. Credit where credit is due.)

Reply

Patrick October 17, 2013 at 5:54 am

Just wanted to add my thanks to your very helpful instructions. Just finished getting it installed and working and LOVE the shortcut to loading images. Can’t believe Apple won’t simplify things like this. I love their products but wonder sometimes at their feature designers…

Reply

Sascha January 5, 2014 at 4:09 am

Hi, thank you for providing the script. Unfortunatly it’s not working on my MBP (with 10.8 latest and its Mail.app). I used it as provided from the Script-Menubar and also exchanged “Load images” to “Bilder laden” due to my systemwide language settings (German). But none version worked, I didn’t event get any alarm or beep.

Any idea what went wrong? Thank you and happy new year!

Reply

Michael Alderete January 5, 2014 at 7:55 pm

@Sascha: Hmmm. It sounds like it’s almost certainly an internationalization problem, which I just won’t have any idea how to deal with. I just don’t have access to machines to test on. The only thing I can suggest is making sure that, when you change the text in the script (a) change it every place it occurs (I count at least seven instances of that string; I just revised the script to make this easier), and (b) make sure the text string matches the button text exactly, including capitalization. So, if you changed the script to look for a button with the text “Bilder laden” but the actual text is “Bilder Laden” (capitalized “L”), it won’t work. Verify the text is the same.

I don’t know if either of these will help, but let me know if it does, and I’ll publish the updated script.

Reply

Michael Alderete January 18, 2014 at 10:38 am

OK, an internationalized version of the script is now available. The download link above gives you the latest version, and the details are available in Load Images Script Updated for International (Non-English) Use

Reply

Joel February 17, 2014 at 10:52 am

This is great! I copied the script into an automator Service with no input for Mail and was able to assign my shortcut there without having to use a third-party app.

Reply

Michael Alderete February 17, 2014 at 7:27 pm

@Joel: That’s a neat way to add a keystroke, thanks for mentioning it! There’s lots of different ways that should work, the tricky thing is the AppleScript part. Use whatever works best in your setup. Cheers!

Reply

David Göhler March 4, 2014 at 1:19 am

Hi Michael,

This Script works great for me - in Mac OS X 10.8, but not with Mac OS X 10.9 (unfortunately). I’m using Mail-Act-On and MailHub (which shows an additional button line between the window buttons and the favorite folders).

In Mac OS X 10.8 your script works (even with MailHub enabled). Any idea to solve this?

Reply

Michael Alderete March 4, 2014 at 8:54 pm

@David: Hmmm, it has to be that the UI Scripting path to the Load Images button is changed by MailHub. Interesting that it happens only on Mavericks. Off the top of my head, I don’t know what the script changes are to address this, but I would imagine it’s not too hard to change. I just need to install MailHub on my own system, and figure out the UI path to the altered location of the Load Images button. I’ll look into doing that this weekend. (Ping me if you don’t see an update within a week.)

FWIW, I’m using Mac OS X 10.9 (“Mavericks”) myself, with Mail Act-On, Mail FollowUp, Attachment Tamer, QuoteFix, and the plugins for MsgFiler and SpamSieve, and it’s been working just fine for me. So, it’s only going to be the UI-altering plugins that cause issues. I don’t know that I can support every possibility here, but perhaps I can provide instructions for folks who choose to use tools I don’t myself.

Reply

Alan Weiss June 5, 2014 at 3:32 pm

Let me add another big THANK YOU!! I, too, have wanted a way to load images from the keyboard for a long time.

I had tried using this script on my old Snow Leopard system but couldn’t get it to work. I recently got a new MacBook Pro running Mavericks, and your script works great, though the first time I ran it I was prompted to go into System Preferences and change the setting in Security & Privacy > Privacy > Accessibility to allow FastScripts.app to control my system, so you might want to add a note about that to your installation instructions. I hadn’t had FastScripts installed until now; I got it just for this purpose (though after I installed it I noticed the note from Joel pointing out that I could just have used an Automator service instead).

Reply

Leave a Comment

Previous post:

Next post: