if then else statement does not work

Our customers upload files to our website, and we receive an email containing lots of order data including a single file download link.
My script parses the email (paragraphs of order_data) and amongst many other tasks, searches for a line starting with “https”, and assigns the variable “download” to the URL as text to go in a MySQL database field.
If there is no file download link, I want to alert the user that no files were uploaded, and place a similar alert in the database field where the URL would have been.

The following scriptlet works perfectly every time:


	repeat with i in order_data
		if i starts with "https" then
			set download to text of i
		end if
	end repeat

The following scriptlets do not work:


	repeat with i in order_data
		if i starts with "https" then
			set download to text of i
		else
			set download to "NO FILES WERE UPLOADED"
			tell application "Finder"
				activate
				display dialog "NO FILES WERE UPLOADED BY CUSTOMER"
			end tell
		end if
	end repeat


	repeat with i in order_data
		if i does not contain "https" then
			set download to "NO FILES WERE UPLOADED"
			tell application "Finder"
				activate
				display dialog "NO FILES WERE UPLOADED BY CUSTOMER"
			end tell
		else if i starts with "https" then
			set download to text of i
		end if
	end repeat

These scriptlets both set download to “NO FILES WERE UPLOADED” and run the Finder dialog, even when “https” is at the start of a line in order_data.

What am I doing wrong?

Browser: Safari 533.21.1
Operating System: Mac OS X (10.6)

Hi,

the first scriptlet which does not work and the working scriptlet have the same syntax except the else branch
so logically both do work or both don’t work.

The problem is probably the missing dereferencing of the list item reference, which can cause unexpected behavior
while doing comparisons with boolean results.
The keyword contents dereferences a list item reference


repeat with i in order_data
	set deRef_i to contents of i
	if deRef_i does not contain "https" then
		set download to "NO FILES WERE UPLOADED"
		tell application "Finder"
			activate
			display dialog "NO FILES WERE UPLOADED BY CUSTOMER"
		end tell
	else if deRef_i starts with "https" then
		set download to text of deRef_i
	end if
end repeat


One obvious possibility is that you’re only looking at ‘download’ after the repeat’s finished. With the script you say does work, ‘download’ either ends up set to something beginning with “https” when the repeat’s finished or it doesn’t exist at all. With the other two, it ends up set to whatever it was set to in the last iteration of the repeat.

Thanks Stefan - Nigel for pointing out the reference issue.

I fixed it and simplified it (for me anyway) by:

  1. setting the download as a default
	
set download to "NO FILES WERE UPLOADED"

  1. moving the modified original if statement into the routine that parses the email data

		set linkcheck to contents of order_data
		if linkcheck does not contain "https" then
			tell application "Finder"
				activate
				display dialog "NO FILES WERE UPLOADED BY CUSTOMER"
			end tell
		end if

  1. keeping the “https” if statement in the later routine that sets the variables for the MySQL routine

		if i starts with "https" then
			set download to text of i
		end if

Success!

Many thanks again