i’m trying to have Safari open a URL that is contained within the first message in my Mail inbox. Mail lauches while the script is trying to do something but nothing happens after that, ie Safari does nothing. i am not getting any errors from the Script Editor though. any ideas?
using terms from application "Mail"
tell application "Mail"
set theMessage to first message of inbox
set the_content to content of theMessage
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 10}
set text_list to (text items of the_content)
repeat with the_line in text_list
if the_line contains "http:" then
try
tell application "Safari"
make new document
set URL of document 1 to the_content
end tell
end try
end if
end repeat
end tell
end using terms from
The obvious thing is that you’re telling it to set the URL to the entire contents, not just the paragraph with the URL. Also, since you have the Safari tell block embedded in the Mail tell block, there may be some terminology conflict. You can just get the paragraphs of the content to coerce it to a list and use the open location command to, well, open the location. Finally, you should parse the paragraph in case the line contains more than just the URL (this is a very basic URL parser, it is by no means exhaustive and will break with commas, angle brackets, etc. and doesn’t allow other types of URLs such as ftp, etc.):
thanks for the help jonn8. unfortunately it’s still not producing any results. if no applications are running (including Script Editor) and the script is launched; first Script Editor will launch, immediately followed by Safari. Mail does not launch. if i then hit Run in Script Editor while keeping Safari open; Mail will then launch but nothing happens after that. perhaps it is the way it is parsing the email message? this is what the message looks like everytime. it always has the same subject and sender and body (aside from the URL which is always unique).
[color=red]
IMPORTANT - YOU MUST TAKE FURTHER ACTION TO GET THIS POSTED !!!
CLICK ON THE WEB ADDRESS BELOW TO PUBLISH, EDIT, OR DELETE THIS POSTING. If your email program doesn’t recognize the web address below as an active link,please copy and paste the following address into your web browser:
I just did a test by sending myself a message with a URL in the body. I don’t normally use Mail but I retrieved this message with mail so it was the only message in my inbox. I then ran the script from Script Editor and it worked like a charm. How are you launching the script? How was the script saved? Do you want this to happen automatically via a rule or just something you run from the script menu?
If you want to run this on the last message received, I think you should change the message id to -1:
I saved this as a script and placed it in my Mail scripts folder:
~/Library/Scripts/Mail Scripts/Go to URL.scpt
Then, choosing that script from the menu (you may need to update the scripts menu to see it), it worked (I sent myself several messages with different URLs and it worked each time).
i think there is some other issue going on. what OS are you running? i am running Panther but i just tested it on a Jaguar machine and it worked perfectly, almost. at first it didn’t recognize “inbox”. so i changed it to “in mailbox” and it worked perfectly. seems to be just the opposite for Panther. has no problem with the “inbox” but the script does nothing. changing it to “in mailbox” and i get “doesn’t recognize variable” error. also the script won’t even launch at all from my Mail scripts menu. now i’m really confused. could it be that by “In” mailbox is split between two sub mailboxes?
both of those work great. i’ve actually figured out the problem. it’s an issue with that specific e-mail. i don’t know if it’s the way it’s formatted or what but i can’t figure it out. i’ve tested it by sending myself a million other messages with text and a URL thrown in and they all are working great. of course, the ONE i actually need and started this mess for happens to not work. ahh, life is just good like that.
so now i’m trying to figure out what the heck it doesn’t like about it. the messages appear exactly as i pasted above in red. no images, weird text, code, attachments. just plain text and the URL on one line. if it doesn’t look familiar it’s actually the email you receive to process a posting to Craigslist. to be exact i’m posting to New York>Services>Computer>Long Island. here is a direct link. give it a shot and see what happens if you like. you can fill it all out and process the form from the link below. it won’t actually post until you’ve answered the e-mail (the one i’m fighting with) and visited the link to continue processing.
ok, i’ll take the dunce cap. turns out the whole time the message was an HTML message. duh. so i got it working and it’s great. thanks for the help jon, awesome. all i did was change “get content of message” to “get SOURCE of message”. and i’m using that little text parser of yours for some similar stuff with a TextEdit task, thanks again.