Help modifying a script to open URLs in emails

I don’t really know AppleScript but found the script below to open URLs in some email messages (run by a Mail rule when certain text is found in the subject line).


using terms from application "Mail"
	on perform mail action with messages theMessages for rule OpenURLs
		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


It works fine if the URLs are on a line of their own, but I found it won’t open URLs if there’s other text in front of it on the same line (e.g. Blah, blah, blah http://website.com ).

Is it possible to make the script open the URL on a line with other text in front of it?

Hi,

try this, it searches for occurrences of “http” somewhere in the paragraph.
But it assumes there is no other text after the link


using terms from application "Mail"
	on perform mail action with messages theMessages for rule OpenURLs
		repeat with eachMessage in theMessages
			tell application "Mail" to set contentParagraphs to paragraphs of the content of eachMessage
			repeat with aParagraph in contentParagraphs
				set httpOffset to offset of "http:" in aParagraph
				if httpOffset > 0 then
					set theLink to text httpOffset thru -1 of aParagraph
					tell application "Safari" to open location theLink
				end if
			end repeat
		end repeat
	end perform mail action with messages
end using terms from

That works perfectly. Thanks a lot! :slight_smile: