Applescript – Visualise exceptions as activities

In Merlin you can enter exceptions for holidays or exceptional working days in the working time inspector. Exceptions applying for the complete project should be entered in the project calendar. Those affecting only a specific resource, should be entered in its calendar.

From time to time we get asked in support how one could visualize those exceptions in the Gantt. Merlin shows non working days in grey, but it has no explicit function for showing which grey date ranges belong to which exception.

To solve this, we wrote a small applescript creating activities out of the defined exceptions of a resource. To test it, just check the following script:

(* 	Scripting with Merlin 2

	You may incorporate this ProjectWizards sample code into your program(s) without
	restriction.  This ProjectWizards sample code has been provided "AS IS" and the
	responsibility for its operation is yours.  You are not permitted to
	redistribute this ProjectWizards sample code as "ProjectWizards sample code" after having
	made changes.  If you're going to redistribute the code, we require
	that you make it clear that the code was descended from ProjectWizards sample
	code, but that you've made changes.

	Copyright ©2011 ProjectWizards, Melle, Germany. All rights reserved.
*)

(*
	Name: Creates activities for visualation of the exceptions of a selected resource
	(German: Erstellt Vorgänge für die Visualisierung der Ausnahmen im Ressourcenkalender einer ausgewählten Ressource)
	Author:	Vicky Stamatopoulou for ProjectWizards, Copyright 2011
	Info: The script prompts for the name of resource, creates the activities, handles title of exception, start & end dates, and working mode
	Date: 	June 1st 2011	 	       						

*)


———–

property ResourceSelectionMessage : "Which resource's calender would you like to visualize?"
property ErrorMessage1 : "You must have an open project in order to run this script."
property ErrorMessage2 : "No exceptions for the selected resource defined."
property ErrorMessage4 : "No resources defined in the project yet."
property ProgressMessageSingular : " activity created out the exceptions of the selected resource: "
property ProgressMessage : " activities created out the exceptions of the selected resource: "

property exitButton : "Ok"


on ResourceSelection(proj)
	global theName, theemail
	
	tell application "Merlin"
		set listOfRes to title of resources of proj
		if listOfRes is {} then
			display dialog ErrorMessage4 buttons {exitButton} default button 1 with icon stop
			return 0 -- error, no resources, nothing to do
		end if
		-- Prompt the user to select the resource.
		set theResourceName to choose from list listOfRes with prompt ResourceSelectionMessage without multiple selections allowed
		if theResourceName is not equal to false then
			set resList to resources of proj whose title contains theResourceName
			
			set theName to title of item 1 of resList
			return theName
		else
			return 0 -- error, user canceled selection
		end if
	end tell
end ResourceSelection


tell application "Merlin"
	
	try
		set doc to the first document
	on error
		-- if no Merlin open file was found
		activate
		display dialog ErrorMessage1
		set chosenFile to (choose file)
		open chosenFile
		set doc to the first document
	end try
	
	set proj to root project of doc
	set TheResult to 1
	set TheResult to ResourceSelection(proj) of me
	
	if TheResult is not 0 then
		
		set theCals to (assigned calendar of resource TheResult of proj)
		set exceptionRules to exception rules of theCals
		tell theCals
			set Startdays to start day of exception rules
			set EndDays to end day of exception rules
		end tell
		set I to 1
		
		repeat with aRule in exceptionRules
			
			set TheStartDate to item I of Startdays
			set TheEndDate to item I of EndDays
			
			tell aRule
				set TheWorkingMode to working mode
				set TheReasonToAct to reason
				if (TheWorkingMode = working) then
					set myStart to start hour
					set myEnd to end hour
					
				end if
			end tell
			if (TheReasonToAct is missing value) then set TheReasonToAct to "untitled"
			
			set theAct to make new activity at end of activities of proj
			tell theAct
				set title to TheResult & "-" & TheReasonToAct
				delete given planned work
				if (TheWorkingMode = working) then
					set given flag status to green flag
					
					set TheStartDate to TheStartDate + (myStart * 60 * 60)
					set TheEndDate to TheEndDate + (myEnd * 60 * 60)
				else
					set given flag status to red flag
				end if
				set given planned start date min to TheStartDate
				set given planned end date min to TheEndDate
			end tell
			set I to I + 1
			
		end repeat
		
		if I is 1 then
			display dialog ErrorMessage2 buttons {exitButton}
		else
			if I is 2 then
				display dialog ((I - 1) & ProgressMessageSingular & TheResult) as string buttons (exitButton)
			else
				display dialog ((I - 1) & ProgressMessage & TheResult) as string buttons (exitButton)
			end if
		end if
	end if
end tell