Getting attachments to work with AppleScript for Mail - macos

I am working with Mac OSX 10.12.6 and have built an AppleScript to create an email and then attach a PDF and a jpeg file. When I run the script i get an error when trying to attach the pdf and jpeg files.
Mail got an error: Can’t make {file name:"MacintoshHD:Users:peteradam:PPAttachments:EmailFolder:Quote_15018_301017.pdf", file size:200339} into type properties of attachment.
The error message is repeated for the jpeg with the file name changed.
I have also tried setting theSignature and theAttachment1 as aliases to no avail.
The script is as follows:
set TheRecipient to "name"
set theAddress to "me#me.com"
set theSubject to "Expoprint "
set theContent to "When in Doubt DON'T"
set theSender to "Me <me#me.com>"
tell application "Finder" to set theSignature to "MacintoshHD:Users:peteradam:PPAttachments:EmailFolder:ExpoPrint Signature.jpg"
set theSignatureSize to 20530
tell application "Finder" to set theAttachment1 to "MacintoshHD:Users:peteradam:PPAttachments:EmailFolder:Quote_15018_301017.pdf"
set theAttachmentSize1 to 200339
tell application "Mail"
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent & return & return, visible:true}
tell theMessage
set sender to theSender
make new to recipient at end of every to recipient with properties {name:TheRecipient, address:theAddress}
try
make new attachment with properties {file name:theAttachment1, file size:theAttachmentSize1} at after last paragraph
set message_Attachment to 0
on error errmess
log errmess
display dialog errmess
set message_Attachment to 1
end try
log "message_attachment =" & message_Attachment
make new attachment with properties {file name:theSignature, file size:theSignatureSize} at after last paragraph
delay 1
end tell
end tell

I resolved the problem by removing the File Size properties

Related

AppleScript error Can't make path into type alias

I’m trying to set up an AppleScript that will get a path from a txt file(mockUPPath.txt) on the Desktop which in theory will allow me to attach a jpg. The name of the folder and jpg will change so I can’t hard code it. I also get the body of the email from another text file on the Desktop which I managed to get working. I can get it to build the email but not find and attach the jpg.
The text in the mockUPPath.txt file looks like this: Name_Mock-up/555.jpg.
The jpg 555.jpg is in a folder “Name_Mock-up” on the Desktop
This is what I have so far (below), anyone know where I’m going wrong?
set AppleScript's text item delimiters to return
set theFile to ((path to desktop as text) & "mockUPText.txt")
set theData to paragraphs of (read file theFile)
set myPath to ((path to desktop as text) & "mockUPPath.txt")
set thePath to paragraphs of (read file myPath)
set newPath to (POSIX path of (path to desktop folder)) & thePath
--Email
set theMessageSubject to "Test email"
set theRecipient to "email#test.com"
set theMessageContent to theData
set theMessageAttachment to newPath
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {visible:true, subject:theMessageSubject, content:theMessageContent & linefeed & linefeed}
tell theMessage
make new to recipient at end of to recipients with properties {address:theRecipient}
make new attachment with properties {file name:theMessageAttachment as alias} at after the last word of the last paragraph
end tell
end tell
Based on the information you presented in your OP, the following example AppleScript code works for me as tested in Script Editor.
First I did the following:
Created a folder on my Desktop named Name_Mock-up containing a screenshot of the example AppleScript code in Script Editor naming it 555.jpg
Created a file on my Desktop named mockUPText.txt with a single line of text, Name_Mock-up/555.jpg.
The ran the example AppleScript code:
set theFile to ((path to desktop as text) & "mockUPText.txt") as alias
set theData to read theFile
set myPath to theFile
set thePath to read myPath
set newPath to (POSIX path of (path to desktop folder)) & thePath
set theMessageSubject to "Test email"
set theRecipient to "email#test.com"
set theMessageContent to theData
set theMessageAttachment to newPath
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {visible:true, subject:theMessageSubject, content:theMessageContent & linefeed & linefeed}
tell theMessage
make new to recipient at end of to recipients with properties {address:theRecipient}
make new attachment with properties {file name:theMessageAttachment} at after the last word of the last paragraph
end tell
end tell
Here is a screenshot of the result of running the example AppleScript code:
Note: That all said, I would not necessarily code it in this manner and have only presented this to correct some of the code you originally posted as mentioned in part by my comment to your OP.
First of all the most reliable way to interact with read/write API is to coerce the path to «class furl» and delete the file keyword.
Second of all most of the text files are UTF 8 encoded. If so read it as «class utf8»
The error occurs because you cannot coerce a POSIX path to alias. You need an extra step
Text item delimiters makes no sense, read the body text as one string and get the first item of the thePath.
set theFile to ((path to desktop as text) & "mockUPText.txt") as «class furl»
set theData to (read theFile as «class utf8»)
set myPath to ((path to desktop as text) & "mockUPPath.txt") as «class furl»
set thePath to paragraphs of (read myPath as «class utf8»)
set newPath to (POSIX path of (path to desktop folder)) & item 1 of thePath
set newAlias to POSIX file newPath as alias -- if the file doesn't exist you'll get an error
--Email
set theMessageSubject to "Test email"
set theRecipient to "email#test.com"
set theMessageContent to theData
set theMessageAttachment to newAlias
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {visible:true, subject:theMessageSubject, content:theMessageContent & linefeed & linefeed}
tell theMessage
make new to recipient at end of to recipients with properties {address:theRecipient}
make new attachment with properties {file name:theMessageAttachment} at after the last word of the last paragraph
end tell
end tell

Set theMessage's content to have multiple lines in applescript

In an automator service, I have a bash script that passes two variables to an applescript that then inputs those into an email. I want both variables to be on a different lines within the email.
on run {input, parameters}
set macPath to item 1 of input
set windowsPath to item 2 of input
set messageContent to macPath & return & linefeed & return & windowsPath
tell application "Microsoft Outlook"
activate
set theMessage to make new outgoing message with properties {content:messageContent}
open theMessage
end tell
end run
I've tried return, linefeed and escape characters but none seem to work and the contents of my message always end up on one line. Is there any way to do this in applescript?
I've just realised that the content property supports "rich text" so linebreaks can be introduced with the <br> tag such as:
tell application "Microsoft Outlook"
activate
set sendToAddress to ""
set theMessage to make new outgoing message with properties {content:"Line 1 <br> line 2"}
open theMessage
end tell

Failed to save mail attachments using applescript

I try to save attachments using applescript, my script works well when the files are small (e.g 109 Bytes) but save empty file when the attachment is too large (304KB). I'm working on macOS High Sierra and had no clue how to fix it, anyone got any idea?
My acript:
using terms from application "Mail"
on perform mail action with messages theMessages
set myPath to "Macintosh HD:Users:MY_USER:Downloads:"
tell application "Mail"
repeat with eachMessage in theMessages
repeat with theAttachment in eachMessage's mail attachments
set originalName to name of theAttachment
set savePath to myPath & originalName
try
save theAttachment in savePath
end try
end repeat
end repeat
end tell
end perform mail action with messages
end using terms from
Thanks
As mentioned in https://apple.stackexchange.com/a/322724/156892, change:
save theAttachment in savePath
to:
save theAttachment in file savePath
In addition, it would be more robust if you set myPath like so:
set myPath to (path to downloads folder as text)

Add vcard to Contacts with Mail rules and Applescript

I receive a lot of customer vcards to a specific email address. I want to automatically add the vcards to my contacts through the Mail rules and an AppleScript.
I searched a lot and found something. I modified it a bit. And the opening and adding process works fine. But only when I choose a file. I can't get the file into a variable from the mail message. I tried it but it won't work.
Here is my code so far:
tell application "Mail"
set att to attachment
end tell
set thefile to att
tell application "Contacts"
activate
open thefile
end tell
tell application "System Events" to keystroke return
If I delete line 1, 2 and 3 and write in line 4 "set thefile to choose file" then it will work - if I choose a file.
But the first three lines I tried something out, but without any success.
So my question is, how can I get the file from the message?
Thank you
Yours sincerely,
Chris
Solution:
set Dest to ((path to desktop folder) as string)
tell application "Finder" to make new folder in Dest with properties {name:"TempFiles"} -- create TempFiles folder
Set Dest to Dest & "TempFiles:"
tell application "Mail"
activate -- not sure is mandatory, but I prefer to see selected mails !!
set ListMessage to selection -- get all selected messages
repeat with aMessage in ListMessage -- loop through each message selected
set AList to every mail attachment of aMessage -- get all attachements
repeat with aFile in AList -- for each attachement
if (downloaded of aFile) then
set Filepath to Dest & (name of aFile)
do shell script "touch " & (quoted form of (POSIX path of Filepath)) -- required because "Save" only works with existing file !
save aFile in (Filepath as alias) as native format
end if
end repeat -- next file
end repeat -- next message
end tell
tell application "Finder" to set CardList to every file of folder Dest whose name extension is {"vcf"}
tell application "Contacts"
activate
repeat with aCard in CardList
open aCard
delay 1
tell application "System Events" to keystroke return
end repeat
end tell
delay 2
-- tell application "Finder" to delete folder Dest
The file attached from email respond to the 'save" command, but not to 'open'. Then, you must first save the attached files, and later, move them to next application (add in 'Contacts' in your case).
The attachement is a member of a list of 'mail attachement' of a message : keep in mind that there could be many files attached.
Also, you can only save the attached file if its 'downloaded' attribute is true.
Last, but not least, it seems that the "save" instruction which was working nice in Snow Leopard, does not work the same in El Capitain : the file where to save must exist before the "save"...this is why I added the "touch" command to create it first (just create the entry in the tempFiles folder).
I also add at bottom of script the open vCard with the enter key to validate in Contact. You may have to add a delay to leave sometime for your computer to process the card.
if the keys broke does not work in your case, please check System Preferences accessibility settings to allow your computer to let your script control your Mac.
I added as much comments as possible to make it clear...may be too much !
set Dest to ((path to desktop folder) as string)
tell application "Finder" to make new folder in Dest with properties {name:"TempFiles"} -- create TempFiles folder
Set Dest to Dest & "TempFiles:"
tell application "Mail"
activate -- not sure is mandatory, but I prefer to see selected mails !!
set ListMessage to selection -- get all selected messages
repeat with aMessage in ListMessage -- loop through each message selected
set AList to every mail attachment of aMessage -- get all attachements
repeat with aFile in AList -- for each attachement
if (downloaded of aFile) then
set Filepath to Dest & (name of aFile)
do shell script "touch " & (quoted form of (POSIX path of Filepath)) -- required because "Save" only works with existing file !
save aFile in (Filepath as alias) as native format
end if
end repeat -- next file
end repeat -- next message
end tell
tell application "Finder" to set CardList to every file of folder Dest whose name extension is {"vcf"}
tell application "Contacts"
activate
repeat with aCard in CardList
open aCard
tell application "System Events" to keystroke return
end repeat
end tell
-- tell application "Finder" to delete folder Dest
As you can see, I filter the content of the temp folder with only files with extension 'vcd'...just in case your selected emails contain also other type of file which Contact can't handled.
At end of the script, I delete the temp folder. however, until you test it, I set this last row as a comment only (more safe !)

Applescript to attach files and then send email to a recipient works if the script is on DT or in Doc folder, but not if placed in Dropbox folder

I am running Apple OS X 10.11.1 on a Macbook Pro. I use OpenOffice Calc ver 4.1.2 Macros to download SQL data from the internet, do calculations on the data, then create pdf files of the results, which are sent out to folks. The Emailing routines built into OpenOffice Basic work fine on a PC running Windows 7, but on the Mac, The Recipient and Subject fields are not passed to the Mac Mail.app. To solve the problem, I test to see if the file path is PC centric or Mac Centric, and if Mac Centric, I create a text file that has the Mac Centric paths to all the pdf files, then call an Applescript that reads the text file data, adds a subject line and recipient EM address, attaches the pdf files to the email message, then send the email to the recipient, without any intervention from the user. If the script is on the Desktop or in the Documents folder, it works fine, but not if it is placed in my Dropbox folder. I Get the "Network file permission error. (-5000). I tried to fix the File Permissions by going to Dropbox preferences, holding the options key down and clicking the "Fix Permissions" button, but that does not work either.
Can anyone tell me what to do to fix this. I have included the Applescript and text file so you can see what happens.
on run
(* tell application "Finder"
open file "somefile.txt" of folder of (file (path to me))
end tell *)
set myPath to (path to me) as text
set MME_path to POSIX path of myPath
set b to MME_path
set text item delimiters to "/"
set d to {}
set c to text items of b
set myhome to item 3 of c
-- display dialog MME_path & " " & myhome
set myPrefsFile to item 2 of c & "/" & item 3 of c & "/" & item 4 of c & "/" & item 5 of c & "/EM-Eargv.txt"
open for access myPrefsFile
set PrefsContents to read myPrefsFile using delimiter {","}
close access myPrefsFile
set thesubject to item 1 of PrefsContents
set thebody to ""
set theSender to item 2 of PrefsContents
set theAddress to item 3 of PrefsContents
set theAttachment to item 4 of PrefsContents
set theAttachment1 to POSIX file theAttachment
set theAttachment to item 5 of PrefsContents
set theAttachment2 to POSIX file theAttachment
set theAttachment to item 6 of PrefsContents
set theAttachment3 to POSIX file theAttachment
set theAttachment to item 7 of PrefsContents
set theAttachment4 to POSIX file theAttachment
--set userCanceled to false
--try
-- set dialogResult to display dialog ¬
-- "Path to file is " & theAttachment & " POSIX " & theAttachment1 buttons {"Cancel", "OK"} ¬
-- default button "OK" cancel button ¬
-- "Cancel" giving up after 15 ¬
-- default answer (long user name of (system info))
--on error number -128
-- set userCanceled to true
--end try
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:thesubject, content:thebody}
tell newMessage
set visible to true
set sender to theSender
make new to recipient at end of to recipients with properties {name:theSender, address:theAddress}
tell content
make new attachment with properties {file name:theAttachment1} at after the last paragraph
make new attachment with properties {file name:theAttachment2} at after the last paragraph
make new attachment with properties {file name:theAttachment3} at after the last paragraph
make new attachment with properties {file name:theAttachment4} at after the last paragraph
end tell
end tell
delay 3
send newMessage
end tell
delay 3
(*
tell application "Mail"
quit
end tell
*)
end run
The Text File is called EM-Eargv.txt
EM-Docs Files FYI.,”Your Name”,”YourEMaddress”,file:///Users/admin/Documents/Dropbox/EM-Docs/Sends/Doc1.pdf,file:///Users/admin/Documents/Dropbox/EM-Docs/Sends/Doc2.pdf,file:///Users/admin/Documents/Dropbox/EM-Docs/Sends/Doc3.pdf,file:///Users/admin/Documents/Dropbox/EM-Docs/Sends/Doc4.pdf,
You will have to update this text file with your own Name and email address plus change the path to whatever your is and place 4 short PDF files, named Doc1.pdf, Doc2.pdf, Doc3.pdf and Doc4.pdf in a folder called "Sends", before this applescript will send the email to you.
I can send you a short OpenOffice Calc document that creates the Text File with the correct path file data for your computer's configuration but I do not see how to attach the Calc.ods file into this request.
I run the entire example I have from a directory called "EM-Docs" That I can create a Zip file to be downloaded, but again, I do not see a way to attach a zip file to this request.
I'm guessing, it's this code block that's throwing the error:
open for access myPrefsFile
set PrefsContents to read myPrefsFile using delimiter {","}
close access myPrefsFile
Place some display dialog messages to debug and find the exact code, like so:
display dialog "About to read file"
open for access myPrefsFile
set PrefsContents to read myPrefsFile using delimiter {","}
close access myPrefsFile
display dialog "Just finished reading file" & return & (PresContents as string)
But here's the thing, if the problem is the open for access conflict with Dropbox, you don't HAVE to open for access a file if all you're doing is reading it. Just removed the open/close access lines, and try that:
display dialog "About to read file"
set PrefsContents to read myPrefsFile using delimiter {","}
display dialog "Just finished reading file" & return & (PresContents as string)
If, for some reason you do need to open file access, you could always quit Dropbox, do the file work, then launch Dropbox.

Resources