Permission Problems with OS 10.7.5

First of all, thanks for this great forum. I don’t know if I have any business being here. I did some scripting many, many years ago but unfortunately haven’t don’t recall much about it. My problem is I have a script that worked fine on my old MacBook Pro running OS 10.5.8 but doesn’t work on my MacBook running 10.7.5. This is NOT my script but the person who created the script is no longer with the company so I was hoping I could find some help here on how to tweak this problem. Here’s a part of the script:


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 "Finder"
		set currentFolderpath to container of (path to me)
		set currentFolder to currentFolderpath as text
	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 = "Principal Emails") or (whichemails = "Both") then
		set f to choose file with prompt "Choose file with SPR summaries:" without invisibles
		set SummaryFile to "" & f
		
		tell application "Finder"
			set summaryfolder to container of (f) as text
		end tell
		
	end if
	
	if (whichemails = "Stuck Student Reminders") or (whichemails = "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 = "Principal Emails") or (whichemails = "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...
		

I hope I did that right. If not please let me know. The problem is I’m getting the message “The document summaryfile.txt could not be opened. You don’t have permission.”
I looked around on the web for a solution but couldn’t find it. It does look like others are having problems with permissions as well but I tried some of those solutions with no success. Could anyone offer advice here or is this something that’s way beyond my abilities.

Thanks in advance for your help!

Hello

At first look, it seems that your problem is with a handler whose code is not given in your message :

GetFileData(SummaryFile)

Gave us also the code of the other handler :
GetText(SummFiPath)

It’s probably linked to Sandboxing.

Yvan KOENIG (VALLAURIS, France) mardi 23 octobre 2012 10:32:42

Hi Carl. Welcome to MacScripter.

As Yvan’s pointed out, the end of the script you posted is missing. It would also help the diagnosis if you could tell what it’s supposed to do!

One thing I’ve noticed which probably isn’t connected with your immediate problem is that the ‘quit’ intercept handler’s wrong. It should have a ‘continue quit’ statement or the quit won’t happen:

on quit
	display dialog "Emails finished!" buttons {"OK"} default button 1 -- No "Cancel" button.
	continue quit -- Continue the quitting process after the dialog's dismissed.
end quit

Thank you for your responses. Nigel, thanks for finding the error in the quit interceptor. I’ll include the corrected script.
What this script eventually does is generate emails with data from a csv file. I only included this part of the script because at the “summaryfile.txt” point is where I was getting the permissions error. I didn’t want to put too much of the script here because I didn’t know if that was frowned upon since it’s rather lengthy. But here it is:


(* 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 "Finder"
		set currentFolderpath to container of (path to me)
		set currentFolder to currentFolderpath as text
	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 = "Principal Emails") or (whichemails = "Both") then
		set f to choose file with prompt "Choose file with SPR summaries:" without invisibles
		set SummaryFile to "" & f
		
		tell application "Finder"
			set summaryfolder to container of (f) as text
		end tell
		
	end if
	
	if (whichemails = "Stuck Student Reminders") or (whichemails = "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 = "Principal Emails") or (whichemails = "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 = "Stuck Student Reminders") or (whichemails = "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 g from 1 to (count 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 of SummData) > 6 then --we have grade level data (as one would hope...)
			--concatenate.
			repeat with f from 7 to (count item g of SummData)
				set currelem to ((f - 6) mod 7)
				
				if currelem = 1 then --grade
					set gradelist to gradelist & item f of item g of SummData
				else if currelem = 2 then -- # students
					set studentslist to studentslist & item f of item g of SummData
				else if currelem = 3 then -- progress
					set proglist to proglist & item f of item g of SummData
				else if currelem = 4 then -- attendance
					set attendlist to attendlist & item f of item g of SummData
				else if currelem = 5 then -- prog per attendance
					set ppslist to ppslist & item f of item g of SummData
				else if currelem = 6 then -- ave days since last login
					set avedayslist to avedayslist & item f of item g of SummData
				else if currelem = 0 then -- num with high tries
					set triesctlist to triesctlist & item f of item g of SummData
				end if
				
			end repeat
		end if
		--attach all back in.
		--set parsedSummData to parsedSummData & [items 1 through 6 of item g of SummData, gradelist, studentslist, proglist, attendlist, ppslist, avedayslist, triesctlist]
		set parsedSummData to parsedSummData & [[(item 1 of item g of SummData), (item 2 of item g of SummData), (item 3 of item g of SummData), (item 4 of item g of SummData), (item 5 of item g of SummData), (item 6 of item g of SummData), 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 h from 1 to (count parsedSummData)
		set currentdatarow to item h of 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 i from 1 to (count mailtext)
			--display dialog (item i of mailtext)
			--Get the list of content for emails.  One paragraph per line.  Look @ each, concatenate.
			set currentline to item i of mailtext
			
			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 j from 1 to lencurrentline
					
					--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 of currentline is not equal to "" then
						set checksym to item 1 of item j of currentline
						if checksym = "@" then
							--we have a substitution to make.
							set checkcode to item j of currentline
							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 of currentline)
						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 i from 1 to (count StuckData)
		
		set currentdata to item i of StuckData
		
		set emailtext to spacer
		
		repeat with j from 1 to (count singlemailtext)
			set currentline to item j of singlemailtext
			set currentpara to ""
			set attachmentses to {}
			
			repeat with k from 1 to (count currentline)
				--inside the loop, parse each, looking for keywords.
				set curritem to item k of currentline
				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 TID to text item delimiters
	set text item delimiters to "$"
	
	tell application "TextEdit"
		activate
		
		open MailFile as alias
		
		tell front document
			
			set parCt to (count paragraph)
			set emailTxt to {}
			
			repeat with getPara from 1 to parCt -- first row is the header row.
				set bbb to get paragraph getPara
				set bsplit to text items of bbb
				set emailTxt to emailTxt & [bsplit]
			end repeat
			
			close
		end tell
	end tell
	
	set text item delimiters to TID
	
	return emailTxt
end GetText

on GetFileData(FileName)
	
	--data file output from python scrpt
	
	set TID to text item delimiters
	set text item delimiters to ","
	--Read in CSV data
	tell application "TextEdit"
		activate
		
		
		open FileName as alias
		
		tell front document
			
			set parCt to (count paragraph)
			set FileData to {}
			
			repeat with getPara from 2 to parCt
				set bbb to get paragraph getPara
				set bsplit to text items of bbb
				set FileData to FileData & [bsplit]
			end repeat
			
			close
		end tell
	end tell
	
	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
			
			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
		--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.
		set etitle to "ST Math:  " & (month of (current date)) & " Update -- " & schoolname
		
		--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
		
		
		return true
	end tell
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
		
		
		--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
		
		
		
		
		
		return true
	end tell
end sendStuckEmail

Thanks again for your quick responses and your help!

Hi Carl. Sorry about the delay getting back. It’s a long script!

It works on my Snow Leopard system at least up to and including the opening of the .txt and .csv files, so Yvan’s probably right about it being a sandboxing issue in Lion/Mountain Lion. I haven’t been paying much attention to that topic, so I’m not sure what it implies! If it means your system’s reluctant to let the script open the files in TextEdit, you may be able to avoid the issue ” and massively speed up the data collection ” by replacing the GetText() and GetFileData() handlers with these versions:

on GetText(MailFile)
	
	--output from math file
	
	set TID to text item delimiters
	set text item delimiters to "$"
	
	-- Change the next line to 'set mailParas to paragraphs of (read file MailFile as «class utf8»)' if the email data are likely to have been saved as UTF-8. Hopefully, they won't have been saved as UTF-16!
	set mailParas to paragraphs of (read file MailFile)
	
	script o
		property emailTxt : mailParas
	end script
	
	repeat with getPara from 1 to (count mailParas)
		set item getPara of o's emailTxt to text items of item getPara of o's emailTxt
	end repeat
	
	set text item delimiters to TID
	
	return o's emailTxt
	
end GetText

on GetFileData(FileName)
	
	--data file output from python scrpt
	
	set TID to text item delimiters
	set text item delimiters to ","
	
	--Read in CSV data
	-- Change the next line to 'set dataParas to paragraphs of (read file FileName as «class utf8»)' if the CSV data are likely to have been saved as UTF-8.
	set dataParas to paragraphs of (read file FileName)
	
	script o
		property FileData : dataParas
	end script
	
	repeat with getPara from 2 to (count dataParas)
		set item getPara of o's FileData to text items of item getPara of o's FileData
	end repeat
	
	set text item delimiters to TID
	
	return rest of o's FileData
	
end GetFileData

Contrary to what it says in the script’s opening comments, the essential .txt files are called “summaryfile.txt” and “stuckfile.txt”. In my tests, I’ve interpreted “IN THIS FILE” to mean “IN THIS FOLDER”, but perhaps the script’s been saved as a bundle?

There are several places in the script where square brackets [] have been used instead of braces {} to represent lists. Square brackets originally indicated a kind of list which was obsolete even before I started AppleScripting fifteen years ago. Nowadays they’re automatically reinterpreted as “brace” lists, but they may cease to work one day and should ideally be corrected.

Hope there’s something useful here. :slight_smile:

While we are talking about old constructs: vector and linked list, did they ever work? An Apple Script list, is as I have understood a vector by itself, at least it behaves like a vector, which is kind of an array for the uninitiated (with regards to how it works). So, I really wonder about the linked list, and how it was used, if it ever was used, or if it was something that was never implemented, other than reserving the word for it.

I am not sure about the usability since we do have the reference operator, and that linked lists easily can be simulated with lists of integers, that makes the linked list thing kind of redundant anyway.

Thanks

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