There has got to be a more efficient way...

Hey everyone,
Thanks for reading. I know that there has to be a more efficient way to code this–I’m new and want to learn. The code is easy to understand. It explains itself better than I could.
Thanks.

		
	else if object_name = "days" then
		set dayslist to {}
		set tableView to table view "alarmslist" of scroll view "alarmslist" of window "main"
		
		set selected_Segments to {}
		repeat with i from 0 to 6
			call method "isSelectedForSegment:" of (control "days" of tab view item "tabs_date" of tab view "tabs" of window "main") with parameter i
			if result is 1 then set end of selected_Segments to i
		end repeat
		my update_status(selected_Segments)
		set selectedday to selected_Segments as Unicode text
		my update_status(selectedday)
		
		if selected_Segments contains 0 then
			set mon of activedays to true
			copy "Monday" to end of dayslist
		end if
		if selected_Segments contains 1 then
			set tues of activedays to true
			copy "Tuesday" to end of dayslist
		end if
		if selected_Segments contains 2 then
			set wed of activedays to true
			copy "Wednesday" to end of dayslist
		end if
		if selected_Segments contains 3 then
			set thurs of activedays to true
			copy "Thursday" to end of dayslist
		end if
		if selected_Segments contains 4 then
			set fri of activedays to true
			copy "Friday" to end of dayslist
		end if
		if selected_Segments contains 5 then
			set sat of activedays to true
			copy "Saturday" to end of dayslist
		end if
		if selected_Segments contains 6 then
			set sun of activedays to true
			copy "Sunday" to end of dayslist
			
		end if
		
		if dayslist is not equal to {} then
			set content of data cell 3 of (selected data row of tableView) to true
		else
			set content of data cell 3 of (selected data row of tableView) to false
		end if
		
		set theDayut to weekday of (current date) as Unicode text
		set theDayAsDay to (current date) + 1 * days
		set theDayAsDay to weekday of theDayAsDay as Unicode text
		my update_status(theDayAsDay)
		
		if dayslist contains theDayut then
			set displayday to "Today at "
			set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
		else if dayslist contains theDayAsDay then
			set displayday to "Tomorrow at "
			set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
		else if dayslist contains "Monday" then
			set displayday to "Monday at "
			set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
			return
		else if dayslist contains "Tuesday" then
			set displayday to "Tuesday at "
			set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
			return
		else if dayslist contains "Wednesday" then
			set displayday to "Wednesday at "
			set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
			return
		else if dayslist contains "Thursday" then
			set displayday to "Thursday at "
			set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
			return
		else if dayslist contains "Friday" then
			set displayday to "Friday at "
			set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
			return
		else if dayslist contains "Saturday" then
			set displayday to "Saturday at "
			set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
			return
		else if dayslist contains "Sunday" then
			set displayday to "Sunday at "
			set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
			return
		else if dayslist = {} then
			set displayday to "No Date Selected!  "
			set content of data cell 3 of (selected data row of tableView) to false
			set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
			return
		end if
	end if
end clicked

I’m pretty busy right now so I did not have the time to seek into your script deeply.

Anyway here is a suggestion:

Use this:

set theDays to {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
repeat with i from 0 to 6
	if selected_Segments contains i then
		set mon of activedays to true
		copy (item (i + 1) of theDays) to end of dayslist
	end if
end repeat

. instead of:

 if selected_Segments contains 0 then
           set mon of activedays to true
           copy "Monday" to end of dayslist
       end if
       if selected_Segments contains 1 then
           set tues of activedays to true
           copy "Tuesday" to end of dayslist
       end if
[.]
       if selected_Segments contains 6 then
           set sun of activedays to true
           copy "Sunday" to end of dayslist           
       end if

Vincent -

Thank you for your reply. The loop worked great! As you can see, I am still learning. I don’t understand how this line:

copys the correct day. If “i” were 0, wouldn’t adding 1 to it return “Tuesday” to the end of daylist?

I appreciate your response. If you do find time in the near future, I would very much like you to review the second half of the code.

Again, thanks.

Trevor

Applescript - in opposite to usual programming languages - starts counting at 1 not 0. So item 1 really means item 1 of x.

As I only have your code as reference and no project files I couldn’t verify my new code.

But this what I’d have done:

else if object_name = "days" then
       set dayslist to {}
       set tableView to table view "alarmslist" of scroll view "alarmslist" of window "main"
       
       set selected_Segments to {}
       repeat with i from 0 to 6
           call method "isSelectedForSegment:" of (control "days" of tab view item "tabs_date" of tab view "tabs" of window "main") with parameter i
           if result is 1 then set end of selected_Segments to i
       end repeat
       my update_status(selected_Segments)
       set selectedday to selected_Segments as Unicode text
       my update_status(selectedday)
       
       set theDayut to weekday of (current date) as Unicode text
       set theDayAsDay to (current date) + 1 * days
       set theDayAsDay to weekday of theDayAsDay as Unicode text
       
        set tempDays to {theDayut, theDayAsDay,"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
       set tempTerms to {"Today at ","Tomorrow at ","Monday", "Tuesday at ", "Wednesday at ", "Thursday at ", "Friday at ", "Saturday at ", "Sunday at "}
       
       repeat with i from 3 to 9
          if selected_Segments contains i then
            set mon of activedays to true
            copy (item (i + 1) of theDays) to end of dayslist
         end if
       end repeat
       
       if dayslist is not equal to {} then
           set content of data cell 3 of (selected data row of tableView) to true
       else
           set content of data cell 3 of (selected data row of tableView) to false
       end if
       
       my update_status(theDayAsDay)

       if dayslist = {} then
           set displayday to "No Date Selected! "
           set content of data cell 3 of (selected data row of tableView) to false
           set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
           return
       else       
            repeat with i from 1 to 9 in tempDays
                if dayslist contains item i of tempDates then
                  set displayday to item i of tempTerms
                  set contents of data cell 2 of (selected data row of tableView) to aname of alarm1 & "
" & displayday & atime of alarm1
			exit repeat
               end if
            end repeat
       end if
end clicked

Your code ain’t that bad. You could just save lots of typing by using some loops and lists.
And loops and lists also make your code more easily readable.