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
Related
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.
I would like to create an application using AppleScript to copy the system info and email it to myself. I know to to do the emailing aspect and how to make the email have the content of whatever is in the clipboard. How might I use AppleScript coding to copy text to the clipboard?
In case it helps here is the way to email whatever is in the clipboard:
set a to "uselessdecoy#gmail.com"
tell application "Mail"
tell (make new outgoing message)
set subject to (the clipboard)
set content to "content"
make new to recipient at end of to recipients with properties {address:a}
send
end tell
end tell
You simply use
set the clipboard to "Some text"
You can also assign to the various parts of the clipboard (plain text, unicode, pict) directly, as in:
set the clipboard to {text:(outputText as string), Unicode text:outputText}
JXA Example - Copying Safari URL and Title into Clipboard.
var appSafari = Application("com.apple.Safari");
appSafari.includeStandardAdditions = true
var winFront = appSafari.windows[0];
var winFrontName = winFront.name().split("\"").join("'");
var currentTabURL = winFront.currentTab().url();
var result = `${winFrontName}: ${currentTabURL}`;
appSafari.setTheClipboardTo(result)
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
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).
I'd like to write an AppleScript to set the sender of the current outgoing message in Apple's Mail.app.
I've tried this:
tell application "Mail" to set sender of front outgoing message to "<my email address>"
but I get the error message error "Mail got an error: Can’t set sender of item to any." number -10006 from sender of item to any which doesn't make sense to me.
When I try to interrogate the front outgoing message as follows:
tell application "Mail" to get properties of front outgoing message
I get {class:item} in return, instead of an "outgoing message" object like I'd expect.
Any ideas?
Unfortunately, you cannot get or set the properties of the outgoing message object of Mail with Applescript.
Instead, you can accomplish this with GUI scripting, a workaround that directly manipulates window elements.
This code should work for you:
tell application "System Events"
tell process "Mail"
click pop up button 1 of window 1
click menu item 6 of menu 1 of pop up button 1 of window 1
end tell
end tell
Change the menu item 6 on the fourth line to whichever number in the list your desired sender is (e.g., if the sender you want to change to with this script is the fourth one listed, change menu item 6 to menu item 4).
Update: If you're curious, since this answer is over two years old: as of 2014-04-26, getting/setting the properties of outgoing messages is still impossible and this workaround still works in OS X 10.9 Mavericks.
I think it's a little more complicated. My own experiments agree with the conclusion of Alan Kimelman in this thread, that AppleScript works as expected for outgoing messages created from scratch by the script. For example, the following code works:
tell application "Mail"
set newMessage to (a reference to (make new outgoing message))
tell newMessage
make new to recipient at beginning of to recipients ¬
with properties {address:"hello#world.com", name:"The World"}
set the sender to "Jerry Krinock <jerry#sheepsystems.com>"
set the subject to "A Test"
set the content to "This is only a test."
send
end tell
end tell
However, if the message is created by other means (for example, I create HTML messages by telling Safari to Share via Email), then Matthew McVickar is correct that AppleScript is broken. You can't set or get any properties, and also it refuses the send command.
Actually you can set the sender of an outgoing message as described here:
Incorrect sender when sending Email via Applescript
It is more than a little frustrating that you can't set the sender of an outgoing message.
Here's a more general version of Matthew's script that I wrote many years ago and have used frequently. The argument is a string that contains exactly what you see in the "From:" pop up, e.g.
"Mitchell L Model <dev#software-concepts.org>"
(It is very tricky to get the details of something like this right if you don't do a lot of GUI scripting.)
to setFrom(address)
tell application "System Events"
activate application "Mail"
tell pop up button "From:" of window 1 of application process "Mail"
click
tell menu item address of menu 1 to click
end tell
end tell
end setFrom
The reason I use a parameterized handler is that I use keystroke macro facilities to bind a keystroke combination for each of my email addresses. Each executes an AppleScript that loads the file containing the above handler and invokes it with a specific email address. For example:
property util : load script alias (((path to home folder) as text) & "Scripts:Utilities.scpt")
util's 's setFrom("Mitchell L Model <dev#software-concepts.org>")
It's trivial to use AppleScript to create a new outgoing message with any outgoing address that you like (well, at least, the ones configured in your Mail.app preferences).
The discussions here hinge around trying (incorrectly) to change it after the message is created. Instead, specify it when you create the message:
set new_m to make new outgoing message with properties {sender:"My Name <me#my.com>"}
The text in quotes needs to match both the real name and the email address (in chevrons) of any configured account. If there's no match, Mail.app will use the default address
Try this instead.
tell application "Mail" to make new outgoing message with properties {sender:"<your_email_address>", visible:true}
UPDATE: I would look in Mail's scripting dictionary and see what you can and cannot do. The information provided there might help. :)
An alternative to the GUI scripting example I provided would be an Automator script that utilizes the 'Watch Me Do' command. I don't understand exactly how it works underneath the hood, but it ostensibly records mouse movement and GUI interaction and then reenacts what you recorded it when it runs. Unfortunately, I experienced significant lag when running the script. After asking it to run, it would sometimes execute after 5 or more seconds, which is clearly unusable.
The GUI scripting method is almost instantaneous and cleaner-looking (no mouse movement) to boot. I recommend it.