Screen Capture to Mail Content

Can someone tell me why my screen capture is not being pasted into the content section of a new mail message. The problem is i can’t seem to get the cursor to go to the content section of mail for it to work. I tried this with TextEdit and it works fine.

display dialog "Use the Right mouse button to Capture selection to Mail or Left mouse button to the Desktop."

do shell script "DATE=`date '+%Y-%h-%d-%H-%M-%S'`;
FILE=~/Desktop/screenshot-${DATE}.png;
screencapture -i  $FILE"
tell application "Mail"
	activate
	make new outgoing message with properties {subject:"Data you requested", visible:true}
	tell application "System Events"
		keystroke "v" using {command down}
	end tell
	activate
end tell

Hi Polish

shell scripting is not my strong point but this works for me.

display dialog "Use the Right mouse button to Capture selection to Mail or Left mouse button to the Desktop."
do shell script "DATE=`date '+%Y-%h-%d-%H-%M-%S'`;
FILE=~/Desktop/screenshot-${DATE}.png;
screencapture -i  $FILE"
delay 0.5
tell application "Finder"
	activate
	set x to name of every file of desktop whose name starts with ("screenshot" as string)
	set y to (path to desktop folder) & x as string
	set y to y as alias
end tell
tell application "Mail"
	activate
	set themessage to make new outgoing message with properties {subject:"Data you requested", visible:true}
	tell themessage
		tell content
			make new attachment with properties {file name:y} at before first paragraph
		end tell
	end tell
end tell

Thanks Guys
Jacques, Your script works somewhat the way i want it to, but if i use the right button on the mouse it works just as i want but it also puts a copy of the capture on the desktop. If i use the left mouse button it doesn’t put the capture in the mail message. The display dialog box is not necessary if i can get it to work using the right button without putting a copy on the desktop.
PolishPrince

Hi PolishPrince,

You script doesn’t work because the focus is not in the body of the message. Also, I think you should send the keystrokes to process “Mail”. This may not be necessary, but that’s how Apple says to do it. Anyway, in the following I used tabs to place the focus on the body, then pasted the screenshot.


set s to "Data you requested"
do shell script "/usr/sbin/screencapture -c"
tell application "Mail"
	activate
	set om to (make new outgoing message with properties ¬
		{subject:s, visible:true})
	repeat while content of om is ""
		my SendKey(ASCII character 9, {})
	end repeat
-- the content now contains 1 tab
	set content of om to ""
end tell
SendKey("v", {command down})
--
on SendKey(c, params)
	tell application "System Events"
		tell process "Mail"
			keystroke c using params
		end tell
	end tell
	return
end SendKey

I’m using Jaguar, but think it should work in your OS.

gl,

Kei
I don’t know why but when i run your code it hangs my machine. I can’t even get control of the machine, i have to do a hard shutdown.
PolishPrince

Sorry about that. I didn’t think the tabs would stop before the body. See Jacques’ last post.

Hi Jacques. Thanks for the info. I wish I could set attributes here in Jaguar.

gl,

Thanks to all
You have helped me very much.
PolishPrince