Hello,
I’m wondering if someone could help me here?
I’m looking for a script that I can attach to a Rule in mail.app that will automatically pull a URL from a subject line in an email (from mail.app) and load that URL into Safari?
Is this possible?
Any help would be greatly appreciated.
Thanks,
Kristin.
I don’t use Mail, so haven’t had occasion to script it, but if you get the subject line as text in the front of this script, the handler will do the rest, assuming that the URL begins with “http:”.
-- assuming you've extracted the subject line from an email in Mail as text --
set subject_line to "Try http://www.google.com or http://www.yahoo.com" -- my sample
OpenURLInSafari(subject_line) -- calls the handler
-- the handler
to OpenURLInSafari(tText)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
ignoring case
if tText contains "http:" then
set textItems to text items of tText
repeat with anyItem in textItems
if contents of anyItem contains "http:" then
tell application "Safari" to open location contents of anyItem
end if
end repeat
else
display dialog "There was no URL in that subject line"
end if
end ignoring
set text item delimiters to astid
end OpenURLInSafari
Thanks Adam,
So, now, does anyone know how to pull the subject line of an email into this script?
Thanks,
Kristin.
A little searching in this forum produced this, K, but you’re on your own now - I can’t test it.
tell application "Mail"
--activate -- optional, brings mail to the front
set a to selected messages of message viewer 1 -- returns a list of message(s)
set b to subject of (item 1 of a)
end tell
which I’ve incorporated in the earlier script as:
tell application "Mail" to set tSubject to (item 1 of selected messages of message viewer 1)
OpenURLInSafari(tSubject)
to OpenURLInSafari(tText)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
ignoring case
if tText contains "http:" then
set textItems to text items of tText
repeat with anyItem in textItems
if contents of anyItem contains "http:" then
tell application "Safari" to open location contents of anyItem
end if
end repeat
else
display dialog "There was no URL in that subject line"
end if
end ignoring
set text item delimiters to astid
end OpenURLInSafari
Thanks Adam,
I gave it a test and it returned the error “Mail got an error: Can’t make item 1 of selected messages of message viewer 1 into type reference.”
I know you can’t test/debug this yourself, but if you could direct me to where you were able to track down the reference on the forum, I’ll continue there!
Thanks again!
Kristin.
Try this:
tell application "Mail" to tell item 1 of (get selection) to set tSubject to subject--this is the short version of what I posted in your other thread - http://bbs.applescript.net/viewtopic.php?id=17940
OpenURLInSafari(tSubject)
to OpenURLInSafari(tText)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
ignoring case
if tText contains "http:" then
set textItems to text items of tText
repeat with anyItem in textItems
if contents of anyItem contains "http:" then
tell application "Safari" to open location contents of anyItem
end if
end repeat
else
display dialog "There was no URL in that subject line"
end if
end ignoring
set text item delimiters to astid
end OpenURLInSafari
EDIT: It worked - opened a page - in the one test I ran
Hey guys,
Thanks so much for your help!
So, the script works, but not exactly how I was hoping it to do so.
The problem is, the script only works if the message is already selected, and so, if no message is selected (as mail.app doesn’t select the new message by default) nothing happens. Or, if another message was already selected, if pulls the subject from that message (rather than the new message that was sent that triggers the AppleScript).
Is there any way to script it so mail.app first selects the newest message? Then it would work as the message that triggers the script would then be selected. Basically, this is all being done so there needs to be no user interaction.
Thanks again for your help!
Kristin.
CapJ; do you know - I use Eudora, haven’t a clue how to script Mail.
Yes - typing slowly be right back…
I think this will do it. I commented out the dialog so there would be no user interaction, but it still needs to be tested with a rule:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail" to repeat with thisMessage in theMessages
set tSubject to subject
OpenURLInSafari(tSubject)
end repeat
end perform mail action with messages
end using terms from
to OpenURLInSafari(tText)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
ignoring case
if tText contains "http:" then
set textItems to text items of tText
repeat with anyItem in textItems
if contents of anyItem contains "http:" then
tell application "Safari" to open location contents of anyItem
end if
end repeat
--else
--display dialog "There was no URL in that subject line"
end if
end ignoring
set text item delimiters to astid
end OpenURLInSafari
Tested same methods as before, but now nothings happens?
k.
Rats.
I’ll look for my mistake. I might be a while - I’ve got a 13 month old boy in my lap
j
Yea man, no worries ” I appreciate all your (both of you) help already! This is all WAY beyond me…
Thanks again!
kristin.
CapJ;
I can’t comment on the Mail part, but I’m assuming that the “rule” will filter out messages that have URLs or are likely to contain URLs. You wouldn’t want to run this on every arriving email.
(Since my kids range from 38 to 43 and collectively have 7 of their own the youngest of whom is 4, can’t say kids are the problem - That I’m supposed to be painting the outside of three windows is more like it, so can’t stay). Good luck.
Yea, the rule in mail app looks for an address from a specific person and a subject that starts with HTTP://.
If it meets both these requirements, then it launches the script.
k.
This just worked for me:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail" to repeat with thisMessage in theMessages
tell thisMessage
set tSubject to subject
end tell
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
ignoring case
if tSubject contains "http:" then
set textItems to text items of tSubject
repeat with anyItem in textItems
if contents of anyItem contains "http:" then
tell application "Safari" to open location contents of anyItem
end if
end repeat
else
display dialog "There was no URL in that subject line"
end if
end ignoring
set text item delimiters to astid
end repeat
end perform mail action with messages
end using terms from
I think the problem was that the rule requires everything to occur within the "Using terms from… " block and the handler threw in a monkey wrench - somebody more knowledgeable might correct me on that. I tried putting the handler in the block, but either I made a mistake or it was the wrong thing to do - either way, it didn’t work.
As written, the script keeps Safari in the background - do you need it to come to the front? Then you could try changing
tell application "Safari" to open location contents of anyItem
to
tell application "Safari"
activate
open location contents of anyItem
end tell
I think that as long as this rule comes before the last rule (which in my case is the “News from Apple” rule that was there by default) which contains the last action - “Stop evaluating rules” - then it should be fine.
j
Brilliant!
That totally works!
And I know this is a totally pain in your ass, and please feel free to completely ignore, but is it possible to get Safari to open in a new window?
Thank you!!!
Kristin.
Hi Adam.
I’m almost 40, so my only real problem with the kids (3 1/2 yrs and 13 mos) is that they weren’t born 10 years or so earlier. :lol: (Ooh, my back… )
Capitalj’s script works with small edit
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail" to repeat with thisMessage in theMessages
set tSubject to subject of thisMessage (* you missed telling mail what to get the subject from *)
my OpenURLInSafari(tSubject) (* adding the my call made this part work *)
end repeat
end perform mail action with messages
end using terms from
on OpenURLInSafari(tText) (* changed to to on *)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
ignoring case
if tText contains "http:" then
set textItems to text items of tText
repeat with anyItem in textItems
if contents of anyItem contains "http:" then
tell application "Safari" to open location contents of anyItem
end if
end repeat
--else
--display dialog "There was no URL in that subject line"
end if
end ignoring
set text item delimiters to astid
end OpenURLInSafari
Also his opens in new window/s
just a stab in the dark, but what about this ?
tell application "Safari" to open location (contents of anyItem) in a new window
When I get another minute I’ll verify that - I always use tabs…
j