View Single Post
Thats good :)

Once again many thanks for solving the problem, I was very stuck and tearing my hair out. I find that I need to write some Applescript about once a year and each time its like starting from scratch. The good thing about ScriptDebugger is that it does show where items are hidden, the bad thing is the price!

I have included my working code below. The script calculates the end date of a task given the start date and duration and writes the end date to the column "End".
The Omni Outline document must have three columns named "Start", "End" and "Duration" and they must be of types date, date and duration respectively. The script ignores weekends and the next step would be to ignore public holidays.

best wishes
Simon

Code:
--display dialog "Script that calulates the end date based on a start date and a duration" buttons "OK"

--Outliner document must have the following columns
--"Start" type date
--"Duration" type duration
tell front document of application "OmniOutliner Professional"
	
	
	try
		-- extract the number of hours in the working day
		tell column "Duration"
			--declare a record variable ready to populate
			set y to {shouldUseVerboseFormat:boolean, hoursPerDay:integer, hoursPerWeek:integer, hoursPerMonth:integer} as record
			--Now read the 'record' in from the column
			set y to format
			--now extract the value of hours per day
			set hPD to |hoursPerDay| of y
			--display dialog "hPD is " & hPD buttons "OK"
		end tell
		
		
		repeat with oneRow in rows --onerow is the row being processed
			set theStartDate to value of cell "Start" of oneRow
			set theDuration to value of cell "Duration" of oneRow
			--Calculate the number of days the task will take
			set DayCount to theDuration div hPD
			
			set the value of cell "End" of oneRow to (CalculateEndDate(theStartDate, DayCount) of me)
			
		end repeat
	on error
		display dialog "Your outline must have these columns: Start, Duration, End"
	end try
end tell


--set myNewDate to myDate + (2 * days), adds 2 days to the date and time stored in myDate.
--set myNewDate to myDate + (4 * hours), adds 4 hours to the current time and date.
--set myNewDate to myDate + (30 * minutes), adds 30 minutes to the current time and date.
on CalculateEndDate(dStart, DurationDays)
	-- dStart is the startdate and is passed as a date
	-- Duration is the number of days
	-- routine calculates the end date based on the working days of the week
	set dend to dStart
	repeat DurationDays - 1 times
		-- get the day of the week
		
		set dayOfWeek to weekday of dend
		if dayOfWeek = Friday then
			--add 3 days to date
			set dend to dend + (3 * days) --skip the weekend
		else
			--add 1 day to date
			set dend to dend + (1 * days)
		end if
	end repeat
	return dend
	--display dialog "End Date is " & date string of dend buttons "OK"
	-- 	set sp to "/"
	-- 	set the day_stamp to ((the day of (dend)) as string)
	-- 	set the month_stamp to ((the month of (dend)) * 1) as string
	-- 	set the year_stamp to ((the year of (dend)) as string)
	-- 	set date_stamp to day_stamp & sp & month_stamp & sp & year_stamp
	--return date_stamp
	
end CalculateEndDate