Help: I've almost finished my first applescript

I have a script that is supposed to run as a mail rule, but it has a few quirks.

this script is supposed to run as a rule on all new incoming mail to a certain address in apple’s mail program.

it is supposed to copy the subject and body of the emails to the clipboard, and then open text edit and paste the subject and body.

If I ‘control-click’ the script on a message in mail, and click ‘apply rules’, the script copies the correct data to the clipboard, but it doesnt open text edit to paste it.

If I click “run” on the script in applescript, it opens mail, but it doesnt copy anything, and then it opens text edit and pastes the old contents of the clipboard to a new document.

i assume it has something to do with timing…but i need a bit of help figuring out how to get the script to wait.

heres the script.
i used a program called ui browser to write part of it…so im sure it could be cleaned up…any pointers are appreciated.

thanks
Tim

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			
			repeat with eachMessage in theMessages
				set theContent to content of eachMessage
				set theSubject to subject of eachMessage
				
			end repeat
			set the clipboard to theSubject & theContent
		end tell
	end perform mail action with messages
end using terms from


tell application "TextEdit" to activate
-- may not need to activate application 

tell application "System Events"
	if (system attribute "sysv") < 4144 or UI elements enabled then
		tell application process "TextEdit"
			click menu item "New" of menu "File" of menu bar item "File" of menu bar 1
			click menu item "Paste" of menu "Edit" of menu bar item "Edit" of menu bar 1
			
		end tell
	else
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
			beep
			display dialog "GUI Scripting is not enabled." & return & return & "Check "Enable access for assistive devices" in the Universal Access preference pane (authentication is required), then run this script again." with icon stop buttons {"OK"} default button "OK"
		end tell
	end if
end tell

someone emailed me a solution, so it’s working now…

i only need to figure out how to add the date/timestamp subroutine to it so that i can save each text file with a unique name. any help is appreciated.

here is the code from the functioning script:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			
			repeat with eachMessage in theMessages
				set theContent to content of eachMessage
				set theSubject to subject of eachMessage
				
			end repeat
			
			set the clipboard to theSubject & theContent
		end tell
		tell application "TextEdit" to activate
		-- may not need to activate application 
		
		tell application "System Events"
			if (system attribute "sysv") < 4144 or UI elements enabled then
				tell application process "TextEdit"
					click menu item "New" of menu "File" of menu bar item "File" of menu bar 1
					click menu item "Paste" of menu "Edit" of menu bar item "Edit" of menu bar 1
					
				end tell
			else
				tell application "System Preferences"
					activate
					set current pane to pane "com.apple.preference.universalaccess"
					beep
					display dialog "GUI Scripting is not enabled." & return & return & "Check "Enable access for assistive devices" in the Universal Access preference pane (authentication is required), then run this script again." with icon stop buttons {"OK"} default button "OK"
				end tell
			end if
		end tell
		
	end perform mail action with messages
end using terms from



here is the code for getting a date/timestamp:

on get_datetime_stamp() 
   -- Date/Time routine based considerably on one written by Kevin Talbert. Thanks, Kev. 
   set todaysDate to (current date) 
   set timeString to time string of todaysDate 
   set AppleScript's text item delimiters to ":" 
   set a to every text item in timeString 
   set condTime to ((item 1 of a) & (item 2 of a) & (text 1 through -4 of (item 3 of a))) 
   set {m, d, y} to {month, day, year} of todaysDate 
   set monthList to {January, February, March, April, May, June, ¬ 
      July, August, September, October, November, December} 
   repeat with i from 1 to 12 
      if m = (item i of monthList) then 
         set monthString to text -2 thru -1 of ("0" & i) 
         exit repeat 
      end if 
   end repeat 
   set dayString to text -2 thru -1 of ("0" & d) 
   set yearString to text -2 thru -1 of (y as string) 
   set datetime_stamp to monthString & "/" & dayString & "/" & yearString & "-" & condTime 
   return datetime_stamp 
end get_datetime_stamp

Two quick points:

Under OS X, a filename may not contain either a colon “:” or a slash “/”, so you will need to modify this handler to use something other than those two characters. Secondly, AppleScript date objects are only accurate to the second, and it is possible, (perhaps even likely), that your script will save files more frequently than once every second, so the timestamp will not assure unique names.