Hello
As I refuse to use Merdosoft products, I can’t test the entire code.
I tested the handlers GetFileData and GetText under 10.8.2 and they behaved flawlessly.
As I always do my best to move out of tell blocks what is not requiring the application, I made a bit of changes :
on GetText(MailFile)
--output from math file
set fileAsAlias to MailFile as alias
tell application "TextEdit"
activate
open fileAsAlias
tell front document
set theParagraphs to get paragraphs
close
end tell
end tell # TextEdit
set TID to text item delimiters
set text item delimiters to "$"
set emailTxt to {}
repeat with aParagraph in theParagraphs
text items of aParagraph
set emailTxt to emailTxt & [result]
end repeat
set text item delimiters to TID
return emailTxt
end GetText
on GetFileData(FileName)
--data file output from python scrpt
set fileAsAlias to FileName as alias
--Read in CSV data
tell application "TextEdit"
activate
open fileAsAlias
tell front document
set theParagraphs to get paragraphs 2 thru -1
close
end tell
end tell # TextEdit
set TID to text item delimiters
set text item delimiters to ","
set FileData to {}
repeat with aParagraph in theParagraphs
text items of aParagraph
set FileData to FileData & [result]
end repeat
set text item delimiters to TID
return FileData
end GetFileData
set p2d to path to desktop as text
my GetFileData(p2d & "_20110904-220114.csv")
my GetText(p2d & "_20110904-220114 copie.txt")
In a text editor I made several changes in the entire script :
(* AutoSPR Script
INSTRUCTIONS:
*To run, click "Run" above
*First, decide if you would like to choose to run summaries, stuck students, or both.
*Select the file 'summary.csv' when prompted to choose SPR Summary File.
*Select the file 'stuckstudents.csv' when prompted to choose the Stuck Student file.
*Do not click on anything until the script is complete. This may take a few minutes.
*in order for this to work right, you need to have your summaryemail.txt and stuckemail.txt files available IN THIS FILE. Otherwise, you'll be prompted to look for them.
*)
on run
run AutoSPR
end run
on quit
display dialog "Emails finished!"
end quit
script AutoSPR
--Get current folder. Use to find email text in later bits.
tell application "System Events"################
set currentFolder to path of container of (path to me) ################
end tell
--Prompt for which type of doc to generate.
set chooseemails to display dialog "Hello! Which emails would you like to generate?" buttons {"Principal Emails", "Stuck Student Reminders", "Both"} default button 3
set whichemails to button returned of chooseemails
--Prorompt for Summary file
if whichemails is in {"Principal Emails","Both"} then ################
set f to choose file with prompt "Choose file with SPR summaries:" without invisibles
set SummaryFile to "" & f
tell application "System Events" ################
set summaryfolder to path of container of f ################
end tell
end if
if whichemails is in {"Stuck Student Reminders","Both"} then
--Prompt for Stuck Student file
set f to choose file with prompt "Choose file with stuck students information:" without invisibles
set StuckFile to "" & f
end if
if whichemails is in {"Principal Emails", "Both"} then
--Get the mail text.
set SummFiPath to currentFolder & "summaryfile.txt"
--try
set SummMailText to GetText(SummFiPath)
set SummData to GetFileData(SummaryFile)
set asdf to SummaryEmails(SummMailText, SummData, summaryfolder)
--on error xyz
--well, we really need that text! Generate the file. Or else exit, for now.
--display dialog "summaryfile.txt is missing. Please place a copy in this folder and try again."
--end try
--Send the principal emails...
end if
--set StuckFiPath to currentFolder & "singlestuckstudent.txt"
if whichemails is in { "Stuck Student Reminders", "Both"} then ################
set StuckMail to GetText((currentFolder & "stuckfile.txt"))
set StuckData to GetFileData(StuckFile)
set asdf to StuckEmails(StuckMail, StuckData)
--Send the teacher emails.
end if
beep
end script
--End of wrapper script
--Handler for the summary emails
on SummaryEmails(mailtext, SummData, summaryfolder)
--Get data from the file, concatenate all the info on grades, students, etc.
set parsedSummData to []
repeat with item_g in SummData ################
--parse out the repeating grade data
set gradelist to []
set studentslist to []
set proglist to []
set attendlist to []
set ppslist to []
set avedayslist to []
set triesctlist to []
if (count item_g) > 6 then --we have grade level data (as one would hope...)
--concatenate.
repeat with f from 7 to (count item_g)
set currelem to ((f - 6) mod 7)
if currelem = 1 then --grade
set gradelist to gradelist & item f of item_g
else if currelem = 2 then -- # students
set studentslist to studentslist & item f of item_g
else if currelem = 3 then -- progress
set proglist to proglist & item f of item_g
else if currelem = 4 then -- attendance
set attendlist to attendlist & item f of item_g
else if currelem = 5 then -- prog per attendance
set ppslist to ppslist & item f of item_g
else if currelem = 6 then -- ave days since last login
set avedayslist to avedayslist & item f of item_g
else if currelem = 0 then -- num with high tries
set triesctlist to triesctlist & item f of item_g
end if
end repeat
end if
--attach all back in.
--set parsedSummData to parsedSummData & [items 1 through 6 of item_g, gradelist, studentslist, proglist, attendlist, ppslist, avedayslist, triesctlist]
set parsedSummData to parsedSummData & [[(item 1 of item_g), (item 2 of item_g), (item 3 of item_g), (item 4 of item_g), (item 5 of item_g), (item 6 of item_g), gradelist, studentslist, proglist, attendlist, ppslist, avedayslist, triesctlist]]
# maybe replaced by : set parsedSummData to parsedSummData & [[item 1 thru 6 of item_g), gradelist, studentslist, proglist, attendlist, ppslist, avedayslist, triesctlist]] ################
end repeat
--set spacer to "<p><br/> <p><br/>"
--set smspacer to "<p><br/>"
set spacer to "<p><br/>"
set smspacer to "<br>"
repeat with currentdatarow in parsedSummData ################
set etext to spacer
--Need to deal with the attachment. Change fr posix to apple, append rel path.
set asdf to item 3 of currentdatarow
set TID to text item delimiters
set text item delimiters to "/"
set mainattach to text items of asdf
set attachmentses to []
set attachmentname to summaryfolder
repeat with f from 1 to (count mainattach)
set attachmentname to attachmentname & item f of mainattach & ":"
end repeat
set text item delimiters to ""
set attachmentname to (text items 1 through -2 of attachmentname as string)
set attachmentses to attachmentses & attachmentname
set text item delimiters to TID
set attachstring to attachmentname
repeat with currentline in mailtext ################
--display dialog (item i of mailtext)
--Get the list of content for emails. One paragraph per line. Look @ each, concatenate.
set lencurrentline to (count currentline)
if lencurrentline > 1 then
set TID to text item delimiters
set text item delimiters to ""
--look for content.
set currentpara to ""
repeat with item_j of currentline ################
--Look for an @ symbol. If it doesn't start with an @, then add to string. If it does, replace with the appropriate value
if item_j is not equal to "" then
set checksym to item 1 of item_j
if checksym = "@" then
--we have a substitution to make.
set checkcode to item_j
if checkcode = "@SchoolName" then
set currentpara to currentpara & (item 1 of currentdatarow)
else if checkcode = "@IID" then
set currentpara to currentpara & (item 2 of currentdatarow)
else if checkcode = "@SchoolAttendance" then
set currentpara to currentpara & (item 6 of currentdatarow)
else if checkcode = "@SchoolProgress" then
set currentpara to currentpara & (item 5 of currentdatarow)
else if checkcode = "@SchoolStudents" then
set currentpara to currentpara & (item 4 of currentdatarow)
else if checkcode = "@ByGradeProgress" then
repeat with k from 1 to (count item 7 of currentdatarow) --look @ each grade, write in data to string, attach.
set codestr to (item k of item 7 of currentdatarow) & " students have an average progress of " & (item k of item 9 of currentdatarow) & "%, "
if k = (count item 7 of currentdatarow) and k > 1 then --we have mult grades and this is the last.
set codestr to "and " & codestr
end if
set currentpara to currentpara & codestr --ditch the last comma.
end repeat
set currentpara to items 1 through -3 of currentpara
else if checkcode = "@ByGradeProgressbr" then
set codestr to ""
repeat with k from 1 to (count item 7 of currentdatarow) --look @ each grade, write in data to string, attach.
set codestr to (item k of item 7 of currentdatarow) & ": " & (item k of item 9 of currentdatarow) & "% complete" & smspacer
set currentpara to currentpara & codestr
end repeat
else if checkcode = "@ByGradeAttendance" then
repeat with k from 1 to (count item 7 of currentdatarow) --look @ each grade, write in data to string, attach.
set codestr to (item k of item 7 of currentdatarow) & " students have attended " & (item k of item 10 of currentdatarow) & " times, "
if k = (count item 7 of currentdatarow) and k > 1 then --we have mult grades and this is the last.
set codestr to "and " & (items 1 through -3 of codestr)
end if
set currentpara to currentpara & (codestr) --ditch the last comma.
end repeat
else if checkcode = "@ByGradeAttendancebr" then
set codestr to ""
repeat with k from 1 to (count item 7 of currentdatarow) --look @ each grade, write in data to string, attach.
set codestr to (item k of item 7 of currentdatarow) & ": " & (item k of item 10 of currentdatarow) & " logins" & smspacer
set currentpara to currentpara & codestr
end repeat
else if checkcode = "@ByGradeAttendProg" then
repeat with k from 1 to (count item 7 of currentdatarow) --look @ each grade, write in data to string, attach.
set codestr to (item k of item 7 of currentdatarow) & " students have attended " & (item k of item 10 of currentdatarow) & " times and have an average progress of " & (item k of item 9 of currentdatarow) & "%, "
if k = (count item 7 of currentdatarow) and k > 1 then --we have mult grades and this is the last.
set codestr to "and " & (items 1 through -2 of codestr)
end if
set currentpara to currentpara & (codestr) --ditch the last comma.
end repeat
set currentpara to items 1 through -2 of currentpara
else if checkcode = "@ByGradeAttendProgbr" then
set codestr to ""
repeat with k from 1 to (count item 7 of currentdatarow) --look @ each grade, write in data to string, attach.
set codestr to (item k of item 7 of currentdatarow) & ": " & (item k of item 10 of currentdatarow) & " logins, " & (item k of item 9 of currentdatarow) & "% Complete" & smspacer
set currentpara to currentpara & codestr
end repeat
else if checkcode = "@ByGradeComplete" then
repeat with k from 1 to (count item 7 of currentdatarow) --look @ each grade, write in data to string, attach.
set codestr to (item k of item 8 of currentdatarow) & " students in " & (item k of item 7 of currentdatarow) & " have attended " & (item k of item 10 of currentdatarow) & " times and have an average of progress of " & (item k of item 9 of currentdatarow) & "%, completing an average of " & (item k of item 11 of currentdatarow) & "% each time they log in. The average date of their last login is " & (item k of item 12 of currentdatarow) & " days ago. They have an average of " & (item k of item 13 of currentdatarow) & " tries on their current game. "
set currentpara to currentpara & (codestr)
end repeat
else if checkcode = "@ByGradeCompletebr" then
set codestr to ""
repeat with k from 1 to (count item 7 of currentdatarow) --look @ each grade, write in data to string, attach.
set codestr to (item k of item 7 of currentdatarow) & ": " & (item k of item 8 of currentdatarow) & " students, " & (item k of item 10 of currentdatarow) & " logins, " & (item k of item 9 of currentdatarow) & "% Complete, " & (item k of item 11 of currentdatarow) & "% progress per session, " & (item k of item 12 of currentdatarow) & " days on average since last login, " & (item k of item 13 of currentdatarow) & " average tries" & smspacer
set currentpara to currentpara & codestr
end repeat
else if checkcode = "@Attachment" then
set attachmentses to attachmentses & (item (j + 1) of currentline)
end if
else
set currentpara to currentpara & (item_j)
end if
end if
end repeat
set text item delimiters to TID
else
set currentpara to item 1 of currentline
end if
set etext to etext & currentpara & spacer
end repeat
--now, make the darn emails!!
set emailsent to sendSummaryEmail(etext, (item 1 of currentdatarow), attachmentses)
end repeat
end SummaryEmails
--Handler for the Stuck student emails.
on StuckEmails(singlemailtext, StuckData)
set spacer to "<p><br/>"
set smspacer to "<br>"
--then, loop thru the new array, creating the emails.
repeat with currentdata in StuckData ################
set emailtext to spacer
repeat with currentline in singlemailtext ################
set currentpara to ""
set attachmentses to {}
repeat with curritem in currentline ################
--inside the loop, parse each, looking for keywords.
if curritem is not equal to "" then --we have content.
set checksym to item 1 of curritem
if checksym = "@" then
--look for and replace the value with the appropriate value.
if curritem = "@SchoolName" then
set currentpara to currentpara & (item 2 of currentdata)
else if curritem = "@IID" then
set currentpara to currentpara & (item 1 of currentdata)
else if curritem = "@Teacher" then
set currentpara to currentpara & (item 4 of currentdata)
else if curritem = "@TCD" then
set currentpara to currentpara & (item 3 of currentdata)
else if curritem = "@Name" then
set currentpara to currentpara & (item 6 of currentdata)
else if curritem = "@SCD" then
set currentpara to currentpara & (item 5 of currentdata)
else if curritem = "@Grade" then
set currentpara to currentpara & (item 9 of currentdata)
else if curritem = "@YCD" then
set currentpara to currentpara & (item 7 of currentdata)
else if curritem = "@Module" then
set currentpara to currentpara & (item 14 of currentdata)
else if curritem = "@Game" then
set currentpara to currentpara & (item 14 of currentdata)
else if curritem = "@Level" then
set currentpara to currentpara & (item 14 of currentdata)
else if curritem = "@Tries" then
set currentpara to currentpara & (item 16 of currentdata)
else if curritem = "@Progress" then
set currentpara to currentpara & (item 12 of currentdata)
else if curritem = "@Attendance" then
set currentpara to currentpara & (item 10 of currentdata)
else if curritem = "@Attachment" then
set attachmentses to attachmentses & (item (k + 1) of currentline)
end if
else
set currentpara to currentpara & curritem
end if
end if
end repeat
set emailtext to emailtext & currentpara & spacer
end repeat
set emailsent to sendStuckEmail(emailtext, (item 2 of currentdata), (item 4 of currentdata), attachmentses)
end repeat
end StuckEmails
on GetText(MailFile)
--output from math file
set fileAsAlias to MailFile as alias
tell application "TextEdit"
activate
open fileAsAlias
tell front document
set theParagraphs to get paragraphs
close
end tell
end tell # TextEdit
set TID to text item delimiters
set text item delimiters to "$"
set emailTxt to {}
repeat with aParagraph in theParagraphs
text items of aParagraph
set emailTxt to emailTxt & [result]
end repeat
set text item delimiters to TID
return emailTxt
end GetText
on GetFileData(FileName)
--data file output from python scrpt
set fileAsAlias to FileName as alias
--Read in CSV data
tell application "TextEdit"
activate
open fileAsAlias
tell front document
set theParagraphs to get paragraphs 2 thru -1
close
end tell
end tell # TextEdit
set TID to text item delimiters
set text item delimiters to ","
set FileData to {}
repeat with aParagraph in theParagraphs
text items of aParagraph
set FileData to FileData & [result]
end repeat
set text item delimiters to TID
return FileData
end GetFileData
on sendSummaryEmail(emailtext, schoolname, attachmentses)
tell application "Microsoft Outlook"
activate
set principalEmail to ""
set principalFirst to "FIRST"
set principalLast to "LAST"
--Look for principal name
--try
set visible of shared contacts panel to true
tell shared contacts panel
set search type to all fields
set search string to schoolname
--display dialog schoolname
delay 0.15
--set contactnums to count of contacts
# I guess that (count contacts] would be neater ##############
if (count of contacts) > 0 then
repeat with i from 1 to (count of contacts)
--display dialog i
--set end of principals to {first name of contact i, last name of contact i, company of contact i, email addresses of contact i}
--display dialog "" & job title of contact i & ""
if (""&(job title of contact i)) is "Principal" then
set emails to email addresses of contact i
set principalEmail to address of first item of emails
set principalFirst to "" & first name of contact i
set principalLast to "" & last name of contact i
set i to (count of contacts) + 1
end if
end repeat
end if
close
end tell # shared contacts panel
--display dialog principalContact & ""
--on error abycd --any error
set err to "ERROR"
--end try
--Add the principal name to the email.
set etxtsend to "Dear " & principalFirst & "," & emailtext
--get title for email.
tell current application # ADDED to get rid of error -1708 & error -1004 ################
set etitle to "ST Math: " & (month of (current date)) & " Update -- " & schoolname
end tell ################
--deal with attachments.
set eMessage to make new outgoing message with properties {subject:etitle, content:etxtsend}
if principalEmail is not "" then
make new recipient at eMessage with properties {email address:{address:principalEmail}}
end if
if (count attachmentses) > 0 then
repeat with n from 1 to (count attachmentses)
-- display dialog item n of attachmentses
set fi to item n of attachmentses
make new attachment at eMessage with properties {file:fi}
end repeat
end if
open eMessage
end tell # Microsoft Outlook
return true
end sendSummaryEmail
on sendStuckEmail(emailtext, schoolname, teachername, attachmentses)
tell application "Microsoft Outlook"
activate
set teacherEmail to ""
set teacherFirst to teachername
set teacherLast to teachername
--Look for teacher name
--get the record
set visible of shared contacts panel to true
tell shared contacts panel
set search type to all fields
set search string to schoolname
delay 0.15
--fix for use with teachers!
if (count of contacts) > 0 then
repeat with i from 1 to (count of contacts)
if display name of contact i is teachername then
set emails to email addresses of contact i
set teacherEmail to address of first item of emails
--set teacherEmail to "" & first email address
set teacherFirst to first name of contact i & ""
set teacherLast to last name of contact i & ""
set i to ((count of contacts) + 1) -- exit loop
end if
--, last name of contact i, company of contact i, email of contact i}
end repeat
end if
close
end tell # shared contacts panel
--Add the principal name to the email.
set etxtsend to "Dear " & teacherFirst & "," & emailtext
--get title for email.
set etitle to "ST Math: Stuck Student -- " & schoolname
set eMessage to make new outgoing message with properties {subject:etitle, content:etxtsend}
if teacherEmail is not "" then
make new recipient at eMessage with properties {email address:{address:teacherEmail}}
end if
if (count attachmentses) > 0 then
repeat with n from 1 to (count attachmentses)
-- display dialog item n of attachmentses
set fi to item n of attachmentses
make new attachment at eMessage with properties {file:fi}
end repeat
end if
open eMessage
end tell # Microsoft Outlook
return true
end sendStuckEmail
If you don’t understand them, you may ask here for explanations
Yvan KOENIG (VALLAURIS, France) mercredi 24 octobre 2012 16:28:24