On My Own...Am I On The Right Road With This Script

Im adding code to this script http://applescript.net/viewtopic.php?id=19644

I know things are missing but just tell me am I on the right road with whats here…

using terms from application "Mail"
	on perform mail action with messages actionMail for rule actionRule
		tell application "Mail"
			repeat with i from 1 to count of actionMail
				
				set thisMail to item i of actionMail
				set theSubject to the subject of thisMail
				set theBody to (the content of thisMail) as text
				set theBody to my getFirstLine(theBody)
				
				---Starts Verify Idenity??? check lines/compare 2 and 10 for secret phrase
				
				set theBody to my getSecondLine(theBody)
				set theBody to my getTenthLine(theBody)
				set getPassPhrase2 to "2"
				set getPassPhrase10 to "10"
				
				
				if (getSecondLine(theBody) is equal to getPassPhrase2) then
					if (getTenthLine is equal to getPassPhrase10) then --proced
						try --4
							if theSubject contains "<launch>" then --3
								my launchApp(theBody)
								
							else if theSubject contains "<say>" then
								my sayThis(theBody)
								
							else if theSubject contains "<do>" then
								my doShell(theBody)
								
							else if theSubject contains "<send>" then
								my sendThis(theBody)
								
							else if theSubject contains "<put>" then
								set attachName to the name of the first mail attachment of thisMail
								try --2
									save first mail attachment of thisMail in theBody & attachName
								on error
									set newMessage to make new outgoing message with properties {subject:"I was not able to put the file you sent in the requested place"}
									tell newMessage --1
										make new to recipient at end of to recipients with properties {address:"dan.watson.vns@adelphia.net"}
									end tell --1
									send newMessage
								end try --2
								
								
							else if theSubject contains "<script>" then
								do shell script "osascript -e '" & theBody & "'"
							end if
						end try
					end if
					
					
				else if getSecondLine(theBody) is not getPassPhrase2 then
					delete thisMail
				else if getTenthLine(theBody) is not getPassPhrase10 then
					delete thisMail
				end if
				
			end repeat
		end tell
		
	end perform mail action with messages
end using terms from

You don’t say what it is you’re tryingto achieve, or what problems you’re having with the script, so it’s hard to know what you’re looking for. However there are a few glaring issues.

           repeat with i from 1 to count of actionMail
               
               set thisMail to item i of actionMail

Since you don’t use i anywhere else in the loop this can, and should, be replaced with:

repeat with thisMail in actionMail

This will do the same thing as your loop, but it’s much more concise. AppleScript will take care of making sure thisMail iterates through the list of items in actionMail.

Now, this code is never going to work as I think you intend:

               set theBody to my getFirstLine(theBody)
               
               ---Starts Verify Idenity??? check lines/compare 2 and 10 for secret phrase
               
               set theBody to my getSecondLine(theBody)
               set theBody to my getTenthLine(theBody)

Ignoring for now the fact that the handlers ‘getFirstLine’, ‘getSecondLine’ and ‘getTenthLine’ do not exist, you have a fundamental problem in how you’re dealing with the results.

I’m assuming that you indend for these handlers to take a chunk of text representing the body of an email message (‘theBody’) and return a specific line in that text, correct?

Following your code logically, and assuming that ‘theBody’ starts off with the entire message text, you then:

set theBody to my getFirstLine(theBody)

So before this line is run, theBody contains the entire message. You pass it to getFirstLine which, presumably, returns the first line of the text. You then take that result and assign it to theBody - so now theBody contains just the first line of the message.

Now it’s impossible for the next line to run because you cannot get the second line of a piece of text that has only one line (remember, theBody now just has the first line of the original text). Nor you can you get the 10th line of that one-liner. You should not redefine theBody to the result of the handlers. Instead you probably want something more like:

set line1 to my getFirstLine(theBody)
set line2 to my getSecondLine(theBody)
set line10 to my getSecondLine(theBody)

In this way you assign the different results to different variables without impacting the original message text.

In either case it would be far more efficient to write a generic handler that accepts the line number to retrieve rather than separate handlers for each line. For example, you could:

set line1 to my getLine(theBody, 1)
set line2 to my getLine(theBody, 2)
set line10 to my getLine(theBody, 10)

Now you only need to write one handler that take a chunk of text and a line number to retrieve from that text. This makes your script leaner and easier to maintain.

You can then go on to fix your if statements. Instead of:

               if (getSecondLine(theBody) is equal to getPassPhrase2) then

you can:

if line2 = getPassPhrase2 then

(there’s no need to re-call the getSecondLine handler since you’ve already got its result and stored it in a variable called ‘line2’).

Those are the most obvious issues I can see. Post back if you have more questions.

Welcome back, Camelot. Long time no see here.