Handler for managing exit dates

Hi,

Many of our AppleScripts are executed at night by cron jobs. Most of them fill documents with data and then send them out in PDF format to our customers. But sometimes we don’t want an AppleScript to run, even if it was executed by the corresponding cron job. For example, when the customer needed his documents a little bit earlier and one of my colleagues already ran the script manually during the day.

Now, of course you can edit the crontab accordingly, but that is a tedious job and not an easy task for people who have no technical background.

Therefor I wrote a simple handler, which provides a graphical interface for entering and deleting exit dates - dates where the script will just shut down after being executed by a cron job. This works very good and that’s why I am sharing it, maybe someone can also make good use of it. Please excuse the bad English in the dialogs, here they are shown in German language.

P.S.: Normally I am implementing the menu in the «on open»-handler, so that people can configure the script by dragging Finder items onto it.

Best regards from sunny and warm Berlin,

Martin


property mytitle : "Sample Script"
property exitdates : {"10.03.2010", "20.03.2010"}

on run
	my dspexitdatesmenu()
	
	-- do we have an exit date today?
	set today to do shell script "date \"+%d.%m.%Y\""
	set tmpexitdates to {}
	set exitscript to false
	repeat with exitdate in exitdates
		if (exitdate as text) is not equal to today then
			set exitdateobj to date exitdate
			set todaydateobj to date today
			-- deleting expired exit dates from the list
			if not ((todaydateobj - exitdateobj) > 0) then
				set tmpexitdates to tmpexitdates & exitdate
			end if
		else
			-- we want the exit date to be active for the whole day
			set tmpexitdates to tmpexitdates & exitdate
			set exitscript to true
		end if
	end repeat
	-- setting the cleaned up list
	set exitdates to tmpexitdates
	if exitscript is true then
		return
	end if
end run

-- I am displaying the main menu for the exit date settings
on dspexitdatesmenu()
	set menuitems to {"Enter new exit date", "Delete existing exit date"}
	repeat
		choose from list menuitems with prompt "Exit Date Menu:" with title mytitle OK button name "Choose" cancel button name "Cancel" without multiple selections allowed and empty selection allowed
		set usrchoice to result
		if usrchoice is not false then
			set chosenmenuitem to ((item 1 of usrchoice as list) as text)
			if chosenmenuitem is equal to "Enter new exit date" then
				set exitdate to my askforexitdate()
				if exitdate is not missing value then
					set exitdates to exitdates & exitdate
				end if
			else if chosenmenuitem is equal to "Delete existing exit date" then
				my remexitdates()
			end if
		else
			exit repeat
		end if
	end repeat
end dspexitdatesmenu

-- Ich biete dem Benutzer die Möglichkeit, Sondertermine einzutragen,
-- an denen das Skript nicht ausgeführt wird
on askforexitdate()
	try
		tell me
			activate
			display dialog "Please enter a date where " & mytitle & " should not be executed:" default answer "" buttons {"Cancel", "Enter"} default button 2 with icon note with title mytitle
			set dlgresult to result
			set usrinput to text returned of dlgresult
			if usrinput is "" then
				my askforexitdate()
			else
				-- hat der benutzer ein valides Datum angegeben?
				try
					set exitdate to date usrinput
				on error errmsg number errnum
					my dsperrmsg(errmsg, errnum)
					my askforexitdate()
				end try
				-- wir zeigen dem Benutzer das Datumsobjekt noch einmal zur Prüfung an
				set exitdatestr to (characters 1 through -10 of (exitdate as text)) as text
				tell me
					activate
					display dialog "Is this the correct date?" & return & return & exitdatestr buttons {"No", "Yes"} default button 2 with icon note with title mytitle
					set dlgresult to result
					-- akzeptiert er es, dann bauen wir einen String im Format "dd.mm.yyyy"
					-- und geben ihn zurück
					if button returned of dlgresult is "Yes" then
						set daystr to ((day of exitdate) as integer) as text
						if length of daystr is 1 then set daystr to "0" & daystr
						set monthstr to ((month of exitdate) as integer) as text
						if length of monthstr is 1 then set monthstr to "0" & monthstr
						set yearstr to ((year of exitdate) as integer) as text
						return (daystr & "." & monthstr & "." & yearstr) as text
					else
						my askforexitdate()
					end if
				end tell
			end if
		end tell
	on error errmsg number errnum
		return missing value
	end try
end askforexitdate

-- I am offering the possibility to delete exit dates
on remexitdates()
	if exitdates is {} then
		set errmsg to "There are currently no defined exit dates."
		my dsperrmsg(errmsg, "--")
		return
	end if
	choose from list exitdates with prompt "Please select exit dates to delete:" with title mytitle OK button name "Choose" cancel button name "Cancel" with multiple selections allowed without empty selection allowed
	set usrchoice to result
	if usrchoice is not false then
		set chosenexitdates to usrchoice as list
		set tmpexitdates to {}
		repeat with exitdate in exitdates
			if exitdate is not in chosenexitdates then
				set tmpexitdates to tmpexitdates & exitdate
			end if
		end repeat
		set exitdates to tmpexitdates
	end if
end remexitdates

-- I am displaying error messages to the user
on dsperrmsg(errmsg, errnum)
	tell me
		activate
		display dialog "Sorry, an error occurred:" & return & return & errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with icon stop with title mytitle
	end tell
end dsperrmsg