Applescript Mail Attachment Not Sending - applescript

The following Applescript sends an email successfully but does not attach the wav file. Can you tell me what I'm doing wrong? Thank you.
tell application "Mail"
set theSubject to "Voicemail"
set theContent to read (the POSIX path of "/private/tmp/voice.tgCrnv/BODY.txt")
set theAddress to "me#example1.com"
set theSender to "me#example2.com"
set theAttachmentFile to (POSIX file "/private/tmp/voice.tgCrnv/msg_1bb3b4f2-c6b1-4012-89c0-a19177cc6ca2.wav") as string
set msg to make new outgoing message with properties {subject:theSubject, content:theContent, visible:false, sender:theSender}
tell msg to make new to recipient at end of every to recipient with properties {address:theAddress}
tell msg to make new attachment with properties {file name:theAttachmentFile as alias}
send msg
end tell

This work for me
set theSubject to "Voicemail"
set theContent to read "/private/tmp/voice.tgCrnv/BODY.txt"
set theAddress to "me#example1.com"
set theSender to "me#example2.com"
set theAttachmentFile to "/private/tmp/voice.tgCrnv/msg_1bb3b4f2-c6b1-4012-89c0-a19177cc6ca2.wav" as POSIX file as alias
tell application "Mail"
set msg to make new outgoing message with properties ¬
{subject:theSubject, content:theContent, visible:false, sender:theSender}
tell msg
make new to recipient at end of every to recipient with properties {address:theAddress}
make new attachment at end of last character of content with properties ¬
{file name:theAttachmentFile}
end tell
delay 1
send msg
end tell

I've slightly adjusted your script that I believe will work if you first try it as it is before making any edits of your own to suit your own coding style (obviously, fill in the appropriate email addresses).
The significant functional changes are setting visible to true when declaring a new outgoing message; and targetting the content of the outgoing message in order to attach the file.
The variable declarations I moved to outwith the tell application block just for good coding practice (there's no need to tell Mail to set those variables, so don't).
And, finally, I reformatted the text of the code to what I'm more accustomed to for improved readability.
set theSubject to "Voicemail"
set theContent to read "/private/tmp/voice.tgCrnv/BODY.txt"
set theAddress to "me#example1.com"
set theSender to "me#example2.com"
set theAttachmentFile to POSIX file "/private/tmp/voice.tgCrnv/msg_1bb3b4f2-c6b1-4012-89c0-a19177cc6ca2.wav" as alias
tell application "Mail" to tell (make new outgoing message with properties ¬
{subject:theSubject, content:theContent, visible:true, sender:theSender})
make new to recipient at end of to recipients ¬
with properties {address:theAddress}
tell its content to make new attachment at after the last paragraph ¬
with properties {file name:theAttachmentFile}
send
end tell
Let me know how it goes.

FWIW: larger files need longer delays. 10mb - I know, too huge, but ... - needed upward of 8 second delay.

Related

One click forward marked messages. AppleScript

I have an AppleScript whose task is to forward flagged messages to another email (in the standard Apple Mail application), but it does not work as I need. I added this script to the Automator as a service.
I have to
1) flag the message.
2) go to the services and select the "Forward marked messages" service that I need, and only then they are forwarded all at once.
My idea is to auto forward a incoming message to the assistant immediately after marked the message with a flag.
Could someone correct my AppleScript?
Or am I mistaken in introducing it into services?
Help me please!
set toAddress to "alex#example.com"
set toName to "Alex"
tell application "Mail"
repeat with _acct in imap accounts
--Look For Flagged Messages in the INBOX
set _acct_name to name of _acct
set _inbox to _acct's mailbox "INBOX"
set _msgs_to_capture to (a reference to ¬
(every message of _inbox ¬
whose flagged status is true))
repeat with _msg in _msgs_to_capture
set _fwdmsg to forward _msg with opening window
tell _fwdmsg
make new to recipient at end of ¬
to recipients with properties {name:toName, address:toAddress}
end tell
activate
send _fwdmsg
end repeat
end repeat
end tell

Applescript to Paste clipboard text into Messages and send to email

The title says it all really.
Im new to applescripts and I'm hoping someone will be able to explain how to
Paste the clipboard text into Messages and send to a user using a applescript.
Also if you know how to send an image attachment instead of the clipboard text that would be amazing, and I would really appreciate it
set clipContent to (the clipboard as text)
tell application "Mail"
set myMessage to make new outgoing message at the beginning of outgoing messages with properties {subject:"Clipboard Content"}
tell myMessage to make new to recipient at beginning of to recipients with properties {address:"first#email.com, second#email.com"}
set myMessage's content to clipContent
set myMessage's visible to true
activate
-- Uncomment the line below to send the message
--send myMessage
end tell

AppleScript changes to Outlook messages are not saved

I am forced to use Outlook for Mac connected to corporate Exchange at work and I have pieced together a small AppleScript to alter subject lines of incoming email messages from our monitoring system. By default they are of an ungodly length and not human readable. I am attempting to fix that.
The script works, but for some strange reason everything is working great when I send test messages myself, however all changes are reverted back when the real messages from the monitoring system come in! When message arrives into my mailbox the subject is exacly how I want it, but within 3-5 seconds it reverts back to the original subject!
I do not know anything about AppleScript and have no idea where to beging troubleshooting something like this.
Would anyone please take a look and tell me if what I am attempting to do is doable?
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
tell application "Microsoft Outlook"
set theMessages to the current messages
end tell
repeat with theMsg in theMessages
tell application "Microsoft Outlook"
set mysubject to get the subject of theMsg
if mysubject contains "[Subscription:Spectrum CTO] SPECTRUM - " then
set mysubject to my replaceText("[Subscription:Spectrum CTO] SPECTRUM - ", "", mysubject)
set mysubject to my replaceText("Alarm Title: ", "", mysubject)
set mysubject to my replaceText("Severity: ", "", mysubject)
set mysubject to my replaceText("Model Name:", "-", mysubject)
set subject of theMsg to the mysubject as string
end if
end tell
end repeat
Since the only line that alters the message is
set subject of theMsg to the mysubject as string
it seems that it might be an issue with Exchange. Have you tried simply setting the subject of another email when it arrived?
Ok, The problem was solved by adding
delay(3)
right before
set subject of theMsg to the mysubject as string

Copying .rtf text into the body of an email with AppleScript

I have an AppleScript application which creates an email (in Mail.app) with attachments from the options I choose through dialogs. The text templates are stored in .rtf format so non-programers can alter the text templates to their wishes.
I am able to create an email from a .txt plain text file, but when I import an .rtf text file it imports the formatting commands, which I don’t want in the mail.
Here is an example of a .rtf import into an email:
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica-Light;}
{\colortbl;\red255\green255\blue255;}
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural\pardirnatural
\f0\fs24 \cf0 technology mail for english speakers\
Here is part of my script:
-- Import the .rtf file and store content in the mycontent variable
set mycontent to (read "/Users/kiara/Desktop/mailer/technology/tech-en-content.rtf")
…
…
-- create mail from values stored in variables
tell application "Mail"
set theMessage to make new outgoing message with properties {visible:true, subject:mysubject, content:mycontent}
tell content of theMessage
make new attachment with properties {file name:this_file} at after last paragraph
end tell
end tell
Is it possible to import formatted text from .rtf files into a new mail without the formatting codes (I choose rtf because mostly bold and color are used to format text)?
Here is another approach:
set the clipboard to (read "/Users/kiara/Desktop/mailer/technology/tech-en-content.rtf" as «class RTF »)
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {visible:true, subject:"mysubject"}
end tell
tell application "System Events"
tell process "Mail"
repeat until focused of UI element 1 of scroll area 4 of window 1
keystroke tab
end repeat
keystroke "v" using command down
end tell
end tell
I think Mail sends an email as either plain text or html, not rtf. So you would need to send your email as html not rtf. Note there's a trick to sending a html email with Mail via applescript. For some reason you can't set the visible property of the new message to true. It won't work if you do.
Here's how this can help you. You can use the command line tool textutil to convert the rtf to html and then send it as an email. Notice I use "tidy" in the command to make sure the html code is clean.
So all you need to do is put the receiver's email address and a subject in this script and run it...
set emailAddress to "someone#somewhere.com"
set theSubject to "My converted rtf to html"
set rtfFile to choose file with prompt "Choose the RTF file to email as HTML:" without invisibles
set theHTML to do shell script "/usr/bin/textutil " & " -stdout -format rtf -convert html " & quoted form of POSIX path of rtfFile & " | /usr/bin/tidy -b -utf8"
tell application "Mail"
set newMessage to make new outgoing message at end of outgoing messages with properties {visible:false}
tell newMessage
make new to recipient at end of to recipients with properties {address:emailAddress}
set subject to theSubject
set html content to theHTML
send
end tell
end tell
adayzdone answer works for me - more or less.
under Mac os X 10.15.6 , Mail 13.4 the scroll area is at another position / index.
And instead of using keystroke tab to get the content-Area an alternative way is to
set value of attribute "AXFocused" of UI element of scroll area 1 of window 1 to true
if you need to find the scroll area or identify other UI elements - you might want to use
on FindScrollAreas()
set Indices to {}
set the clipboard to "hello World"
tell application "System Events"
tell process "Mail"
activate
set i to 1
repeat with UIElement in UI elements of front window
-- button, text field, scroll area, static text
if class of UIElement is scroll area then
set end of Indices to i
end if
set i to i + 1
end repeat
end tell
end tell
return Indices
end FindScrollAreas
and there is also the fantastic approach in using Automator watch me do to get the menu items:
see this post
(you have to copy-paste the Events from Automator to some text-Editor to get some corresponding applescript)
and there is Xcode -> Xcode(menu) -> Open Developer Tool -> Accessibility Inspector - but i find it hard to transfer info to applescript
hope that helps, best -tom

Applescript for creating New Message with Mail application

I have an AppleScript for Mail.app which opens a new message window with pre-defined recipient address and subject. This script opens a new window every time I run it:
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return}
tell newMessage
set visible to true
make new to recipient at end of to recipients with properties {name:"some name", address:"some address"}
end tell
activate
end tell
But I want the script to open a new message window only when the previously opened window is closed – otherwise, the previously opened window should come to the front.
Can anyone please help me in modifying this script to achieve the above mentioned functionality?
I didn't test this but it should do what you need... at least it shows you the proper approach. You basically use a "property" to keep track of some value from the last time the script was run. In this case we check for the name of the frontmost window and see if it matches your criteria. If the window name doesn't do what you need then just find some other value to track between launches of the script. The basic approach should work.
EDIT: using the ID of the message, which is unique, the following will do what you want:
property lastWindowID : missing value
tell application "Mail"
set windowIDs to id of windows
if windowIDs does not contain lastWindowID then
set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return}
tell newMessage
set visible to true
make new to recipient at end of to recipients with properties {name:"some name", address:"some address"}
end tell
activate
set lastWindowID to id of window 1
else
tell window id lastWindowID
set visible to false
set visible to true
end tell
activate
end if
end tell
the visibility toggle seems to be the only way to get the window in front, as frontmost is a read-only property. The lastWindowID property will store the ID as long as the script is not re-compiled (caveat empteor: do not put this into an Automator service, as these get re-compiled every time the service is loaded).

Resources