New Mail attachment from selected file with variables

Hi,

this is my first post. And it’s from germany.

I have two scripts. Each works fine.
The first one puts a selected file from the finder window as an annex into a new mail. Some possible errors are considered.
The second one scales a selected file and copy it to the desktop with some conditions.

I tried to put the two scripts into one, but It doesn’t work. The problem is to bring the variable of the scale-script to the mail-script. And the new one should work without the save and close command.

The plan is to save the script as an app and place it into the finder button bar.

Any ideas?


tell application "Finder"
	try
		set my_annex to POSIX path of (selection as alias)
		set _my_annex to name of (selection as alias)
		set my_selection to kind of (selection as alias)
	on error
		do shell script "afplay /System/Library/Sounds/glass.aiff"
		display dialog "my_selection-Fehler" with title "malfunktion message" buttons {"ok"} default button "ok" giving up after 5
		error number -128
	end try
end tell
if my_annex is not "" and my_selection is not "Ordner" then -- Ordner is the german text result. I am not sure, if it has to be directory, folder or container 
	tell application "Mail"
		activate
		set my_new_mail to make new outgoing message with properties {subject:"", content:"my_annex: " & _my_annex & return & return}
		tell my_new_mail
			make new to recipient at beginning of to recipients with properties {name:"", address:""}
		end tell
		set visible of my_new_mail to true
		tell my_new_mail
			make new attachment with properties {file name:my_annex}
		end tell
	end tell
else if my_annex is "" then
	do shell script "afplay /System/Library/Sounds/glass.aiff"
	display dialog "selection was empty" with title "malfunction message" buttons {"ok"} default button "ok" giving up after 5
else if my_selection is "Ordner" then
	do shell script "afplay /System/Library/Sounds/glass.aiff"
	display dialog "selection was a folder" with title "malfunction message" buttons {"ok"} default button "ok" giving up after 5
end if


tell application "Finder"
	set my_annex to POSIX path of (selection as alias)
	set my_selection to kind of (selection as alias)
	set my_suffix to name extension of (selection as alias)
	set my_size to physical size of (selection as alias)
	set my_size to my_size / 1000
	set my_size to (round my_size)
	display dialog my_selection & " / " & my_suffix & " / " & my_size
	if my_selection contains "Bild" and my_suffix contains "jpg" and my_size > 250 then -- Bild is the german text result. Is the english result picture?
		tell application "Image Events"
			set my_mail to open my_annex
			scale my_mail to size 600
			save my_mail as JPEG in (path to desktop) with icon
			close my_mail
		end tell
	end if
end tell

Model: iMac
Browser: Safari 533.18.5
Operating System: Mac OS X (10.6)

Hallo,

you need the scaled image to be saved because the attachment must be a physical file.

Try this, it takes file type and class parameters, because they are
the same in all languages. After creating the outgoing mail the scaled image will be deleted
Note: selection in Finder is a list so you must get item 1 explicitly


try
	tell application "Finder"
		set selectedItem to item 1 of (get selection)
		set my_annex to POSIX path of (selectedItem as text)
		tell selectedItem
			set my_annexName to its name
			set selectionClass to class as text
			set my_suffix to name extension
			set my_size to physical size
			set my_fileType to file type
		end tell
	end tell
	set scaledImage to missing value
	if my_suffix is "jpg" and my_fileType is "JPEG" then
		set my_size to my_size / 1000
		set my_size to (round my_size)
		display dialog (selectionClass as text) & " / " & my_suffix & " / " & my_size
		tell application "Image Events"
			launch
			set my_mail to open (selectedItem as alias)
			scale my_mail to size 600
			set scaledImage to (save my_mail as JPEG in (path to desktop) with icon) as alias
			close my_mail
		end tell
	end if
on error
	do shell script "afplay /System/Library/Sounds/glass.aiff"
	display dialog "selection was empty" with title "malfunction message" buttons {"ok"} default button "ok" giving up after 5
	error number -128
end try

if my_annex is not "" and selectionClass is not "folder" then -- Ordner is the german text result. I am not sure, if it has to be directory, folder or container 
	
	tell application "Mail"
		activate
		set my_new_mail to make new outgoing message with properties {subject:"", content:"my_annex: " & my_annexName & return & return}
		tell my_new_mail
			make new to recipient at beginning of to recipients with properties {name:"", address:""}
		end tell
		set visible of my_new_mail to true
		tell my_new_mail
			make new attachment with properties {file name:scaledImage}
		end tell
	end tell
	if scaledImage is not missing value then
		tell application "Finder" to delete scaledImage
	end if
else if selectionClass is folder then
	do shell script "afplay /System/Library/Sounds/glass.aiff"
	display dialog "selection was a folder" with title "malfunction message" buttons {"ok"} default button "ok" giving up after 5
end if



I took a little different approach from StefanK. I understood this to mean if the selected file was a large image (eg. > 250) then he wanted to scale the image and use that as an attachment. If the image wasn’t large or if the selected file wasn’t an image at all, then he still wanted to create the email with the attachment… he just didn’t need the “image events” stuff to take place. As such here’s my attempt. Note: I took out the error checking and afplay stuff to make it simpler.

set fileKind to "image" -- Bild

tell application "Finder"
	set theFile to (item 1 of (get selection)) as alias
	set fileName to name of theFile
	set my_annex to POSIX path of theFile
	set my_selection to kind of theFile
	set my_suffix to name extension of theFile
	set my_size to physical size of theFile
	set my_size to my_size / 1000
	set my_size to (round my_size)
end tell
display dialog my_selection & " / " & my_suffix & " / " & my_size

if my_selection contains fileKind and my_suffix is "jpg" and my_size > 250 then -- Bild is the german text result. Is the english result picture?
	tell application "Image Events"
		set my_mail to open my_annex
		scale my_mail to size 600
		save my_mail as JPEG in (path to desktop) with icon
		close my_mail
	end tell
	set attachmentPath to (path to desktop as text) & fileName
else
	set attachmentPath to theFile as text
end if

tell application "Mail"
	activate
	set my_new_mail to make new outgoing message with properties {subject:"", content:"my_annex: " & fileName & return & return, visible:true}
	tell my_new_mail
		make new to recipient at beginning of to recipients with properties {name:"", address:""}
		make new attachment with properties {file name:(attachmentPath as alias)}
	end tell
end tell

Hallo Stefan, Hallo Regulus,

thanks a lot for your answers. I tested the scripts, but for some reason, the script from Stefan does not work. It will not place any selected file into a new mail. The new mail itself appears, but only with the text as content.

So I changed the Regulus-script - which works fine - a little bit.

The next step is to handle a multiple selection. For instance, to add four files ( jpg > 250kb / png < 250kb / pdf / txt ) to one Mail. Only the big sized picture is the one to be scaled. Should I try this with REPEAT ?

Here my mod


tell application "Finder"
	set my_list to {"jpg", "png", "gif"}
	try
		set theFile to (item 1 of (get selection)) as alias
		set fileName to name of theFile
		set my_annex to POSIX path of theFile
		set my_selection to kind of theFile
		set my_suffix to name extension of theFile
		set my_size to physical size of theFile
		set my_size to my_size / 1000
		set my_size to (round my_size)
	on error
		do shell script "afplay /System/Library/Sounds/glass.aiff"
		display dialog "Falsche Auswahl" with title "Das Programm meldet einen Fehler" buttons {"ok"} default button "ok" giving up after 5
		error number -128
	end try
	
end tell
display dialog my_selection & " / " & my_suffix & " / " & my_size

if my_selection contains "Bild" and my_suffix is my_list and my_size > 250 then
	tell application "Image Events"
		set my_mail to open my_annex
		scale my_mail to size 600
		save my_mail as JPEG in (path to desktop) with icon
		close my_mail
	end tell
	set attachmentPath to (path to desktop as text) & fileName
else
	set attachmentPath to theFile as text
	
end if

tell application "Mail"
	activate
	set my_new_mail to make new outgoing message with properties {subject:"", content:"Anhang: " & fileName & return & return, visible:true}
	tell my_new_mail
		make new to recipient at beginning of to recipients with properties {name:"", address:""}
		make new attachment with properties {file name:(attachmentPath as alias)}
	end tell
end tell

Sure, give it a try and let us know how you do.

2 comments about your code:

  1. You shouldn’t tell an application to do something when the application isn’t required to do it. As such move this line out of the Finder tell block of code… set my_list to {“jpg”, “png”, “gif”}

  2. You need to change this line to the following (notice the word “in”)…

if my_selection contains "Bild" and my_suffix is in my_list and my_size > 250 then