Automatically open specific URLs in incoming emails

Hi,

I subscribed to a forum which sends me the newest topics via email. In these emails there is some text and a URL to the new topic that was created.

I wonder if it is possible to write an applescript which “extracts” the URLs from all those emails and automatically opens them in the background in Safari?

The email looks something like this:

Thanks in advance

Hi and welcome :slight_smile:

try something like this


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail" to set t to paragraphs of the content of eachMessage
			repeat with i in t
				if i starts with "http://" then tell application "Safari" to open location i
			end repeat
		end repeat
	end perform mail action with messages
end using terms from

save the script somewhere, define a rule in mail with the proper condition,
and the action “Run AppleScript” and attach the script

This seems to work perfectly, thank you very much.

Now I have another problem, when I have the rule set up I can’t exclude emails which are in the trash so it also opens the URLs in the emails which I trashed. Do you know a work around for this?

Thanks in advance

The script will be executed normally when new emails will be received,
so I guess it wouldn’t affect messages in the trash

Ok, thanks very much!

Can someone get this script to work with Firefox. I tried to change the application name to Firefox but nothing happens.
This would be very useful for me if I can get it to work because I receive numerous URLs in emails with links to PDF’s but I need to be able to save the PDFs automatically as I receive the emails. Or can there be something added to this to get Safari to save the linked PDF to a folder. Currently it just opens the PDF.

Thanks

using terms from application “Mail”
on perform mail action with messages theMessages for rule theRule
repeat with eachMessage in theMessages
tell application “Mail” to set t to paragraphs of the content of eachMessage
repeat with i in t
if i starts with “http://” then tell application “Safari” to open location i
end repeat
end repeat
end perform mail action with messages
end using terms from

Quit Safari, open Terminal and paste this into the terminal:

defaults write com.apple.Safari WebKitOmitPDFSupport -bool YES

The PDF inline-viewing is now disabled so Safari should download it instead.

did what you wrote but Safari is still opening the PDF

Thanks

I haven’t tested it but try this script:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail" to set t to paragraphs of the content of eachMessage
			repeat with i in t
				if i starts with "http://" then
					set i to «class ktxt» of ((i as string) as record)
					tell application "Firefox"
						activate
						Get URL i
					end tell
				end if
			end repeat
		end repeat
	end perform mail action with messages
end using terms from

This downloads the link directly on desktop without any browser


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail" to set t to paragraphs of the content of eachMessage
			repeat with i in t
				if i starts with "http://" then
					set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
					set fileName to last text item of i
					set AppleScript's text item delimiters to ASTID
					do shell script "/usr/bin/curl -o " & quoted form of (POSIX path of (path to desktop) & fileName) & space & quoted form of i
				end if
			end repeat
		end repeat
	end perform mail action with messages
end using terms from

These worked great. Thanks to both of you!

I’m looking to do a very similar thing”hence why I’m resurrecting this thread. I want to scan my email for a link and have it download automatically and preferably in the background. I’m most of the way there, the problem at the moment is it’s hard to find the specific link I want because there are four very similar links in the same email, for example:

PDF (5MB): http://domain.com/link.pdf

MOBI (2.8MB): http://domain.com/link.mobi

EPUB (3.8MB): http://domain.com/link.epub

Combined (12MB): http://domain.com/link.zip

I’m trying to get the epub link, but I get a combination of the epub link and the links above it. Here’s the code I currently have and if you’re curious why I have a Growl notification in there, it’s for debugging purposes.

using terms from application "Mail"
	on perform mail action with messages theMessages
		repeat with eachMessage in theMessages
			--Get the link from the current message:
			set theText to source of eachMessage
			set P1 to offset of "http://hackermonthly.com/delivery/" in theText -- the beginning of the link (better yet: "http://website/folder/")
			set theText to text P1 through -1 of theText
			set P2 to offset of ".epub" in theText -- the end of the link
			set theLink to text 1 through (P2 + 4) of theText
			
			tell application "GrowlHelperApp"
				-- Make a list of all the notification types 
				-- that this script will ever send:
				set the allNotificationsList to ¬
					{"Test Notification", "Another Test Notification"}
				
				-- Make a list of the notifications 
				-- that will be enabled by default.      
				-- Those not enabled by default can be enabled later 
				-- in the 'Applications' tab of the growl prefpane.
				set the enabledNotificationsList to ¬
					{"Test Notification"}
				
				-- Register our script with growl.
				-- You can optionally (as here) set a default icon 
				-- for this script's notifications.
				register as application ¬
					"Growl AppleScript Sample" all notifications allNotificationsList ¬
					default notifications enabledNotificationsList ¬
					icon of application "Script Editor"
				
				--	Send a Notification...
				notify with name ¬
					"Test Notification" title ¬
					"Stuff" description ¬
					theLink application name "Growl AppleScript Sample"
			end tell
			
			
			--Get the name of the ePub file to download:
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "/"
			set theFileName to text item -1 of theLink
			set AppleScript's text item delimiters to TID
			
			--Download the ePub file:
			set theDownloadedFile to ((path to downloads folder) as string) & theFileName
			tell application "URL Access Scripting" to download theLink to theDownloadedFile
		end repeat
	end perform mail action with messages
end using terms from

How could I modify this so that I’m only getting the ePub link?

Wes

Hi Wes,

try this one:

using terms from application "Mail"
	on perform mail action with messages theMessages
		repeat with eachMessage in theMessages
			--Get the link from the current message:
			set theText to source of eachMessage
			set P1 to offset of "EPUB (" in theText
			set theText to text P1 through -1 of theText
			set P2 to offset of ".epub" in theText -- the end of the link
			set theLink to text 1 through (P2 + 4) of theText
			set P3 to offset of "http" in theLink
			set theRealLink to text P3 through -1 of the theLink
			
			tell application "GrowlHelperApp"
				-- Make a list of all the notification types
				-- that this script will ever send:
				set the allNotificationsList to ¬
					{"Test Notification", "Another Test Notification"}
				
				-- Make a list of the notifications
				-- that will be enabled by default.
				-- Those not enabled by default can be enabled later
				-- in the 'Applications' tab of the growl prefpane.
				set the enabledNotificationsList to ¬
					{"Test Notification"}
				
				-- Register our script with growl.
				-- You can optionally (as here) set a default icon
				-- for this script's notifications.
				register as application ¬
					"Growl AppleScript Sample" all notifications allNotificationsList ¬
					default notifications enabledNotificationsList ¬
					icon of application "Script Editor"
				
				--    Send a Notification...
				notify with name ¬
					"Test Notification" title ¬
					"Stuff" description ¬
					theLink application name "Growl AppleScript Sample"
			end tell
			
			
			--Get the name of the ePub file to download:
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "/"
			set theFileName to text item -1 of theLink
			set AppleScript's text item delimiters to TID
			
			--Download the ePub file:
			set theDownloadedFile to ((path to downloads folder) as string) & theFileName
			tell application "URL Access Scripting" to download theRealLink to theDownloadedFile
		end repeat
	end perform mail action with messages
end using terms from

halbtuerke,

Worked perfectly, thanks so much!

Wes