looking for critique/improvements

I wrote this last week. Initially, it was just going to be a reminder for me to create a daily staffing report with a blank email. But the day before Thanksgiving was a bit slow, so I started playing. This does work exactly as I envisioned, but I’m sure there are some ways to improve it. The biggest challenge I had was how to remove people from the list as they were assigned a status; I went with temporary lists which then fed the “real” lists. Anywho, I humbly throw this to the masses for any suggestions.



display dialog "Send VisCom staffing report?" default button 2 giving up after 7200

if gave up of the result is true then error number -128


set theStaff to {"Luke", "Leia", "Chewie", "Lando", "Jabba", "Ben"}
set staffOut to {}
set staffSick to {}
set staffTDY to {}
set staffTelework to {}

try #allows for no one to be here
	set staffHere to choose from list theStaff with prompt "Who is here?" with multiple selections allowed and empty selection allowed
end try

if (count of staffHere) is not 0 then
	set telework to choose from list staffHere with prompt "Is anyone teleworking?" with multiple selections allowed and empty selection allowed
	
	set tempTDY to {}
	repeat with emplTDY in staffHere
		if emplTDY is not in telework then
			set end of tempTDY to emplTDY
		end if
	end repeat
	
	set tempTDY to choose from list tempTDY with prompt "Is anyone on travel?" with multiple selections allowed and empty selection allowed
end if


set tempHere to staffHere
set staffHere to {}
repeat with emplHere in tempHere
	if emplHere is in telework then
		set emplHere to emplHere & " (telework)"
		set end of staffTelework to text of emplHere
		
	else if emplHere is in tempTDY then
		set emplHere to emplHere & " (TDY)"
		set end of staffTDY to text of emplHere
	else
		set end of staffHere to text of emplHere
	end if
end repeat

#reset staffHere
set staffHere to staffHere & staffTelework & staffTDY



#create list of employees that are not here
repeat with emplOut in theStaff
	if emplOut is not in tempHere then
		set the end of staffOut to text of emplOut
	end if
end repeat






set allOut to staffOut
#create list of employees on sick leave
if (count of staffOut) is not 0 then
	
	set tempOut to staffOut
	set staffOut to {}
	set tempSick to choose from list tempOut with prompt "Is anyone sick?" with multiple selections allowed and empty selection allowed
	
	repeat with emplOut in tempOut
		if emplOut is not in tempSick then
			set end of staffOut to emplOut
		end if
	end repeat
	
	
	
	set tempOut to staffOut
	set staffOut to {}
	
	
	repeat with emplOut in allOut
		if emplOut is in tempSick then
			set emplOut to emplOut & " (SL)"
			set end of staffSick to text of emplOut
		else
			set emplOut to emplOut & " (AL)"
			set end of staffOut to text of emplOut
		end if
	end repeat
	
	set staffOut to staffOut & staffSick
	
end if






set textHere to ""
repeat with emplHere in staffHere
	set textHere to textHere & text of emplHere & "<br>"
end repeat


set textOut to ""
repeat with emplOut in staffOut
	set textOut to textOut & text of emplOut & "<br>"
end repeat

try
	tell application "Microsoft Outlook"
		set newMessage to make new outgoing message with properties {subject:"Staffing", content:"<B><U>IN</B></U>" & textHere & "<br><br>" & "<B><U>OUT</B></U>" & "<br>" & textOut & "<br><br>"}
		make new recipient at newMessage with properties {email address:{name:"Han Solo", address:"han.solo@corellia.gov"}}
		open newMessage
	end tell
end try


You may study that :

property original : true

display dialog "Send VisCom staffing report?" default button 2 giving up after 7200

if gave up of the result is true then error number -128


set theStaff to {"Luke", "Leia", "Chewie", "Lando", "Jabba", "Ben"}
set staffOut to {}
set staffSick to {}
set staffTDY to {}
set staffTelework to {}

--try #allows for no one to be here
set staffHere to choose from list theStaff with prompt "Who is here?" with multiple selections allowed and empty selection allowed
--end try

--if (count of staffHere) is not 0 then
if staffHere is not {} then
	set telework to choose from list staffHere with prompt "Is anyone teleworking?" with multiple selections allowed and empty selection allowed
	
	set tempTDY to {}
	repeat with emplTDY in staffHere
		if emplTDY is not in telework then
			set end of tempTDY to emplTDY
		end if
	end repeat
	
	set tempTDY to choose from list tempTDY with prompt "Is anyone on travel?" with multiple selections allowed and empty selection allowed
	-- end if # (count of staffHere) is not 0
	
	# If staffHere is {} this loop is not executed so we may jump over it
	set tempHere to staffHere
	set staffHere to {}
	if original then
		repeat with emplHere in tempHere
			if emplHere is in telework then
				(*
			set emplHere to emplHere & " (telework)"
			set end of staffTelework to text of emplHere
			*)
				set end of staffTelework to emplHere & " (telework)"
			else if emplHere is in tempTDY then
				(*
			set emplHere to emplHere & " (TDY)"
			set end of staffTDY to text of emplHere
			*)
				set end of staffTDY to emplHere & " (TDY)"
			else
				(*
			set end of staffHere to text of emplHere
			*)
				set end of staffHere to emplHere
			end if
		end repeat
		
	else
		# Alternate version
		repeat with emplHere in tempHere
			if emplHere is in telework then
				" (telework)"
			else if emplHere is in tempTDY then
				" (TDY)"
			else
				""
			end if
			set end of staffTelework to emplHere & result
		end repeat
	end if
	
	# If original staffHere is {} , staffTelework & staffTDY are {} so staffHere remain {}, we may jump over that
	#reset staffHere
	set staffHere to staffHere & staffTelework & staffTDY
	
end if # (count of staffHere) is not 0

# 
#create list of employees that are not here
repeat with emplOut in theStaff
	if emplOut is not in tempHere then
		set the end of staffOut to text of emplOut
	end if
end repeat



set allOut to staffOut
#create list of employees on sick leave
-- if (count of staffOut) is not 0 then
if staffOut is not {} then
	
	set tempOut to staffOut
	set staffOut to {}
	set tempSick to choose from list tempOut with prompt "Is anyone sick?" with multiple selections allowed and empty selection allowed
	
	repeat with emplOut in tempOut
		if emplOut is not in tempSick then
			set end of staffOut to emplOut # Here you insert by reference and it works so, why aren't you doing the same elsewhere ?
		end if
	end repeat
	
	log staffOut
	
	set tempOut to staffOut
	set staffOut to {}
	
	if original then
		repeat with emplOut in allOut
			if emplOut is in tempSick then
				(*
			set emplOut to emplOut & " (SL)"
			set end of staffSick to text of emplOut
			*)
				set end of staffSick to emplOut & " (SL)"
			else
				(*
			set emplOut to emplOut & " (AL)"
			set end of staffOut to text of emplOut
			*)
				set end of staffOut to emplOut & " (AL)"
			end if
		end repeat
		
	else
		# Alternate version
		repeat with emplOut in allOut
			if emplOut is in tempSick then
				" (SL)"
			else
				" (AL)"
			end if
			set end of staffOut to emplOut & result
		end repeat
	end if
	
	set staffOut to staffOut & staffSick
	
end if






set textHere to ""
repeat with emplHere in staffHere
	-- set textHere to textHere & text of emplHere & "<br>"
	set textHere to textHere & emplHere & "<br>"
end repeat


set textOut to ""
repeat with emplOut in staffOut
	-- set textOut to textOut & text of emplOut & "<br>"
	set textOut to textOut & emplOut & "<br>"
end repeat

try
	tell application "Microsoft Outlook"
		set newMessage to make new outgoing message with properties {subject:"Staffing", content:"<B><U>IN</B></U>" & textHere & "<br><br>" & "<B><U>OUT</B></U>" & "<br>" & textOut & "<br><br>"}
		make new recipient at newMessage with properties {email address:{name:"Han Solo", address:"han.solo@corellia.gov"}}
		open newMessage
	end tell
end try

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) mardi 1 décembre 2015 18:29:08

Thanks very much. I’ll take a look at the changes you made. The biggest one that I need to figure out is the original property.

Thanks again!

The original property was just introduced to show you in a single script two ways to code the same actions.

With original set to true, the script would be :

display dialog "Send VisCom staffing report?" default button 2 giving up after 7200

if gave up of the result is true then error number -128


set theStaff to {"Luke", "Leia", "Chewie", "Lando", "Jabba", "Ben"}
set staffOut to {}
set staffSick to {}
set staffTDY to {}
set staffTelework to {}

set staffHere to choose from list theStaff with prompt "Who is here?" with multiple selections allowed and empty selection allowed

if staffHere is not {} then
	set telework to choose from list staffHere with prompt "Is anyone teleworking?" with multiple selections allowed and empty selection allowed
	
	set tempTDY to {}
	repeat with emplTDY in staffHere
		if emplTDY is not in telework then
			set end of tempTDY to emplTDY
		end if
	end repeat
	
	set tempTDY to choose from list tempTDY with prompt "Is anyone on travel?" with multiple selections allowed and empty selection allowed
	
	set tempHere to staffHere
	set staffHere to {}
	
		repeat with emplHere in tempHere
			if emplHere is in telework then
			set end of staffTelework to emplHere & " (telework)"
			else if emplHere is in tempTDY then
			set end of staffTDY to emplHere & " (TDY)"
			else
			set end of staffHere to emplHere
			end if
		end repeat
	
	#reset staffHere
	set staffHere to staffHere & staffTelework & staffTDY
	
end if # 

# 
#create list of employees that are not here
repeat with emplOut in theStaff
	if emplOut is not in tempHere then
		set the end of staffOut to  emplOut
	end if
end repeat



set allOut to staffOut
#create list of employees on sick leave

if staffOut is not {} then
	
	set tempOut to staffOut
	set staffOut to {}
	set tempSick to choose from list tempOut with prompt "Is anyone sick?" with multiple selections allowed and empty selection allowed
	
	repeat with emplOut in tempOut
		if emplOut is not in tempSick then
			set end of staffOut to emplOut
		end if
	end repeat
	
	set tempOut to staffOut
	set staffOut to {}
	
	repeat with emplOut in allOut
			if emplOut is in tempSick then
			set end of staffSick to emplOut & " (SL)"
			else
				set end of staffOut to emplOut & " (AL)"
			end if
		end repeat

	set staffOut to staffOut & staffSick
	
end if

set textHere to ""
repeat with emplHere in staffHere
	set textHere to textHere & emplHere & "<br>"
end repeat


set textOut to ""
repeat with emplOut in staffOut
	set textOut to textOut & emplOut & "<br>"
end repeat

try
	tell application "Microsoft Outlook"
		set newMessage to make new outgoing message with properties {subject:"Staffing", content:"<B><U>IN</B></U>" & textHere & "<br><br>" & "<B><U>OUT</B></U>" & "<br>" & textOut & "<br><br>"}
		make new recipient at newMessage with properties {email address:{name:"Han Solo", address:"han.solo@corellia.gov"}}
		open newMessage
	end tell
end try

With original set to false it would be :

display dialog "Send VisCom staffing report?" default button 2 giving up after 7200

if gave up of the result is true then error number -128


set theStaff to {"Luke", "Leia", "Chewie", "Lando", "Jabba", "Ben"}
set staffOut to {}
set staffSick to {}
set staffTDY to {}
set staffTelework to {}

set staffHere to choose from list theStaff with prompt "Who is here?" with multiple selections allowed and empty selection allowed

if staffHere is not {} then
	set telework to choose from list staffHere with prompt "Is anyone teleworking?" with multiple selections allowed and empty selection allowed
	
	set tempTDY to {}
	repeat with emplTDY in staffHere
		if emplTDY is not in telework then
			set end of tempTDY to emplTDY
		end if
	end repeat
	
	set tempTDY to choose from list tempTDY with prompt "Is anyone on travel?" with multiple selections allowed and empty selection allowed
	
	set tempHere to staffHere
	set staffHere to {}
	repeat with emplHere in tempHere
		if emplHere is in telework then
			" (telework)"
		else if emplHere is in tempTDY then
			" (TDY)"
		else
			""
		end if
		set end of staffTelework to emplHere & result
	end repeat
	
	#reset staffHere
	set staffHere to staffHere & staffTelework & staffTDY
	
end if # (count of staffHere) is not 0

#create list of employees that are not here
repeat with emplOut in theStaff
	if emplOut is not in tempHere then
		set the end of staffOut to emplOut
	end if
end repeat



set allOut to staffOut
#create list of employees on sick leave

if staffOut is not {} then
	
	set tempOut to staffOut
	set staffOut to {}
	set tempSick to choose from list tempOut with prompt "Is anyone sick?" with multiple selections allowed and empty selection allowed
	
	repeat with emplOut in tempOut
		if emplOut is not in tempSick then
			set end of staffOut to emplOut
		end if
	end repeat
	
	
	set tempOut to staffOut
	set staffOut to {}
	
	repeat with emplOut in allOut
		if emplOut is in tempSick then
			" (SL)"
		else
			" (AL)"
		end if
		set end of staffOut to emplOut & result
	end repeat
	
	set staffOut to staffOut & staffSick
	
end if

set textHere to ""
repeat with emplHere in staffHere
	set textHere to textHere & emplHere & "<br>"
end repeat


set textOut to ""
repeat with emplOut in staffOut
	set textOut to textOut & emplOut & "<br>"
end repeat

try
	tell application "Microsoft Outlook"
		set newMessage to make new outgoing message with properties {subject:"Staffing", content:"<B><U>IN</B></U>" & textHere & "<br><br>" & "<B><U>OUT</B></U>" & "<br>" & textOut & "<br><br>"}
		make new recipient at newMessage with properties {email address:{name:"Han Solo", address:"han.solo@corellia.gov"}}
		open newMessage
	end tell
end try

As I don’t own/use Outlook (like every Mer.oSoft products) I didn’t studied the code ruling it.

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) mercredi 2 décembre 2015 17:47:27