I wanted to write a script to open the first URL in an incoming new Mail message. I failed to do so and ended up adding a “if then else” to avoid the second URL.
I cannot understand why the script does not work.
I have placed it in /Library/Scripts/Mail Scripts/Rule Actions.
I have set up the rule correctly by choosing “Run applescript”.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set paracnt to (count of paragraphs in eachMessage)
repeat with pgraph from 1 to paracnt
set par to the paragraph pgraph of eachMessage
set str to offset of "http" in par
if str ≠0 then
set len to length of par
set addr to characters str through len of par as string
if addr does not start with "something" then
display dialog addr
my openweb(addr)
end if
end if
end repeat
end repeat
end tell
end perform mail action with messages
end using terms from
on openweb(pge)
do shell script "open -a Firefox " & pge
end openweb
tell application "Mail"
set mes to source of message 1 of mailbox "write the name of one of your mailbox"
set pcnt to (count of paragraphs in mes)
repeat with pgraph from 1 to pcnt
set par to the paragraph pgraph of mes
set str to offset of "http" in par
if str ≠0 then
set len to length of par
set addr to characters str through len of par as string
set sp to offset of " " in addr
set qt to offset of quote in addr
if sp > qt then
set endaddress to sp - 1
else
if qt > sp then
set endaddress to qt - 1
else
set endaddress to length of addr
end if
end if
set addr to characters 1 through endaddress of addr as string
display dialog addr
my openweb(addr)
end if
end repeat
end tell
on openweb(pge)
tell application "Safari"
open location pge
end tell
end openweb
The first script that you wrote is not working.
I will describe you the Mail rule that I have set up:
If “any” of the following conditions are met
“From” contains “one of my email id”
Perform the following actions:
Move message to mailbox: “some mailbox”
Run applescript <path to the applescript in the “Rule Actions” folder>.
To test the script, i send an email from my other email id containing only one url and nothing else.
The email is moved to the “some mailbox” but nothing else happens.
I am confused about what else you have in mind and what you don’t regarding my problem. Fortunately, I came up with a solution to this.
We will consider an email, the contents of which are known to both of us:
“the new reply notification” email sent from macscripter when a thread to which you are subscribed receives a new post. It looks like this (the email messages that I am trying to use the script on, is also similar to this):
Contents of the message:
Jacques has replied to the topic ‘Mail Rule script’ to which you are subscribed. There may be more new replies, but this is the only notification you will receive until you visit the board again.
in Firefox (or Safari, whatever you use)
(and the script should work when attached to a “Mail Rule” having the condition “From” contains “do-not-reply@macscripter.net”)
In my tests (not as a rule but as a selection) this works.
repeat with eachMessage in theMessages
set aList to paragraphs of content of eachMessage
repeat with par in aList
set str to the offset of "http" in par
if str ≠0 then
set addr to text str thru -1 of par
set str to the offset of " " in addr -- look for a space
try
set addr to text 1 thru str of addr -- look for any spaces after the url
end try
if addr does not start with "something" then
my openweb(addr)
exit repeat
end if
end if
end repeat
end repeat
Thanks Mark for the script. I made the necessary additions like “using terms from…” and added the handler,but I still cannot get it to work.
Can you please try it after setting a rule?
(I posted a script above which also works on a selected message but does not work as a rule)
After setting a rule, you can control+click any message and click on “Apply Rules” to test it.
Using the say command as a debug.
The script first fails if I use ‘content’ rather than ‘contents’
It also seems the class of contents of the message is coming up as mssg.
rather than text?
here is what I am using to debug.
Just save any changes and then run the rule,
The script will say its progress as the mail rule runs.
property debug : true
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
if debug then say 1
tell application "Mail"
repeat with eachMessage in theMessages
if debug then say 2
set aList to (contents of eachMessage)
set kclass to class of aList
if debug then say 3
--if debug then do shell script "echo " & quoted form of aList & "> mail.txt"
if debug then say kclass
set aList to paragraphs of aList
if debug then say 4
repeat with par in aList
set str to the offset of "http" in par
if str ≠0 then
set addr to text str thru -1 of par
set str to the offset of " " in addr -- look for a space
try
set addr to text 1 thru str of addr -- look for any spaces after the url
end try
if addr does not start with "something" then
my openweb(addr)
exit repeat
end if
end if
end repeat
end repeat
end tell
end perform mail action with messages
end using terms from
This is used a switch to switch the Audio type debug on (true) or off (false)
Then note the
if debug then say
parts.
these are what you are hearing.
I put a new one along the script to see if the script reaches it.
In this case the script reaches the ‘if debug then say kclass’ and runs it, but does not reach ‘if debug then say 4’
Soscript fails at ‘set aList to paragraphs of aList’
But because I know that should work, I put in the part to get the class of the contents.
Which is coming up as a message?? rather than text.
and thats why the ‘set aList to paragraphs of aList’ part fails the script.
Rather than waste time I used source which is the Raw source of the message
The only thing to worry about is if a message header contains ‘Http’
property debug : true
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
if debug then say 1
tell application "Mail"
repeat with eachMessage in theMessages
if debug then say 2
set aList to source of eachMessage as text
set kclass to class of aList
if debug then say 3
--if debug then do shell script "echo " & quoted form of aList & "> mail2.txt"
if debug then say kclass
set aList to paragraphs of aList
if debug then say 4
repeat with par in aList
set str to the offset of "http" in par
if str ≠0 then
set addr to text str thru -1 of par
set str to the offset of " " in addr -- look for a space
try
set addr to text 1 thru str of addr -- look for any spaces after the url
end try
if addr does not start with "something" then
my openweb(addr)
if debug then say 6
exit repeat
end if
end if
end repeat
end repeat
end tell
end perform mail action with messages
end using terms from
on openweb(pge)
do shell script "open -a Safari " & pge
end openweb