Receive a message in the create window from Mail.app

I am trying to create a script that will receive the contents of the email message that I am composing in Mail, do something with the data, and then send the message. I know how to create and send a new message from scratch using AppleScript, but I cannot find a way to get the message that I am already writing. I don’t care what language is used, and I would be willing to try another email client. Thank you for your help!

+4
source share
4 answers

Mail has huge limitations regarding Applescript, and dealing with its content area is one of the main ones. It is best to use GUI scripts to go to the content area, type cmd-C, and then work through the data on the clipboard.

Unfortunately, due to what I see, Applescript has not improved at all in Lion.

+2
source

It is possible, but it hurts. It is painful enough that I am still trying to determine exactly how to do something like this, but in Safari. I went so far as to find a text box, but the documentation I found to get the content does not work. (Unfortunately, this is pretty much a course for AppleScript, each program is really a little different from the next program.)

EDIT: ok, I have a terrible evil that I hope can be adapted to work with Mail: http://www.ece.cmu.edu/~allbery/edit_textarea.script

0
source

This can be done if we make two rather weak assumptions: that the message you are working on is ahead, and that the theme of all draft messages is unique. Then, before running the script, save the message you are working on; this will put it in the draft mailbox. Then, since the message object is the window name, we can easily access it; and since we can easily access the draft mailbox, we can combine the two. This gives us:

tell application "Mail" set msgs to messages of drafts mailbox ¬ whose subject is (name of window 1 as string) if (count of msgs) = 1 then -- Do whatever else -- Error, disambiguate, whatever end if end tell 

You can probably make a script save the front window itself, and it wouldn't surprise me if the freshly saved message is always the first element of the draft mailbox, but this remains as an exercise for the reader :-)

0
source

It is actually quite easy to do what you need.

  • If you want to start some kind of built-in processing (assignable, say, a hot key (in Mail, for example, Cmd + D is not busy) or “just”, listed in the “Services” menu, available after choosing something), you can just use Automator. The sample Automator script reads the current selection, making some changes (here, converting some ASCII char + combinations to some characters with an accent) and, finally, returning the changed text looks like this:

     on run {input, parameters} set myText to replaceText("a1", "á", (input as text)) set myText to replaceText("e1", "é", myText) set myText to replaceText("i1", "í", myText) return myText end run on replaceText(find, replace, someText) set prevTIDs to text item delimiters of AppleScript set text item delimiters of AppleScript to find set someText to text items of someText set text item delimiters of AppleScript to replace set someText to "" & someText set text item delimiters of AppleScript to prevTIDs return someText end replaceText 

    Be sure to enable “Replaces selected text” if you want to overwrite the original content with the returned.

  • if you want to write an external script that is not called from the local "Services" menu (or using the hotkey), you will also need to add clipboard processing. A solution similar to the above with an extra copy / clipboard palette:

     on replaceText(find, replace, someText) set prevTIDs to text item delimiters of AppleScript set text item delimiters of AppleScript to find set someText to text items of someText set text item delimiters of AppleScript to replace set someText to "" & someText set text item delimiters of AppleScript to prevTIDs return someText end replaceText tell application "Mail" activate tell application "System Events" tell process "Mail" click menu item "Select All" of menu "Edit" of menu bar 1 click menu item "Copy" of menu "Edit" of menu bar 1 end tell end tell end tell tell application "Mail" set textclip to (the clipboard) end tell set myText to replaceText("a1", "á", textclip) set myText to replaceText("e1", "é", myText) set myText to replaceText("i1", "í", myText) set the clipboard to myText tell application "Mail" activate tell application "System Events" tell process "Mail" click menu item "Paste" of menu "Edit" of menu bar 1 end tell end tell end tell 

Note that the last script selects (and then overwrites) the contents of the entire window. It should be easy to work only with the current selection.

0
source

Source: https://habr.com/ru/post/1344874/


All Articles