Help needed with script to send one or more files per email.

I want the script to send all files with extension .png from the desktop in an email.

I have:

tell application “Finder” to try
set tFiles to (every file of desktop whose name extension is “png”) as alias list
on error – Tiger
set tFiles to (every file of desktop whose name extension is “png”) as alias as list
end try

if tFiles is {} then – nothing found
else
repeat with AnItem in tFiles – attach AnItem to the message
end repeat
end if


set {tTo, tSender, tSubject, tContent} to {“1@2-3.com”, “1@2-3.com”, ("PNG “), " Shutdown Failed pics”}

delay 5

tell application “Mail”
activate
set nMessage to make new outgoing message with properties {sender:tSender, subject:tSubject, content:((do shell script “date ‘+%Y.%m.%d’”) & (tContent & return & return)), visible:true}
tell nMessage
make new to recipient at end of to recipients with properties {address:tTo}
tell content to make new attachment with properties {file name:(tFiles as alias)} at after last paragraph
end tell
send nMessage
end tell

This script works if there is only one .png file on the Desktop. If more than one it does not. I get error message:

Can’t make {alias “OS 861 Seva:Users:861user:Desktop:23-39-49.png”, alias “OS 861 Seva:Users:861user:Desktop:23-41-37.png”} into type alias.

So I need some help to adjust this script to being able to send either any amount of messages, or lets say exactly 3 (I control how many there are there as an other script puts them there).

Many thanks.

Hi,

I don’t use Tiger, but I guess it works, if you put the try statement in the tell Finder line into a separate line


tell application "Finder"
	try
		set tFiles to (every file of desktop whose name extension is "png") as alias list
	on error -- Tiger
		set tFiles to (every file of desktop whose name extension is "png") as alias as list
	end try
end tell

But if you’re going to process each file in a repeat loop, you can use this syntax
by coercing the Finder file specifier to an alias

tell application "Finder"
	set tFiles to every file of desktop whose name extension is "png"
end tell


.

repeat with AnItem in tFiles -- attach AnItem to the message
	set P to POSIX path of (AnItem as alias) -- POSIX path is just an example
end repeat

Note: the if tFiles is {} line also isn’t needed, because if the list is empty, the code within the repeat loop will never be executed

Hi StefanK.

thanks for this. Both work if there is only one file, but as soon as there is more than one it does not. (I am BTW testing in 10.5 and 10.4 both did not work).

I get this error message:

So what is wrong?

Thanks

Hi

I had a similar problem. This worked for me in 10.5


tell application "Finder"
   try
       set tFiles to (every file of desktop whose name extension is "png") as alias list
   on error -- Tiger
       set tFiles to (every file of desktop whose name extension is "png") as alias
   end try
end tell



In Leopard this line is sufficient, because the alias list bug has been fixed


tell application "Finder" to set tFiles to (every file of desktop whose name extension is "png") as alias list

My script above must work in Tiger.
Do you use nested tell blocks or some other unusual syntax.
Your error occurs obviously in the on error part of the try block

Can you please post the displayed error message of the actual try block


tell application "Finder"
	try
		set tFiles to (every file of desktop whose name extension is "png") as alias list
	on error e
		display dialog e
		set tFiles to (every file of desktop whose name extension is "png") as alias as list
	end try
end tell

Did that and get error message:

If I run that the result is:

{alias “OS E:Users:e:Desktop:Picture 1 copy 2.png”, alias “OS E:Users:e:Desktop:Picture 1.png”}

It does not in 10.5 or 10.4, sorry

Not that I know, script is at the end for you perusal.

it occurs in this line at the alias point
tell content to make new attachment with properties {file name:(tFiles as alias)} at after last

Can’t make {alias “OS E:Users:e:Desktop:Picture 1 copy 2.png”, alias “OS E:Users:e:Desktop:Picture 1.png”} into type alias.

Running this section I get as a result:

{alias “OS E:Users:e:Desktop:Picture 1 copy 2.png”, alias “OS E:Users:e:Desktop:Picture 1.png”}

And using it in my script I get the same error message.

any thoughts?

the script with one suggestion included:


tell application "Finder"
	try
		set tFiles to (every file of desktop whose name extension is "png") as alias list
	on error e
		display dialog e
		set tFiles to (every file of desktop whose name extension is "png") as alias as list
	end try
		
	--
	set {tTo, tSender, tSubject, tContent} to {"1@xx.xx", "1@xx.xx", ("PNG "), " Shutdown Failed pics"}
	
	delay 5
	
	tell application "Mail"
		activate
		set nMessage to make new outgoing message with properties {sender:tSender, subject:tSubject, content:((do shell script "date '+%Y.%m.%d'") & (tContent & return & return)), visible:true}
		tell nMessage
			make new to recipient at end of to recipients with properties {address:tTo}
			tell content to make new attachment with properties {file name:(tFiles as alias)} at after last paragraph
		end tell
		send nMessage
	end tell
end tell

I see, it’s not the Finder part, the error occurs in the Mail part.
It would have been more helpful to run the Finder part (my suggested script) separately to eliminate this possibility

You have to create one attachment for each file, a list won’t be accepted

tell application "Finder"
	set tFiles to every file of desktop whose name extension is "png"
	
	--
end tell
set {tTo, tSender, tSubject, tContent} to {"1@xx.xx", "1@xx.xx", ("PNG "), " Shutdown Failed pics"}

delay 5

tell application "Mail"
	activate
	set nMessage to make new outgoing message with properties {sender:tSender, subject:tSubject, content:((do shell script "date '+%Y.%m.%d'") & (tContent & return & return)), visible:true}
	tell nMessage
		make new to recipient at end of to recipients with properties {address:tTo}
		repeat with oneFile in tFiles
			tell content to make new attachment with properties {file name:oneFile as alias} at after last paragraph
		end repeat
	end tell
	send nMessage
end tell

hi StefanK

This one works under 10.5, will test later under 10.4.

Thanks! Learning all the time.

hi StefanK

and under 10.4 too! Thanks a million!