Skip to content

Instantly share code, notes, and snippets.

@alderete
Created September 19, 2022 03:51
Show Gist options
  • Save alderete/d52135da1d186642206aef86b7b7f32e to your computer and use it in GitHub Desktop.
Save alderete/d52135da1d186642206aef86b7b7f32e to your computer and use it in GitHub Desktop.
Apple Mail Load Remote Images
(*
Click the Load Images / Load Remote Content button in an open Mail message.
This is to work around Mail's idiotic inability to Load Images via keyboard shortcut.
Attach this to a keystroke using FastScripts, Keyboard Maestro, or some other utility.
Originally written May 2013 by Michael A. Alderete
Project URL: http://aldoblog.com/link/load-images-applescript
Version History
1.0 -- Initial release, 2013-05-06
1.0.1 -- Consolidate reference to button to a single line, instead of two separate references (never released)
1.1 -- Add OS version detection, use right reference for Mail.app version (never released)
1.2 -- Refactor the OS detection and button reference (never released)
1.3 -- Use a cascade of statements to get various Mail version/config permutations, released 2013-05-27
1.4 -- Updated for Mac OS X 10.9 ("Mavericks"), released 2013-11-23
1.4.1 -- Oops, finish writing the Mavericks dialog box message, released 2013-11-24
1.5 -- Beginnings of internationalization "support": button text is centralized (never released)
1.5.1 -- Add commented-out [Load Images] button text strings for 10 non-English languages (never released)
1.5.2 -- Updated comments and localization / language instructions, released 2014-01-15
1.6 -- Updated for Mac OS X 10.10 ("Yosemite"), released 2014-12-31
1.6.1 -- GUI Scripting check opens the right system preference. Fixed a bug, needs to catch Yosemite, too.
1.7 -- Updated for Mac OS X 10.11 ("El Capitan")
1.8 -- Updated for macOS 10.12 ("Sierra"). Cleanup. Improved GUI scripting message in later macOS versions.
1.8.1 -- Updated with trivial change to try to work with High Sierra. Didn't work.
1.9 -- Updated for macOS 10.13 ("High Sierra"), because Meltdown forced me to upgrade and now I can test.
1.10 -- Updated for macOS 11 ("Big Sur") on 2020-12-12. Started some refactoring on version checks.
1.11 -- Updated for macOS 12 ("Monterey") on 2021-12-14.
Use as you please, give credit if you like.
*)
-- The text of the [Load Images] button in your version of Apple Mail.
-- If you're using a non-English version of Mac OS X, uncomment ONLY the line with your language on it, and save.
-- (To uncomment the line, remove the "--" from in front of it.)
-- If you have additional languages you can send to me, I'd love to get them!
-- Note that I recognize this has to be the worst possible way to localize an AppleScript...
-- Monterey adds a new button name, but not all the time. Need to figure out how to detect the need for this.
-- if (my isMontereyMail()) then
-- set loadImagesButtonNameTextString to "Load Content Directly" -- English
-- else
if (my isYosemiteOrLaterMail()) then
set loadImagesButtonNameTextString to "Load Remote Content" -- English
-- set loadImagesButtonNameTextString to "Carica contenuto remoto" -- Italiano
-- set loadImagesButtonNameTextString to "Cargar contenido remoto" -- Español
-- set loadImagesButtonNameTextString to "Carregar conteúdo remoto" -- Português
-- set loadImagesButtonNameTextString to "Charger du contenu distant" -- Français
-- set loadImagesButtonNameTextString to "Entfernte Inhalte laden" -- Deutsch
-- set loadImagesButtonNameTextString to "Laad extern materiaal" -- Nederlands
-- set loadImagesButtonNameTextString to "Last inn eksternt innhold" -- Norsk bokmål
-- set loadImagesButtonNameTextString to "Läs in fjärrinnehåll" -- Svenska
-- set loadImagesButtonNameTextString to "Indlæs eksternt indhold" -- Dansk
-- set loadImagesButtonNameTextString to "Lataa etäsisältö" -- Suomi
else
-- Versions of Apple Mail / OS X before Yosemite (10.10)
set loadImagesButtonNameTextString to "Load Images" -- English
-- set loadImagesButtonNameTextString to "Carica immagini" -- Italiano
-- set loadImagesButtonNameTextString to "Cargar imágenes" -- Español
-- set loadImagesButtonNameTextString to "Carregar imagens" -- Português
-- set loadImagesButtonNameTextString to "Charger les images" -- Français
-- set loadImagesButtonNameTextString to "Bilder laden" -- Deutsch
-- set loadImagesButtonNameTextString to "Laad afbeeldingen" -- Nederlands
-- set loadImagesButtonNameTextString to "Last inn bilder" -- Norsk bokmål
-- set loadImagesButtonNameTextString to "Läs in bilder" -- Svenska
-- set loadImagesButtonNameTextString to "Indlæs billeder" -- Dansk
-- set loadImagesButtonNameTextString to "Lataa kuvat" -- Suomi
end if
-- You can comment this out after you run it once, for slightly faster performance.
-- It just makes sure you really have GUI Scripting enabled.
GUIScripting_status()
tell application "System Events"
tell process "Mail"
try
-- Verify there is a window where it's possible to load images
-- From <https://discussions.apple.com/message/11234439#11234439>
if not (window 1 exists) then return beep 1
set loadImagesButton to my getLoadImagesButtonReference(loadImagesButtonNameTextString)
-- Make sure there's a [Load Images] button in the window
if not (loadImagesButton exists) then return beep 1
-- Actually click the button
click loadImagesButton
end try
end tell
end tell
-- Check to see if assistive devices is enabled
-- From <http://www.macosxautomation.com/applescript/uiscripting/>
on GUIScripting_status()
tell application "System Events"
set UI_enabled to UI elements enabled
end tell
if UI_enabled is false then
tell application "System Preferences"
activate
if (my isMavericksOrLaterMail()) then
-- Mavericks moves this to the Security preference pane, and has a different UI
set current pane to pane id "com.apple.preference.security"
display dialog "This script uses the Graphic User Interface Scripting architecture of Mac OS X, which is currently disabled." & return & return & "In System Preferences, enable your script trigger utility in the Accessibility section of the Privacy tab of the Security & Privacy preference pane." with icon 1 buttons {"OK"} default button 1
else
set current pane to pane id "com.apple.preference.universalaccess"
display dialog "This script uses the Graphic User Interface Scripting architecture of Mac OS X, which is currently disabled." & return & return & "Enable GUI Scripting by checking \"Enable access for assistive devices\" in the Accessibility preference pane." with icon 1 buttons {"OK"} default button 1
end if
end tell
end if
end GUIScripting_status
-- Get a reference to the [Load Images] button
-- The UI specifier path to the button changes depending on which version of Mail.app is running,
-- whether the message is open in a preview pane or independent message window, and
-- whether you have the "classic" layout enabled (luddites unite!), or use the new sideways layout (like an animal).
on getLoadImagesButtonReference(loadImagesButtonName)
tell application "System Events"
tell process "Mail"
if (my isBeforeLionMail()) then
try
-- Older versions of Mail.app use a simpler UI specifier
-- From: https://discussions.apple.com/thread/2405934
set loadImagesButtonRef to a reference to button loadImagesButtonName of front window
end try
else if (my isLionMail()) then
-- Cascade through a series of attempts to get the button reference
try
-- The [Load Images] button in an independent message window
-- Found with [UI Browser](http://pfiddlesoft.com/uibrowser/).
-- Also described here, but with a different scripting intent:
-- http://superuser.com/questions/421215/mail-app-showing-remote-images-automatically-for-specific-senders
set loadImagesButtonRef to a reference to button ¬
loadImagesButtonName of UI element 1 of row 1 of table 1 of scroll area 1 of front window
end try
if not (loadImagesButtonRef exists) then
try
-- The [Load Images] button in a mailbox preview pane when in "classic layout"
-- Found with [UI Browser](http://pfiddlesoft.com/uibrowser/).
set loadImagesButtonRef to a reference to button ¬
loadImagesButtonName of UI element 1 of row 1 of table 1 of scroll area 2 of splitter group 2 of splitter group 1 of front window
end try
end if
if not (loadImagesButtonRef exists) then
try
-- The [Load Images] button in a message displayed in a mailbox preview pane when in new (non-"classic") layout
-- [Suggested by John Waers](http://aldoblog.com/2013/05/load-images-applescript-for-apple-mail/#comment-26329)
-- and [David Horne](http://aldoblog.com/2013/05/load-images-applescript-for-apple-mail/#comment-26335)
set loadImagesButtonRef to a reference to button ¬
loadImagesButtonName of UI element 1 of row 1 of table 1 of scroll area 1 of splitter group 2 of splitter group 1 of front window
end try
end if
else if (my isMavericksOrLaterMail()) then
-- Cascade through a series of attempts to get the button reference
try
-- The [Load Images] button in an independent message window
set loadImagesButtonRef to a reference to button ¬
loadImagesButtonName of group 1 of group 1 of scroll area 1 of front window
end try
if not (loadImagesButtonRef exists) then
try
-- The [Load Images] button in a message in mailbox preview pane when in "classic layout"
set loadImagesButtonRef to a reference to button ¬
loadImagesButtonName of group 1 of group 1 of scroll area 2 of splitter group 1 of splitter group 1 of front window
end try
end if
if not (loadImagesButtonRef exists) then
try
-- The [Load Images] button in a message displayed in mailbox preview pane when in new (non-"classic") layout
set loadImagesButtonRef to a reference to button ¬
loadImagesButtonName of group 1 of group 1 of scroll area 1 of splitter group 1 of splitter group 1 of front window
end try
end if
else
-- Unsupported version of Mail.app
return beep 2
end if
return loadImagesButtonRef
end tell
end tell
end getLoadImagesButtonReference
-- Mac OS X version detection functions
-- We're not going to attempt to support anything before Tiger (10.4), impossible to test
-- This is a horrible hack, but AppleScript leaves some things to be desired here
on isBeforeLionMail()
-- From: http://stackoverflow.com/questions/498323/find-mac-osx-version-installed-using-applescript
set osVersion to system version of (system info)
if ((osVersion begins with "10.4") or (osVersion begins with "10.5") or (osVersion begins with "10.6")) then
return true
end if
return false
end isBeforeLionMail
-- Lion (10.7) changed the UI specifier for the [Load Images] button
-- Mountain Lion (10.8) kept the same UI specifier
on isLionMail()
set osVersion to system version of (system info)
return ((osVersion begins with "10.7") or (osVersion begins with "10.8"))
end isLionMail
-- Mavericks (10.9) changed the UI specifier for the [Load Images] button. Again.
on isMavericksMail()
return (system version of (system info) begins with "10.9")
end isMavericksMail
-- Yosemite (10.10) uses the same UI specifier as Mavericks, but a [Load Remote Content] button
on isYosemiteMail()
return (system version of (system info) begins with "10.10")
end isYosemiteMail
-- El Capitan (10.11) and later addition is experimental; works for "classic" UI layout
on isElCapMail()
return (system version of (system info) begins with "10.11")
end isElCapMail
-- Sierra (10.12) detection.
on isSierraMail()
return (system version of (system info) begins with "10.12")
end isSierraMail
-- High Sierra (10.13) detection.
on isHighSierraMail()
return (system version of (system info) begins with "10.13")
end isHighSierraMail
-- Mojave (10.14) detection.
on isMojaveMail()
return (system version of (system info) begins with "10.14")
end isMojaveMail
-- Catalina (10.15) detection
on isCatalinaMail()
return (system version of (system info) begins with "10.15")
end isCatalinaMail
-- Big Sur (11.x) detection
-- Currently system version string is "10.16", but might change...
on isBigSurMail()
set osVersion to system version of (system info)
return ((osVersion begins with "10.16") or (osVersion begins with "11"))
end isBigSurMail
-- Monterey (12.x) detection
-- Currently system version string is "12.1", but might change...
on isMontereyMail()
set osVersion to system version of (system info)
return ((osVersion begins with "10.17") or (osVersion begins with "12"))
end isMontereyMail
-- Semantically grouping different functional groups; this is gross, but...
on isYosemiteOrLaterMail()
return (my isMontereyMail() or my isBigSurMail() or my isCatalinaMail() or my isMojaveMail() or my isHighSierraMail() or my isSierraMail() or my isElCapMail() or my isYosemiteMail())
end isYosemiteOrLaterMail
on isMavericksOrLaterMail()
return (my isYosemiteOrLaterMail() or my isMavericksMail())
end isMavericksOrLaterMail
-- Copied from C-Command script for EagleFiler
on isLionOrBetter()
considering numeric strings
return (system version of (system info)) ≥ "10.7"
end considering
end isLionOrBetter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment