Getting schedule for a specific resource


#1

Hi @lizard

I’m trying to calculate the available days for all the resources in my project, considering both “normal hours” and “extra & off hours”. I think I’m getting close but haven’t been able to work out how to access resource specific schedules properly. I’ve looked through this forum and the old forums but haven’t found any helpful reference material for accessing a specific resources schedule.

on isWorkingDay(_date, _resource)
	tell application "OmniPlan"
		tell _resource
			
			-- Check that the _date is not a calendar exception (such as a holiday)
			if duration of (calendar day schedules of schedule of _resource where specific date is _date) is 0 then
				-- this seems to be the point of failure! It always returns {} rather than hours as docs lead me to expect
				return false
				
				-- Also check and see if the day of the week is a normal working day
			else if duration of item (weekday of _date as integer) of week day schedules of schedule of _resource is 0 then
				return false
				
				-- Otherwise, it is a working day
			else
				return true
			end if
			
		end tell
	end tell
end isWorkingDay

tell application "OmniPlan"
	set _projectDays to {}
	set _projectAvailableDays to 0
	set _projectStartDate to starting date of front document
	
	-- Six week project length
	repeat with n from 0 to (7 * 6)
		copy (_projectStartDate + n * days) to end of |_projectDays|
	end repeat
	
	-- Count available days for each resource
	repeat with r in resources of front document
		if resource type of r is person then
			repeat with d in _projectDays
				if my isWorkingDay(d, r) then
					set _projectAvailableDays to _projectAvailableDays + 1
				end if
			end repeat
		end if
	end repeat
end tell

return _projectAvailableDays

Any assistance would be much appreciated! Thanks in advance.


#2

I’m flattered that you look to me as an expert, but I haven’t worked in this area lately and don’t know when I might get back to it. If anyone else has insight, please chime in.


#3

No worries 😀 I saw you’d helped out a few other people with their Omni+AppleScript questions, so I assumed you were the one to ask.

Hopefully someone else can give me a hand…


#4

Hi JRamsay!

The only thing that is biting you here is the “is” in “specific date is _date”. Unfortunately, this is doing a full-on date + time comparison and you only intend to be comparing the dates. The specific date property on OmniPlan’s calendar day schedule happens to always be at midnight (the first instant of the given day), so the easiest way to make this script work is to make sure that the date you are comparing is also always at midnight:

set _date to _date - (hours of _date) * hours - (minutes of _date) * minutes - (seconds of _date)
if duration of (calendar day schedules of schedule of _resource where specific date is _date) is 0 then

This ought to give you the right answer.

If you find that it is a bit slow on larger projects you might think about doing this a bit differently. If you count the work days in the week day schedules of the resource (to get the number of “normal” working days each week) and multiply by the number of weeks, you’d much more quickly get the total “normal” work days. Then you could repeat over all calendar day schedules (the holidays/exceptions) where specific date >= _projectStart and specific date <= _projectEnd and just check to see if the resource is off but the normal schedule would have them on (-1 work day) or resource is on but the normal weekday schedule would have them off (+1 work day). It’s a bit more complicated, but would mean that you’d only be looping over the exceptions (which ought to be rare) instead of over every day in the schedule.


#5

Thanks @greg! That solved it. 🎉

Would it be rude to also ask you to take a look at another issue I’m having? Create new customDataEntry using JXA