Use Applescript to set the "Reply-To" address - macos

I have a registered domain, and I use hushmail as a mail provider. I'd like to send emails from Mail.app as if they were sent from my domain. The Hushmail SMTP server does not allow me to use a different "from" address than my account name, for security reasons (spam).
I found a way to have Apple mail fill the reply-to mail with a default all the time, here: http://email.about.com/od/macosxmailtips/qt/etalwaysreplyto.htm but that's too drastic for me, as I have multiple mail accounts in my mail client.
In Mail.app, I can set the "Reply to" field manually, but there is no setting in Mail.app to have that automatically filled based on the mailbox I select.
So far, I have an AppleScript which is able to create a reply on the selected mail:
tell application "Mail"
set theSelection to selection
if theSelection is {} then return
activate
repeat with thisMessage in theSelection
set theOutgoingMessage to reply thisMessage with opening window
# Wait for Mail.app to create the reply window
repeat until exists (window 1 whose name = "Re: " & subject of thisMessage)
end repeat
delay 0.1
#
# Here I want to set the reply-to address here based on the
# selected mailbox, or the "from" address of
# the current mail.
#
#
# The I need to restore the cursor to the body of the mail
# (if cursor was moved)
#
end repeat
end tell
I've looked in the AppleScript Dictionary (File -> Open Dictionary -> Mail.app -> Message -> message -> reply to), and this seems to be a property I should be able to set, but when I do something like:
tell theOutgoingMessage
make new recipient at end of reply to with properties {address:"myreplyto#example.com"}
An error pops up saying "Mail got an error: Can't get reply to of outgoing message id 65."
I also tried
tell theOutgoingMessage
set reply to to "myreplyto#example.com"
But that pops up an error saying "Mail got an error: Can’t set reply to of outgoing message id 69 to "myreplyto#example.com".
How can I set the reply-to property of the reply mail I just created?

As I mentioned in the comments to your post, you can't set the reply-to address programmatically because it's a read-only property. Therefore you need to ui script this solution.
The problem with ui scripting is that it's not an exact science. If Apple changes the positioning of the ui elements then your script will stop working. For example, I have Mail.app v6.5 and the reply-to field can be referenced using "text field 1 of scroll area 1 of window 1". In other versions of Mail.app that could be different (and probably is).
Anyway, in v6.5 of Mail.app this will do what you want using ui scripting.
tell application "Mail"
set theSelection to selection
if theSelection is {} then return
activate
repeat with thisMessage in theSelection
set replyToAddress to item 1 of (get email addresses of account of mailbox of thisMessage)
set replyToWindowName to subject of thisMessage
if replyToWindowName does not start with "Re:" then set replyToWindowName to "Re: " & replyToWindowName
-- open the reply message
set theOutgoingMessage to reply thisMessage with opening window
-- Wait for Mail.app to create the reply window
repeat until exists (window 1 whose name is replyToWindowName)
delay 0.2
end repeat
-- make sure the reply-to field is showing
my openReplyToField()
-- set the reply-to address here based on the selected mailbox
my setValueOfReplyToField(replyToAddress)
end repeat
end tell
on openReplyToField()
tell application "System Events"
tell process "Mail"
-- figure out if the reply-to text field is visible
set staticText to value of every static text of window 1
if staticText contains "Reply To:" then return
-- the reply-to field is not visible so we click the menu item
set viewMenu to menu 1 of menu bar item "View" of menu bar 1
set replyToMenuItem to first menu item of viewMenu whose name contains "Reply-To"
click replyToMenuItem
end tell
end tell
end openReplyToField
on setValueOfReplyToField(theValue)
tell application "System Events"
tell process "Mail"
set replyToField to text field 1 of scroll area 1 of window 1
set value of replyToField to theValue
end tell
end tell
end setValueOfReplyToField

Try if this works:
make new recipient at end of to recipients with properties {address:"myreplyto#example.com"}

Related

Best way to delete all attachments in all messages in specific Mailbox

I'm total AppleScript novice and I'm trying to write a script which will delete all attachments in all messages in specified Mailbox (Mail.app).
Here is what am I trying:
tell application "Mail"
tell mailbox "Sent" of account id "<XXX>"
set ListMessage to get every message
repeat with aMessage in ListMessage
set aList to get every mail attachment of aMessage
repeat with aFile in aList
if (downloaded of aFile) then
delete aFile -- this gives an error
end if
end repeat -- next file
end repeat -- next message
end tell
end tell
Running this results in error:
Error: the Apple Event handler failed (errAEEventFailed:-10000)
What am I doing wrong?
Thank you!
It turns out, that delete doesn’t work on mail attachments, but there is an alternative way to solve the issue: you can click the Remove Attachments menu item with System Events:
tell application "Mail"
activate
every item of (get selection)
end tell
tell application "System Events" to click menu item "Remove Attachments" of menu "Message" of menu bar item "Message" of menu bar 1 of process "Mail"

apple scripting with Messages to take action on URL

I'm trying to write an AppleScript for Messages such that if I get a message that contains a URL, the script automatically opens the link in my browser.
I've been able to set a script to run that sends me a notification with the properties of the message, but I've not yet been able to figure out how to take action on the URL.
Here's what I've been able to produce:
using terms from application "Messages"
on message received from theSender for theChat with theContents
display notification theContents as text ¬
with title "New Message from " & theSender
end on message received
end using terms
Here's an example that might help you:
set msg to theContents as text
set AppleScript's text item delimiters to space
set msg to msg as list
repeat with x in items of msg
if "http" is in x then
display dialog x
end if
end repeat

How do I make a copy of a sent email, to edit and send again

I am using Outlook for Mac 2011 version 14.3.1 on Mac OS X 10.7.5. I want to take a sent email, make a copy, open, edit and send it again. I used to have an applescript to do this but the link to download it no longer appears to function.
I suspect it was something like this.
Select you email in the sent iTems and run the script
tell application "Microsoft Outlook"
set theMessage to item 1 of (get selection) # get the first email message
set subj to subject of theMessage
set recip to (email address of to recipient of theMessage)
set cont to content of theMessage
set newMail to make new outgoing message with properties {subject:subj, content:cont}
repeat with i from 1 to number of items in recip
set this_item to item i of recip
tell newMail to make new recipient at newMail with properties {email address:this_item}
end repeat
open newMail
end tell
The script creates a new message with ALL previous recipients,subject and content.
Then opens it ready for editing

Weird behavior from the results of "Get Link URLs from Webpages" action

I'm building an Automator workflow that parses a Dropbox "gallery" page containing Quicktime video files, and automatically builds "direct-download" links for each of the files. In the end, I want all the links to be generated into the body of a Mail.app message.
Basically, everything works as expected, except for the part where the new links are sent to a new Mail.app message. My problem is that the links are sent to the body of the message without newlines, so they all get concatenated into a single line, like this:
https://dl.dropbox.com/u/149705/stackexchange/20121209/stackexchange_automator_prob2.png
(Mail.app seems to be wrapping the concatenated string on the question-marks)
The oddest thing about this is that if I use a "Copy to Clipboard" action at the end of the workflow (instead of my send-to-Mail Applescript), I get totally different results when I paste the same clipboard contents into TextEdit vs. BBEdit.
The links seem to paste into TextEdit properly. However, the same clipboard, pasted into a plaintext BBEdit document, yields only the first link:
https://dl.dropbox.com/u/149705/stackexchange/20121209/stackexchange_automator_prob5.jpg
What could be causing these 3 entirely different behaviors from the exact same results of the "Get Link URLs" action?
The reason you get that result is it the links start out as a List of strings but are being sent to mail.app as a single string.
You do not need to do anything fancy to fix this just put a return after the link.
I would just use this a single 'Run applescript' Action to do the whole job.
The script gathers only the file DL links. Adds a return on the end and send them to Mail.app.
No other formatting required
on run {input, parameters}
set db_tag to "dl-web.dropbox.com/get"
set myLinks to {}
tell application "Safari"
set thelinkCount to do JavaScript "document.links.length " in document 1
repeat with i from 1 to thelinkCount
set this_link to (do JavaScript "document.links[" & i & "].href" in document 1) as string
if this_link contains db_tag then
--add the link with a carriage return on the end.
copy this_link & return to end of myLinks
end if
end repeat
end tell
tell application "Mail"
activate
make new outgoing message with properties {visible:true, content:"" & myLinks}
end tell
end run
Get rid of all of the other actions and update myPage with your gallery URL.
on run
set myPage to "https://www.dropbox.com/sh/aaaaaaaaaaaaaaa/aaaaaaaaaa"
set myLinks to do shell script "curl " & quoted form of myPage & " | grep -Eo 'https://www.dropbox.com/sh/[^/]*/[^/\"]*/[^\"]*' | sed s'/https:\\/\\/www.dropbox.com\\(.*\\)/https:\\/\\/dl.dropbox.com\\1?dl=1/g'"
tell application "Mail"
activate
make new outgoing message with properties {visible:true, content:"" & myLinks}
end tell
end run
Each application is different, it understand or don't understand the output's format.
Regarding your AppleScript action:
input is an AppleScript list, the coercion from a empty string "" do a single line, because the delimiters is set to "" by default.
To get the desired result, set the delimiters to return or linefeed, like this.
on run {input}
set {oTID, text item delimiters} to {text item delimiters, linefeed}
set tContent to input as text
set text item delimiters to oTID
tell application "Mail"
activate
make new outgoing message with properties {visible:true, content:tContent}
end tell
return input
end run

Change iChat settings with applescript?

Is there a way to create a script that will change the preferences of iChat to run the script when a message is received?
In other words, I want to make a script to change the iChat preferences to enable the "Message Received.applescript" i have created. Wow, this is confusing. Let me simply this.
I want a script that does this:
Activate iChat
Open iChat Preferences
Move to "Alerts" tab
select event "Message Received"
turn on "Run applescript"
select a certain script from the script folder called "Message Receive.applescript"
please help?
You just need to edit iChat's plist file. defaults is a bit awkward for nested values so use System Events instead:
tell application "System Events"
set EventActions to property list item "EventActions" of property list file ((path to preferences folder from user domain as text) & "com.apple.iChat.plist")
repeat with e in {property list item "MessageNotification" of EventActions, property list item "SubsequentMessage" of EventActions}
make new property list item at end of e with properties {name:"RunAppleScript", value:true}
make new property list item at end of e with properties {name:"iChatAppleScriptsKey", value:{"~/Library/Scripts/iChat/Message Received.scpt"}}
end repeat
end tell
This will attach "Message Received.scpt" to both initial text invitations and subsequent messages - you could simplify it if you only wanted it for subsequent messages. Also note you may need to relaunch iChat for the change to take effect.

Resources