The subject probably isn’t explicit enough. Basically I want to email the contents of a Safari page to myself every morning. (It’s a bunch of reports run on a Unix server.) I can use Applescript to tell Safari to open the web page and then “email contents of front document” which creates a new mail message with that web page as the body. That’s great, but for the life of me, I can’t find a way (using Mail.app) to reference that already created message to add a recipient to it! Is this possible? Everything I’ve searched says I need to create a new message in Mail.app to get the reference to it.
I should also add that I want the message to preserve the HTML formatting which is why I want to use Safari. Otherwise, is there a way in Mail.app to create an HTML message using a web page as its source?
I’m afraid there are a couple of issues here, guys.
While Mail can reference outgoing messages that have been created by direct scripting, it can’t do the same with manually created messages - nor those produced through UI scripting. (Any such references point to the application - and, as such, are completely meaningless.) This is clearly a bug, so let’s hope an early fix is forthcoming. There’s no simple way of inserting html content into a message, either.
So - workaround time, I suppose. You could try something like this, which seems to work tolerably well here:
set targetURL to "http://www.somewebsite.com/" (* modify as required *)
set targetRecipient to "someperson@somedomain.com" (* modify as required *)
launch application "Safari"
launch application "Mail"
tell application "Safari"
if (count documents) is 0 or (exists document 1's URL) and document 1's URL is not targetURL then
make new document with properties {URL:targetURL}
else
set document 1's URL to targetURL
end if
tell document 1
repeat until name starts with "Loading"
delay 0.2
end repeat
repeat while name starts with "Loading"
delay 0.4
end repeat
set docName to name
end tell
email contents of document 1
end tell
tell application "System Events" to tell application process "Mail"
set frontmost to true
tell window docName
repeat until exists
delay 0.2
end repeat
tell text field 1 of scroll area 2
set value to targetRecipient
repeat until value is targetRecipient
delay 0.2
end repeat
end tell
end tell
keystroke "d" using {shift down, command down}
end tell
I was about to post that I did exactly that, used keystrokes to simulate the same action. Just now though, I found XMail 3.2 which is an OSAX to send mail. It looks like it should do what I need, though I already have a working solution so it’s not a priority for now. The nice thing about using Mail though is that I can configure server settings in one place rather than have to remember to update the script in case my ISP changes its settings.
Keystroke commands will only work if you have checked “Enable access for assistive devices” at the bottom of any Universal Access Preferences Pane (in System Preferences).