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)