How to run scripts using Mail, Safari and TextEdit in the background?

I would like to copy the text of a message in Mail and paste it into a new TextEdit document so I can XML tag it, and download a file linked in the mail message using Safari.

When the script runs, I can’t do anything else on my Mac, because the apps and docs have to be active.
This is because I’ve been using keyboard functions in my script, such as the following snippet:

tell application "Mail" to activate
tell application "System Events"
	tell process "Mail"
		keystroke "a" using command down
		delay 1
		keystroke "c" using command down
		delay 2
	end tell
end tell

tell application "TextEdit" to activate
tell application "System Events"
	tell process "TextEdit"
		keystroke "n" using command down
		delay 2
		keystroke "v" using command down
	end tell
end tell

There must be a way to do this so I can work in other apps while this runs. Any help out there?

Model: PowerMac G5 OSX 10.4.11
AppleScript: 1.10.7
Browser: Safari 525.18
Operating System: Mac OS X (10.4)


As far as I know, GUI scripting requires that the used app is at front.

You may look at the AppleScript dictionaries of the two app to learn if you may do what you want without GUI scripting.

Yvan KOENIG (from FRANCE mardi 3 juin 2008 17:58:56)

Thanks for the reply Yvan.
I didn’t find any help in the dictionaries, which is why these scripts are GUI in first place.

Here is one example not using GUI. There are lots on this forum and some in the /computer_name/Library/Scripts/Mail Scripts/ folder on your computer.



tell application "Mail"
	set m to item 1 of (get selection)
	if m = {} then
	end if
	set _sender to sender of m
	set _subject to subject of m
	set _date to date received of m as string
	set _contents to content of m
end tell

tell application "TextEdit"
	make new document at beginning with properties {text:_contents}
end tell


why the hell do you use GUI scripting, both applications are fairly scriptable, for example

tell application "Mail" to set theText to content of item 1 of (get selection)
tell application "TextEdit"
	make new document
	set text of document 1 to theText
end tell

Thanks for the info - I now have another issue.
I am on a previously-owned Mac, and new to Applescript (former VB writer).
A former co-worker Mac guru has shown me the dictionary for Mail is missing a couple of classes, including Message, which is why I didn’t see it.
Apparently Sdef Editor has been used (or abused) on this system. My friend will help me get it fixed.

Thanks again to all for the scripting help.