Attach file to Thunderbird-Mail

Hi @ all

I would like to built a little script which should work as follows:

I assign a shortcut to this script via Spark.

I select a couple of file, press the shortcut assigned shortcut which creates a new mail with the files attached.

What do you guess: Is it theoretically possible to build something like that?

Regards

joh

Hi iprecious,

Unfortunately Thunderbird does not (yet) support AppleScript, but you can still compose new eMail messages with files attached by utilizing its command line interface:

Email script for Thunderbird

The AppleScript code is as follows (not perfect, but you get the idea…):


on run
	tell application "Finder"
		set finderitems to (selection as list)
	end tell
	set filepaths to {}
	repeat with finderitem in finderitems
		set iteminfo to info for (finderitem as alias)
		if not folder of iteminfo then
			set filepaths to filepaths & (POSIX path of (finderitem as Unicode text))
		end if
	end repeat
	my send_mail("Message with attachments", "Hallo and Good Bye!", "phil@mac.com", "steve@mac.com", filepaths)
end run

on send_mail(email_subject, email_message, email_sender, email_recipient, email_files)
	try
		set thunderbird_bin to "/Applications/Thunderbird.app/Contents/MacOS/thunderbird-bin -compose "
		set email_subject to "subject=" & email_subject
		set email_recipient to "to=" & email_recipient
		set email_message to "body=" & email_message
		set attachments to "attachment='"
		repeat with a_file in email_files
			if a_file as string is (last item of email_files) then
				set attachments to attachments & "file://" & a_file
			else
				set attachments to attachments & "file://" & a_file & ","
			end if
		end repeat
		set arguments to email_subject & "," & email_recipient & "," & email_message & "," & attachments & "'"
		do shell script thunderbird_bin & quoted form of arguments
	on error error_message number error_number
		log error_message & " " & error_number
	end try
end send_mail

HTH!

Hi Martin,

sorry for my late reply, but my flat was packed with people yetserday.

Now for my reaction: Wow!!!

I went through it an understood most of it.

But as I’m still very new to AS I can’t follow the empty braclets as in set filepaths to {}.

I’m going through the missing manual of AS, but up to now I haven’t come to something like that. What does it mean?

Unfortunately there is one little flaw in this script: If TB is already started, the script tries to start it again, which - of course - is met with the polite notification that TB is already up.

Any idea how to formulate the shell script which just creates a message with the attachments - in this case the progtram had to be up when the script is run, but that shouldn’t be too much of a problem.

To make things smooth it should be possible to create a little if/else-switch which runs the start-up-included command in case TB is not up yet and which will select the more modest version in case it’s already running.

My problem is that I’m not really into this wholy unixeria. I’ve started a bit, but sometimes the “real life” calls…

Thanks for the help so far and for potential upcoming aid.

joh

There is another tiny problem: The script only finishes, if I entirely shut down Thunderbird.

I guess that’s do to the ongoing shell process.

I tried to improve the script code, but I could not circumvent the mentioned limitations (maybe someone with more Thunderbird experience can). Therefor I think you might be better off using GUI Scripting to create a convenient and functional workflow solution. Hopefully the developers will add native AppleScript support to Thunderbird in the future :wink:

Hi Martin,

thanks for the effort.

When I read the thread again yesterday, your link draw my attention. At the end of it they mentioned EXACTLY the same problem, as is described in this post.

I probably leave this for the moment and hope that TB 3 will support AS.

All the best

joh