HELP Please: Mail script revisions for Mail Droplet

Hi All - Can someone please help me?

After a lot of searching I found two scrips that partially cover what I need (when run separately) covering most of what I am hoping to achieve. My zero coding skills has resulted in a couple of weeks trying to learn Applescript in my limited spare time, to combine the two versions, without success!

I am truly grateful for your time, and assistance, in helping me with my quest.

The requirement is to take an image (always a single image) and use Automator to run the process once dropped into a watched folder.

When the image is dropped onto the Mail Droplet in the watched folder it should load a new Mail message, populate the To, Cc and Bcc fields (if populated), set the senders account, insert the name of the file as the Subject, minus its extension, insert the image, and populate a short line of text as the body of message above the image. No Signature is used or required.

I have inserted comments in the version of the script below, which does most of what I am looking to achieve, along with a copy of the other script, which does insert the name of the file, but errors on doing so, and does not populate and Mail details. The code may still have some of my botched attempts to combine, so please forgive. The other version is pretty much untouched.

Below is a screenshot of the error message for reference, and a screenshot of a mocked-up version of the Mail Message I would like to achieve, with notes where things are working, or not quite working.

In addition I would like to sequentially change the email bodymessage, which is also covered in the notes of the script below. This is because my son will be sending the files for media usage, and the message line will be used for the image supplied as part of the post. I would be happy to either hard code a selection (ideally without a limit) of messages to be used into the code, or an XML file, whichever is best / easiest to maintain.

Once the sequential email body message list is complete the code would start again from the first message. The body message needs to be able to accept # . and @ as shown in the example image, to work with mediums such as twitter.

Please note I have left out the send command from each example, as I wanted time to check everything was working first. If I am correct this is a simple addition once the code is complete - but, I have discovered when coding nothing is as it seems!

The following code works when saved as an Application, and an image is dropped onto it. I have inserted some notes / requirements on the items that are missing or need resolving, or where I simply don’t have the skill to code.


--the following script works, however, it does not have code to include the name of the attachment as the Subject, minus the attachments extension
--the option to use NameOfFile or Fixed subject would be ideal, which would be used by creating more than one version of the code for use with the core code items toggled on or off as required
--ability to insert fixed text such as #TAG after the file name would be useful too
property nameList : {"name1", "name2"}
--nameList works with the code below
property emailList : {"name1@address.com", "name2@address.com"}
--emailList works with this code
--Property CcList : {"CcName@address.com"}
--not configured, as do not know how to include this in the code. Please help
--property bccList : {"Bccname@address.com"}
--not sure if Bcc format is correct, and it is not included in the code below. Please help
property myaccount : "John Doe <john@doe.com>"
--code fails with error message if this is marked out
--comfortable leaving in to minimise code changes as will only use one account anyway
property theSubject : "Subject with File Name. Or file name and #Tags, or just text"
--works when using fixed text!
--code desired needs to insert file name of the file, or fixed text, or a mix of fixed text and the file name. for example. Updated Image: NameOfFle, which will change based on what is entered
property EmailBody : ".@names_of_name body message including . and @ for social media needs."
--works with code below as fixed text entry
--looking for an ability to have sequential body text with ideally no limit. Required for events where a new message needs to be attached to the post for social media posting.  
--similar to random quotes only sequential


on run
	tell application "Finder"
		set sel to (get selection)
	end tell
	new_mail(sel)
end run

on open droppedFiles
	new_mail(droppedFiles)
end open

on new_mail(theFiles)
	tell application "Mail"
		set newMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:EmailBody}
		tell newMessage
			
			set sender to myaccount
			repeat with i from 1 to count nameList
				make new to recipient at end of to recipients with properties {name:item i of nameList, address:item i of emailList}
			end repeat
			tell content
				repeat with oneFile in theFiles
					make new attachment with properties {file name:oneFile as alias} at after the last paragraph
				end repeat
			end tell
		end tell
		activate
	end tell
end new_mail

The following code inserts the names of the file, but creates and error, and the image attachment is not shown:



on open Dropped_File
	tell application "Finder"
		set NameOfFile to (name of (info for Dropped_File))
		set myFile to Dropped_File as alias
		--display dialog NameOfFile
	end tell
	
	
	tell application "Mail"
		set mySubject to NameOfFile as rich text
		set myContent to "Attached is the latest image from today " & NameOfFile &  "Thanks, Prescience"
		set myAttachment to Dropped_File
		set myMsg to make new outgoing message with properties {subject:mySubject, content:myContent}
		tell myMsg
			make new attachment with properties {file:myFile}
		end tell
		open myMsg
		
	end tell
	
end open


I’ll happily donate to a registered charity of choice for the person that helps me solve this, or to MacScripter.

Image links used for reference images will be deleted once any help provided resolves the request:

Mocked-up image:

Error message image:

Operating system is macOS Sierra 10.12.3

Here is the edited second script.

on open Dropped_File # Dropped_File is ALWAYS a list of aliases
	tell application "Finder"
		set myFile to item 1 of Dropped_File
		set NameOfFile to name of myFile
	end tell
	
	
	tell application "Mail"
		set mySubject to NameOfFile --as rich text
		set myContent to "Attached is the latest image from today " & NameOfFile & "Thanks, Prescience"
		--set myAttachment to Dropped_File
		set myMsg to make new outgoing message with properties {subject:mySubject, content:myContent}
		tell myMsg
			make new attachment with properties {file name:myFile}
		end tell
		open myMsg
		
	end tell
	
end open

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) dimanche 5 février 2017 14:08:37

Try this one.

# written for http://macscripter.net/viewtopic.php?id=45505


property subjectFormat : 3
# 0 = use the dedicated dialog
# 1 = use fileName(s)
# 2 = use default (theSubject)
# 3 = use default (theSubject) + filename(s)

property editAvailable : false
# true = the edit dialog is active
# false = the edit dialog is disabled

property useListOfLists : true
# true  use my preferred structure
# false  use your original structure

property dropDashes : true
# true  replace dashes in the subject by space characters
# false  leaves the dash in the subject

# My preferred structure with a single list of lists
property targetList : {{"name1", "name1@address.com"}, {"name2", "name2@address.com"}}
# Your original one with two lists
property nameList : {"name1", "name2"}
--nameList works with the code below
property emailList : {"name1@address.com", "name2@address.com"}

property CcList : {"CcName@address.com"} # may contain more than an address

property BccList : {"Bccname@address.com"} # may contain more than an address

property myAccount : "John Doe <john@doe.com>"
--code fails with error message if this is marked out
--comfortable leaving in to minimise code changes as will only use one account anyway
property theSubjects : {"Additional txt here to add before file name", "subject 2", "Subject 3", "Subject 4"}
-- Edit it to fit your default needs

property EmailBodies : {"This is the first in the list of messages to @names for social media.", "This is the Second and a new line of text with a different message associated with a new image post", "Third line of text associated to the next time the droplet runs with a new image ", "Ou Fourth line of text associated to the next time the droplet runs with a new image "}

# Use two different indexes
# As I guess that bodies and subjects must be synchronized, a list of lists would be safer but difficult to read.
# I added a test at the beginning of the handler new_mail checking that the two lists have the same length.
# This way subject and body will be synchronized.

property currentIndex : 1
property currentSubjectIndex : 1

#=====

on run
	tell application "Finder"
		set sel to (get selection as alias list) # Edited to pass a list of aliases
	end tell
	new_mail(sel)
end run

#=====

on open droppedFiles # droppedFiles is a list of aliases
	new_mail(droppedFiles)
end open

#=====

on new_mail(theFiles)
	
	# Enable this instruction if subjects and bodies MUST be synchronized.
	--if (count theSubjects) ≠ (count EmailBodies) then error "the lists "theSubjects" and "EmailBodies" MUST have the same length !"
	
	if theFiles is {} then
		# No attachment so use the default string
		set itsSubject to theSubjects's item currentSubjectIndex
	else
		# Define 3 variables required for tests
		set whichFormat to 0 # must define the variable
		set format2 to "default string"
		set format3 to "default + fileName(s)"
		
		if subjectFormat = 0 then
			set format1 to "fileName(s)"
			set whichFormat to button returned of (display dialog "Choose the subject format" buttons {format1, format2, format3} default button 3)
		end if
		if (whichFormat is format2) or (subjectFormat = 2) then
			set itsSubject to theSubjects's item currentSubjectIndex
		else
			set itsSubject to {}
			tell application "Finder"
				repeat with aFile in theFiles
					set aName to name of aFile
					set itsExt to name extension of aFile
					if itsExt is not "" then
						set aName to text 1 thru -(2 + (count itsExt)) of aName
					end if
					set end of itsSubject to aName
				end repeat
			end tell
			set itsSubject to my recolle(itsSubject, ", ")
			if dropDashes then # ADDED
				set itsSubject to my remplace(itsSubject, "-", " ") # ADDED
			end if # ADDED
			if (whichFormat is format3) or subjectFormat = 3 then
				set itsSubject to theSubjects's item currentSubjectIndex & " - " & itsSubject
			end if
		end if
		
	end if # theFiles is {}
	
	if editAvailable then # ADDED
		# Dialog allowing you to edit the subject if needed
		set itsSubject to text returned of (display dialog "Last chance to edit the subject" default answer itsSubject)
	end if # ADDED
	
	set EmailBody to item currentIndex of EmailBodies # MOVED BACK
	
	set currentIndex to currentIndex + 1
	if currentIndex > (count EmailBodies) then set currentIndex to 1
	
	set currentSubjectIndex to currentSubjectIndex + 1
	if currentSubjectIndex > (count theSubjects) then set currentSubjectIndex to 1
	
	tell application "Mail"
		activate
		set newMessage to make new outgoing message with properties {visible:true, subject:itsSubject, content:EmailBody & linefeed & linefeed, sender:myAccount}
		
		tell newMessage
			if useListOfLists then
				repeat with aTarget in targetList
					make new to recipient at end of to recipients with properties {name:item 1 of aTarget, address:item 2 of aTarget}
				end repeat
			else
				repeat with i from 1 to count nameList
					make new to recipient at end of to recipients with properties {name:item i of nameList, address:item i of emailList}
				end repeat
			end if
			
			repeat with aCC in CcList
				make new cc recipient at end of cc recipients with properties {address:aCC}
			end repeat
			
			repeat with aBCC in BccList
				make new bcc recipient at end of bcc recipients with properties {address:aBCC}
			end repeat
			
			tell content
				repeat with oneFile in theFiles
					make new attachment with properties {file name:oneFile} at after the last paragraph
				end repeat
			end tell # content
		end tell # newMessage
		
		tell me to delay 20
		send newMessage
		
	end tell # Mail
	
end new_mail

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====

I hope that I didn’t forget something.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) dimanche 5 février 2017 21:16:23

Morning Yvan,

Thanks for coming back to me. I am feeling somewhat foolish, as I did not spot the code was already in place to enable / disable the human input!

I have just seen your update on the code and will use this version and let you know if there are any issues.

Thank you for sharing your amazing knowledge, and patience too.