Skip to content

Instantly share code, notes, and snippets.

@stevenschobert
Last active January 12, 2022 11:19
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenschobert/f559be35190bd432fcce8f4febd713ef to your computer and use it in GitHub Desktop.
Save stevenschobert/f559be35190bd432fcce8f4febd713ef to your computer and use it in GitHub Desktop.
Apple Script to click the "Load Remote Content" button. I like to disable all remote content in Mail.app, and then bind this script to a keyboard shortcut using Keyboard Maestro. https://www.keyboardmaestro.com
tell application "System Events" to tell process "Mail"
set mainWindow to a reference to the first window
set rootSplitter to a reference to the first splitter group of the mainWindow
set firstSplitter to a reference to the last splitter group of the rootSplitter
set scrollArea to a reference to the last scroll area of the firstSplitter
set scrollGroup to a reference to the first group of the scrollArea
if number of groups of the scrollGroup is greater than 1 then
set maybeRemoteContentGroup to a reference to the first group of the scrollGroup
if number of buttons of the maybeRemoteContentGroup is greater than or equal to 1 then
set maybeRemoteContentButton to a reference to the last button of the maybeRemoteContentGroup
if name of the maybeRemoteContentButton contains "load remote content" then
click the maybeRemoteContentButton
else
name of the maybeRemoteContentButton
end if
else
UI elements of maybeRemoteContentGroup
end if
else
UI elements of the scrollGroup
end if
end tell
tell application "System Events" to tell process "Mail"
set mainWindow to the first window
set mainScrollGroup to the first scroll area of the mainWindow
set everyMessage to every group of the mainScrollGroup
log (number of everyMessage)
repeat with currentMessage in everyMessage
set loadRemoteContentButton to the first button of the first group in the currentMessage
click the loadRemoteContentButton
end repeat
end tell
@PARADicmSHIFT
Copy link

I have taken the main script and put into an Automator workflow. This can then be used as a system service, and assigned a keyboard shortcut with any 3rd party app requirement. With your permission and credit, I'd like to share it.
See here: https://app.box.com/s/36nin8u81h5er4mszzlxni73h7ieh5eq
Many thanks for the code.

@zonetuke
Copy link

zonetuke commented Aug 8, 2016

I get the following error when running the Automator workflow. Running MacOS 10.11.6.

screen capture 2016-08-07 at 9 50 48 pm

@paulrudy
Copy link

paulrudy commented Dec 17, 2016

Thanks for this. I made a version that combines the two, testing first if the frontmost window is a message viewer window. Feel free to use.

http://pastebin.com/XjnxZHus

@rafisaar
Copy link

rafisaar commented Mar 7, 2018

1 - Thanks for this script!
2 - Instead of using Keyboard Maestro, I put the script in an Automator Service which shows up in Mail and I added a keyboard shortcut to it through the System Preferences (like PARADicmSHIFT suggested as well)
3 - I have MailButler installed, which sometimes adds additional buttons after the "Load Remote Content" button.
In the following line from paulrudy's edited script:
set maybeRemoteContentButton to the last button of the maybeRemoteContentGroup
you choose the last button - so in my case this won't work. But the fix is very simple: just change last with first.
The button is actually the 2nd element of the scrollGroup, but it's the 1st button so with my change the code is always correct (in my case).

Thanks!

@LwsBtlr
Copy link

LwsBtlr commented Apr 16, 2018

This doesn't work when messages are in a thread (well, it only loads content of the first message).

@alecvance
Copy link

alecvance commented Dec 5, 2019

I combined some of the above ideas (https://gist.github.com/stevenschobert/f559be35190bd432fcce8f4febd713ef#gistcomment-1826177 and https://gist.github.com/stevenschobert/f559be35190bd432fcce8f4febd713ef#gistcomment-1950364) to create this Automator script that will work if the message is seen in a sidepanel or is in a separate window:


on run {input, parameters}
	
	tell application "System Events" to tell process "Mail"
		
		set mainWindow to a reference to the first window
		
		try
			set rootSplitter to a reference to the first splitter group of the mainWindow
			set firstSplitter to a reference to the last splitter group of the rootSplitter
			set scrollArea to a reference to the last scroll area of the firstSplitter
			set scrollGroup to a reference to the first group of the scrollArea
			
			if number of groups of the scrollGroup is greater than 1 then
				set maybeRemoteContentGroup to a reference to the first group of the scrollGroup
				
				if number of buttons of the maybeRemoteContentGroup is greater than or equal to 1 then
					set maybeRemoteContentButton to a reference to the last button of the maybeRemoteContentGroup
					if name of the maybeRemoteContentButton contains "load remote content" then
						click the maybeRemoteContentButton
					else
						name of the maybeRemoteContentButton
					end if
				else
					UI elements of maybeRemoteContentGroup
				end if
			else
				UI elements of the scrollGroup
			end if
			
		on error theError
			# my regularmessage()
			
			set mainScrollGroup to the first scroll area of the mainWindow
			set everyMessage to every group of the mainScrollGroup
			
			log (number of everyMessage)
			repeat with currentMessage in everyMessage
				set loadRemoteContentButton to the first button of the first group in the currentMessage
				click the loadRemoteContentButton
			end repeat
			
			
		end try
		
		
	end tell
	
	
	return input
end run

@alecvance
Copy link

Well for some reason, the script will no longer run successfully in the Services menu. I see the gear icon in the menubar still. It will run successfully from Automator as a workflow, but that's it.

@lothar-cell
Copy link

lothar-cell commented Oct 28, 2020

The script doesn't work anymore on MacOS 10.14.6. Script Editor says:

error "System Events got an error: Can’t get window 1 of process "Mail". Invalid index." number -1719 from window 1 of process "Mail"

Does anyone know how to update it?

@nicolaibach
Copy link

Thank you for this script! It was driving me crazy that there is no menu item for this action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment