Set theMessage's content to have multiple lines in applescript - 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

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

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)

Getting attachments to work with AppleScript for Mail

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

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.

Applescript Handler

I am using the below script for few months. It ask the user to select from the list and copy paste the text in MS Word and run some VB Macro and save the file as Text file.
tell application "Finder"
if not (exists folder "Test" of desktop) then make new folder at desktop with properties {name:"Test"}
end tell
set desktopTestFolder to (path to desktop folder as text) & "Test:"
set mychoice to (choose from list {"PS List", "AA Table", "PS Legend", "PO Chart", "MD"} with prompt "Please select which sound you like best" default items "None" OK button name {"Play"} cancel button name {"Cancel"})
if mychoice is false then error number -128 -- user canceled
tell application "Microsoft Word"
set theContent to content of text object of selection
copy object text object of selection
set newDoc to make new document
delay 2
tell application "System Events"
tell process "Microsoft Word"
keystroke "v" using command down
end tell
end tell
run VB macro macro name "Normal.NewMacros.Clean"
run VB macro macro name "Normal.Module9.bold"
save as newDoc file format format Unicode text file name (desktopTestFolder & mychoice & ".txt")
close document 1 saving no
end tell
But when I try it to put in an handler it not works. What I have tried is:
tell application "Finder"
if not (exists folder "Test" of desktop) then make new folder at desktop with properties {name:"Test"}
end tell
set desktopTestFolder to (path to desktop folder as text) & "Test:"
set mychoice to (choose from list {"PS List", "AA Table", "PS Legend", "PO Chart", "MD"} with prompt "Please select which sound you like best" default items "None" OK button name {"Play"} cancel button name {"Cancel"})
if mychoice is false then error number -128 -- user canceled
set mychoice to mychoice as text
if mychoice is equal to "PS List" then
handler1()
else
handler2()
end if
on handler1()
tell application "Microsoft Word"
set theContent to content of text object of selection
copy object text object of selection
set newDoc to make new document
delay 2
tell application "System Events"
tell process "Microsoft Word"
keystroke "v" using command down
end tell
end tell
run VB macro macro name "Normal.NewMacros.EDCleanup1"
run VB macro macro name "Normal.Module9.bold"
save as newDoc file format format Unicode text file name (desktopTestFolder & mychoice & ".txt")
close document 1 saving no
end tell
end handler1
on handler2()
tell application "Microsoft Word"
run VB macro macro name "Normal.NewMacros.EDCleanup1"
run VB macro macro name "Normal.Module9.bold"
save as newDoc file format format Unicode text file name (desktopTestFolder & mychoice & ".txt")
close document 1 saving no
end tell
end handler2
Please let me know, Where I am wrong?
Thanks
Josh
You didn't say what result you get when you say "it not works". Do you get an error? Do you get nothing? Do you get the same result when you select "PS List" versus the others?
The error I see is that you never bring Microsoft Word to the front. This is necessary for when you're using UI scripting and the keystroke command. Add 'activate' to your Word tell blocks.
tell application "Microsoft Word"
activate
set theContent to content of text object of selection
…
Also, yes, your variable desktopTestFolder loses scope. You can make the variable a global property by placing this at the front of your script:
property desktopTestFolder: ""

Resources