Trying to create an e-mail with attachment… Not successful yet…
set the_subject to "Hello there"
set the_body to "Please read the attached file:"
set theattachment to document file "vbvbvb.scpt" of folder "Documents" of folder "pascalharvey" of folder "Users" of startup disk
tell application "Mail"
set the_message to make new outgoing message ¬
with properties {subject:the_subject, content:the_body, visible:true}
tell content of the_message
make new attachment with properties {file name:theattachment}
make new to recipient ¬
at end of to recipients ¬
with properties {name:"Pascal", address:"pascalharvey@videotron.ca"}
make new attachment with properties {file name:theattachment}
end tell
end tell
I know the path to file “vbvbvb.scpt” is good since i got it from recording its opening in scripteditor.
As Yvan Koenig showed you, the file reference should be an alias.
You need to tell not to the contents of the outgoing message, but to the outgoing message itself.
They are needed delays, because Mail.app is not as fast in its actions as AppleScript.
set theAddress to "pascalharvey@videotron.ca"
set the_subject to "Hello there"
set the_body to "Please read the attached file:"
set theattachment to ((path to documents folder as text) & "vbvbvb.scpt") as alias
tell application "Mail"
set the_message to make new outgoing message with properties {subject:the_subject, content:the_body, visible:true}
tell the_message
make new to recipient at end of every to recipient with properties {address:theAddress}
delay 5
make new attachment with properties {file name:theattachment}
delay 5
send
end tell
end tell
That’s right, but as theattachment is already an alias the coercion to alias in the make new attachment line is redundant.
Not necessarily. You can tell the content to make a new attachment with or without specifying the insertion location.
Actually they are not needed. Which source did you get this predication from?
This script compiles and works
set theAddress to "pascalharvey@videotron.ca"
set the_subject to "Hello there"
set the_body to "Please read the attached file:"
set theattachment to ((path to desktop as text) & "vbvbvb.scpt") as alias
tell application "Mail"
set the_message to make new outgoing message with properties {subject:the_subject, content:the_body, visible:true}
tell the_message
make new to recipient at end of every to recipient with properties {address:theAddress}
tell content
make new attachment with properties {file name:theattachment} -- at before first paragraph
end tell
send
end tell
end tell
It was a mechanical mistake on my part, when I just forgot to remove the second coercion. I updated the script. Here, thanks for your attention.
As for the second, you are partially right. You cannot tell the contents of an outgoing message to create a new one recipient to (as it does the OP). Attaching file telling to its contents is possible. But this is not necessary. The outgoing message attacks the files without problems, by itself. Here, both my script and your script are written correctly. I just like 1 tell statement instead of 2, were it is possible.
As for the third, you are completely wrong. The Mail.app will not have time to attack the file on slower computers than yours (before sending). My script does what it needs. Therefore - do not mislead users about the delays.
NOTE: The 3) is tested on my slow computer with little resources (4 GB RAM, 9 years old Mac). The script doesn’t send attached file without the delay. It sends without the delay only the text content.
9 year old Macs with little resources are certainly the exception, even on my 9 year old MacPro the script sends the attachments reliably without any delay. So the delays are not needed per se and can be omitted on the majority of faster Macs.
I appreciate your knowledge, but believe me, here you are wrong. I conducted several tests, and read a lot of complaints about this in the network. Although it works for some users as is, others need less delay than my (for example, 1 second). I set 5 seconds just in case. But the main thing here is not what is delay, but why the script doesn’t work for some users. That is, the reason for the refusal.
And, yes, you are right, the delays are not needed per se and can be omitted on the majority of faster Macs.
Here, iMac 21.5" mid-2011, 16GB, 10.13.6,
with or without the delays, the script is created but it is not sent.
The History is:
Don't click upon [Open this Scriplet in your Editor:]
tell current application
path to desktop as text
--> "SSD 1000:Users:**********:Desktop:"
end tell
tell application "Mail"
make new outgoing message with properties {subject:"Hello there", content:"Please read the attached file:", visible:true}
--> outgoing message id 6
make new to recipient at end of every to recipient of outgoing message id 6 with properties {address:"trucmuche@rsf.fr"}
--> missing value
make new attachment with properties {file name:alias "SSD 1000:Users:**********:Desktop:vbvbvb.scpt"}
--> missing value
send current application
--> missing value
end tell
Résultat :
missing value
If I read well the dictionary, send is supposed to return a boolean which it didn’t.
So I had a closer look and saw that in StefanK’s version, the send command is wrongly located.
Here is the corrected active part:
tell application "Mail"
activate
set the_message to make new outgoing message with properties {subject:the_subject, content:the_body, visible:true}
tell the_message
make new to recipient at end of every to recipient with properties {address:theAddress}
tell content
make new attachment with properties {file name:theattachment} -- at before first paragraph
end tell
send # MOVED
end tell
end tell
With this version the mail is sent without any inserted delay. And the command returns true
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 25 décembre 2019 17:45:08
The fact that you are not hungry does not mean that everyone is full. Stop focusing on this fact so that others do not look for hours and days why it does not work for them!
Of course, send current application will never work. You should send an outgoing message instead of sending the Script Editor application… And you can send the message outside the tell the_message:
Stefan’s script in other form:
set theAddress to "pascalharvey@videotron.ca"
set the_subject to "Hello there"
set the_body to "Please read the attached file:"
set theattachment to ((path to documents folder as text) & "vbvbvb.scpt") as alias
tell application "Mail"
set the_message to make new outgoing message with properties {subject:the_subject, content:the_body, visible:true}
tell the_message
make new to recipient at end of every to recipient with properties {address:theAddress}
tell content to make new attachment with properties {file name:theattachment} -- at before first paragraph
end tell
send the_message
end tell
NOTE: without the delay it still doesn’t send the attached file on my machine. It sends only the text content.
The tested script was really issuing what was logged. It was because the ‘send’ command was misplaced. StefanK’s which is accustomed to be fair recognized its error.
If you really worry about a possible required delay you may try this ‘active’ part of the code.
tell application "Mail"
activate
set the_message to make new outgoing message with properties {subject:the_subject, content:the_body, visible:true}
tell the_message
make new to recipient at end of every to recipient with properties {address:theAddress}
tell content
make new attachment with properties {file name:theattachment} -- at before first paragraph
end tell
repeat 10 times
set maybe to send # MOVED
if maybe is not missing value then exit repeat
delay 0.5
end repeat
end tell
end tell
As here there is no need for a delay I can’t test if it does correctly the job.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 25 décembre 2019 18:05:51
Oops, they took time but the mails issued by my tests arrived in my mailbox.
Only the first one with a delay before ‘send’ had the script attached.
I made new tests and saw that my proposal of test was not efficient.
I tried to monitor the duration of the delay with:
tell application "Mail"
activate
set the_message to make new outgoing message with properties {subject:the_subject, content:the_body, visible:true}
tell the_message
make new to recipient at end of every to recipient with properties {address:theAddress}
set oldCount to (get count attachments)
tell content
make new attachment with properties {file name:theattachment} -- at before first paragraph
end tell
repeat 10 times
if (get count attachments) > oldCount then exit repeat
delay 0.5
end repeat
send
end tell
end tell
but it appeared that the count was incremented before the script was really attached.
I was forced to insert ‘delay 1’ to get a correct behavior.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 25 décembre 2019 18:32:50
LEARN APPLESCRIPT book from Hamish Sanderson and Hanaan Rosenthal
Original script…before i bully it…
set the_subject to "Hello there"
set the_body to "Please read the attached file:"
set the_file to choose file with prompt "Choose the file to attach:"
tell application "Mail"
set new_message to make new outgoing message ¬
with properties {subject:the_subject, content:the_body, visible:true}
tell new_message
-- Add the recipients:
make new to recipient ¬
at end of to recipients ¬
with properties {name:"Simon", address:"simon@example.net"}
make new cc recipient ¬
at end of cc recipients ¬
with properties {name:"Georgia", address:"georgia@example.net"}
-- Add the attachment:
make new paragraph ¬
at end of last paragraph of content ¬
with data (linefeed & linefeed & linefeed)
make new attachment ¬
at end of last paragraph of content ¬
with properties {file name:the_file}
end tell
end tell
You have one of the best book to learn the AppleScript. If you read it further, there is explained how works the AppleScript - the AppleScript itself has only 5 commands.
Here, 2 objects : document file and folder, belongs to Finder.app. So, you can send (that is, tell to) to Finder only this code line:
tell application "Finder" to set theattachment to document file "vbvbvb.scpt" of folder "Documents" of folder "pascalharvey" of folder "Users" of startup disk
Having the Finder reference, you can pass it to other applications by coercion it to alias:
tell application "Finder"
set theattachment to document file "vbvbvb.scpt" of folder "Documents" of home
end tell
set theattachment to theattachment as string as alias
tell application "Script Editor"
activate
open theattachment
end tell
delay 5 -- this is only to make the result visible to user
As you see, when it is possible, it is better to provide the alias directly. This is faster, and shorter:
set theattachment to ((path to documents folder as string) & "vbvbvb.scpt") as alias
tell application "Script Editor"
activate
open theattachment
end tell
delay 5 -- this is only to make the result visible to user
Remember, most applications works fine with the AppleScript aliases, the command line tools and ASObjC methods - requires the Posix paths. Here, important is this: To the Finder references has access only the Finder.app, AppleScript aliases is like one bridge between the applications.
It’s true for command line tools but it’s not for ASObjC.
Many scripts use them because when ASObjC was introduced they were required but since the world evolved.
use AppleScript version "2.5" # requires at least El Capitan
use framework "Foundation"
use scripting additions
property NSArray : a reference to current application's NSArray
set anAlias to (path to desktop)
# Convert the alias into an URL
set theURL to (NSArray's arrayWithObject:anAlias)'s firstObject()
set theKey to current application's NSURLCreationDateKey
set {theResult, theDate} to theURL's getResourceValue:(reference) forKey:theKey |error|:(missing value)
theDate as date
repeat
set this_file to choose file with prompt("pick a jpeg or tiff image:)¬
default location (path to pictures folder)¬
without invisibles
set this_info to the info for this_file
if the file type of this_info is in {"JPEG", "TIFF"} or ¬
the name extension of this_info is in {"jpg", "jpeg", "tif", "tiff"} then
exit repeat
else
beep
diplay dialog "The selected file is not the correct type."
end if
end repeat