Create Mail, Cut and Paste Text from Pages

Greetings,
I made a script that I use with iCal to automate a daily email.
Simply, it:

  • creates a new mail
  • opens a Pages document
  • cuts the subject
  • pastes in the mail
  • cuts the mail content
  • pastes in the mail
    (I am still testing so I don’t have auto send yet, but plan on adding that.)

The script works perfectly when I run it manually… however, when I automate it with iCal it produces mixed results… often not cutting correctly from Pages, and hence not pasting the subject correctly.
It has me bewildered… code is below, and if there are any comments on how I might change things, it would be much appreciated.

FYI… running on an iMac, OSX 10.6.6… cutting and pasting Japanese text.

SCRIPT CONTENT:


on run
	
	tell application "Mail"
		set theNewMessage to make new outgoing message with properties {visible:true}
		tell theNewMessage
			make new to recipient at end of to recipients with properties {address:"mail@address.com"}
			make new bcc recipient at end of bcc recipients with properties {address:"mail@address.com"}
		end tell
	end tell
	
	tell application "Finder"
		open document file "2011MasterEmailSend.pages" of folder "2011 JPD Online Version" of folder "Documents" of folder "LinscottFamily" of folder "Users" of startup disk
		
		delay 0.5
		
	end tell
	
	
	tell application "Pages"
		
		activate
		
		tell application "System Events"
			
			tell application process "TextEdit"
				
				delay 0.5
				
				keystroke (ASCII character 31) using shift down -- down, and select text
				
				delay 0.5
				
				keystroke "x" using command down -- Cut the text
				
				delay 0.5
				
			end tell
			
		end tell
		
	end tell
	
	tell application "Mail"
		
		activate
		
		delay 0.5
		
		tell application "System Events"
			
			tell application process "TextEdit"
				
				delay 0.5
				
				keystroke "v" using command down -- Paste email subject		
				
				delay 0.5
				
				keystroke tab
				
				delay 0.5
				
			end tell
			
		end tell
		
	end tell
	
	tell application "Pages"
		
		activate
		
		delay 0.5
		
		tell application "System Events"
			
			tell application process "TextEdit"
				
				delay 0.5
				
				keystroke (ASCII character 31) using shift down -- down
				
				keystroke (ASCII character 31) using shift down -- down
				
				keystroke (ASCII character 31) using shift down -- down, and select text
				
				delay 0.5
				
				keystroke "x" using command down -- Cut the text
				
				delay 0.5
				
				keystroke "s" using command down -- Save document
				
				delay 0.5
				
				keystroke "w" using command down -- Close document
				
				delay 0.5
				
			end tell
			
		end tell
		
	end tell
	
	tell application "Mail"
		
		activate
		
		delay 0.5
		
		tell application "System Events"
			
			tell application process "TextEdit"
				
				delay 0.5
				
				keystroke "v" using command down -- Paste message body			
				
			end tell
			
		end tell
		
	end tell
	
	if weekday of (current date) = Sunday then -- on Sundays, include the country information
		
		tell application "Finder"
			open document file "2011MasterEmailCountry2.pages" of folder "2011 JPD Online Version" of folder "Documents" of folder "LinscottFamily" of folder "Users" of startup disk
			
			delay 0.5
			
		end tell
		tell application "Pages"
			
			activate
			
			tell application "System Events"
				
				tell application process "TextEdit"
					
					keystroke (ASCII character 31) using shift down -- down
					
					keystroke (ASCII character 31) using shift down -- down
					
					keystroke (ASCII character 31) using shift down -- down
					
					keystroke (ASCII character 31) using shift down -- down
					
					keystroke (ASCII character 31) using shift down -- down
					
					keystroke (ASCII character 31) using shift down -- down
					
					keystroke (ASCII character 31) using shift down -- down
					
					keystroke (ASCII character 31) using shift down -- down, and select text
					
					delay 0.5
					
					keystroke "x" using command down -- Cut the text
					
					keystroke "s" using command down -- Save document
					
					keystroke "w" using command down -- Close document
					
				end tell
				
			end tell
			
		end tell
		
		tell application "Mail"
			
			activate
			
			tell application "System Events"
				
				tell application process "TextEdit"
					
					delay 0.5
					
					keystroke "v" using command down -- Paste message body			
					
				end tell
				
			end tell
			
		end tell
		
	end if
	
end run

Model: iMac
AppleScript: 2.3
Browser: Safari 534.13
Operating System: Mac OS X (10.6)

Hi,

I don’t exactly understand all the GUI scripting steps in the script,
but GUI scripting is not needed to cut text out of a Pages document and paste it into a new Mail message.

This example opens the Pages document,
reads the first paragraph for the subject and the third paragraph for the body,
cuts the paragraphs, saves the document and makes a new outgoing messages with the specified subject and body.


set pagesDocument to ((path to documents folder as text) & "2011 JPD Online Version:2011MasterEmailSend.pages")

tell application "Pages"
	open alias pagesDocument
	tell body text of document 1
		set mailBody to paragraph 3
		delete paragraph 3
		set mailSubject to paragraph 1
		delete paragraph 1
	end tell
	save document 1
end tell

tell application "Mail"
	set theNewMessage to make new outgoing message with properties {visible:true, subject:mailSubject, content:mailBody}
	tell theNewMessage
		make new to recipient at end of to recipients with properties {address:"mail@onfire.jp"}
		make new bcc recipient at end of bcc recipients with properties {address:"Kuniguni, jefaya.lakjfiuealkjf@blogger.com"}
	end tell
end tell

There is a design flaw in the OP’s script.

When the script activate Pages, it trigger process TextEdit or process Mail.

A logical scheme would :
activate Pages to trigger the process Pages
activate TextEdit to trigger the process TextEdit
activate Mail to trigger the process Mail.

Yvan KOENIG (VALLAURIS, France) samedi 5 février 2011 22:14:53

Thanks for your advice!
Problem solved. Script is working well, automatically sending emails daily created from the Pages document.

I also have an IF/THEN for Sunday that posts a larger amount of text in the email body.
Of course the Pages document is prepared with the exact amount of lines so that the dates and content match properly.

The full script is here for anyone who may need to create regular emails from a Pages or Text document.


set pagesDocument to "Users:MainUser:Documents:Email Text Content.pages"

tell application "Pages"
	open alias pagesDocument
	tell body text of document 1
		set mailSubject to paragraph 1
		if weekday of (current date) = Sunday then -- on Sundays, include the country information
			set mailBody to paragraph 2 & paragraph 3 & paragraph 4 & paragraph 5 & paragraph 6 & paragraph 7 & paragraph 8 & paragraph 9 & paragraph 10 & paragraph 11 & paragraph 12
			delete paragraph 12
			delete paragraph 11
			delete paragraph 10
			delete paragraph 9
			delete paragraph 8
			delete paragraph 7
			delete paragraph 6
			delete paragraph 5
			delete paragraph 4
			delete paragraph 3
			delete paragraph 2
			delete paragraph 1
			tell application "Mail"
				set theNewMessage to make new outgoing message with properties {visible:true, subject:mailSubject, content:mailBody}
				tell theNewMessage
					make new to recipient at end of to recipients with properties {address:"email@address.com"}
					make new bcc recipient at end of bcc recipients with properties {address:"email@address.com"}
					send
				end tell
			end tell
		else
			set mailBody to paragraph 2 & paragraph 3 & paragraph 4
			delete paragraph 4
			delete paragraph 3
			delete paragraph 2
			delete paragraph 1
			tell application "Mail"
				set theNewMessage to make new outgoing message with properties {visible:true, subject:mailSubject, content:mailBody}
				tell theNewMessage
					make new to recipient at end of to recipients with properties {address:"email@address.com"}
					make new bcc recipient at end of bcc recipients with properties {address:"email@address.com"}
					send
				end tell
			end tell
		end if
	end tell
	save document 1
	close document 1
end tell


Here is a stripped version doing exactly the same job.


set pagesDocument to ("" & (path to documents folder from user domain) & "Email Text Content.pages")

tell application "Pages"
	open pagesDocument
	tell body text of document 1
		set mailSubject to paragraph 1
		if weekday of (current date) = Sunday then -- on Sundays, include the country information
			set mailBody to (paragraphs 2 thru 12) as text
			delete (paragraphs 1 thru 12)
		else
			set mailBody to (paragraphs 2 thru 4) as text
			delete (paragraphs 1 thru 4)
		end if
	end tell
	close document 1 with saving
end tell -- Pages

tell application "Mail"
	set theNewMessage to make new outgoing message with properties {visible:true, subject:mailSubject, content:mailBody}
	tell theNewMessage
		make new to recipient at end of to recipients with properties {address:"email@address.com"}
		make new bcc recipient at end of bcc recipients with properties {address:"email@address.com"}
		send
	end tell
end tell

Yvan KOENIG (VALLAURIS, France) mercredi 16 février 2011 16:45:16

Thanks.
That is way shorter than my original code!
It’s been very helpful and informative learning from you.