The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > Developer > AppleScripting Omni Apps
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Outliner Pro - Read the "hoursPerday" setting Thread Tools Search this Thread Display Modes
Hi,
I am attempting to write a script that uses three columns: Duration, Start and End. I wish to enter values for duration and Start (date) and have the enddate entered in the clumn "End". In order to make a start I need to read the value of the property "HoursPerDay". I have tried various syntax but I can not get it to work.

Script debugger offers the following:
tell column 3
tell properties
tell format
hoursPerDay
end tell
end tell
end tell

but if I attempt to add run this or a modified version with a set myVar to it always fails.
Any ideas?
best wishes
Simon
 
The following code fragment should give you an idea of how to access these values:

Code:
tell application "OmniOutliner Professional"
	tell front document
		tell column 3
			set y to {shouldUseVerboseFormat:boolean, hoursPerDay:integer, hoursPerWeek:integer, hoursPerMonth:integer} as record
			set y to format
			set hPD to |hoursPerDay| of y
			display dialog "hPD is " & hPD buttons "OK"
		end tell
	end tell
end tell
I found the record definition in Script Editor by running

Code:
tell application "OmniOutliner Professional"
	tell front document
		tell column 3
			format
		end tell
	end tell
end tell
and looking at the result. All this with a document that had the appropriate definition of column 3, of course!
 
Many thanks.

I had totally failed to understand that I had to declare a record variable, then populate it then read it!

Simon
 
I was looking all over in the dictionary for hoursPerDay, and of course it isn't there...wouldn't have even known it existed without your question, though I did know that one could configure the values. We both got something out of the exercise :)
 
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
 
Quote:
Originally Posted by Simon Knight View Post
I had totally failed to understand that I had to declare a record variable, then populate it then read it!
Simon
As it happens, it's a bit simpler than that, and your code can be shortened - there's no need to "declare a record variable and then populate it"

You simply need to coerce the reference to a record before accessing its properties:

Code:
tell application id "com.omnigroup.OmniOutlinerPro3"
	tell front document
		set numHours to |hoursPerDay| of ¬
			((format of column 3) as record)
	end tell
end tell
To quickly get all the values in the record, you could coerce it to a list:

Code:
tell application id "com.omnigroup.OmniOutlinerPro3"
	tell front document
		set {blnVerbose, numHrsPerDay, numPerWeek, ¬
			numPerMonth} to (format of column 3) as list
	end tell
end tell
I would probably use OmniPlan to do this kind of thing incidentally, but it's an interesting illustration of Applescript date arithmetic.

One could do something like this rough sketch, for example:

Code:
property pstrStart : "Start"
property pstrDurn : "Duration"
property pstrEnd : "End"

on run
	tell application id "com.omnigroup.OmniOutlinerPro3"
		tell front document
			set recRates to format of (column pstrDurn)
			set {hrsDay, hrsWeek} to {|hoursPerDay| of recRates, |hoursPerWeek| of recRates}
			set dblScale2Day to 24 / hrsDay
			
			repeat with oRow in rows
				tell oRow
					try
						set dteStart to value of cell pstrStart
						-- adjust start date to Monday if it falls on weekend
						set lngWeekDay to (weekday of dteStart) as integer
						if lngWeekDay is (Saturday as integer) then
							set day of dteStart to (day of dteStart) + 2
						else if lngWeekDay is (Sunday as integer) then
							set day of dteStart to (day of dteStart) + 1
						else
							-- see how many working days remain in first week
							set lngDays2WkEnd to (6 - lngWeekDay)
						end if
						
						-- get the number of weeks
						set lngWkHours to (value of cell pstrDurn)
						set {lngWeeks, lngHoursLeft} to {lngWkHours div hrsWeek, lngWkHours mod hrsWeek}
						-- and remaining working days & hours
						set {lngDays, lngHours} to {lngHoursLeft div hrsDay, lngHoursLeft mod hrsDay}
						
						-- add the weeks
						set dteEnd to dteStart + (lngWeeks * weeks)
						-- and any remaining days
						if lngDays > lngDays2WkEnd then set lngDays to (lngDays + 2)
						set dteEnd to dteEnd + (lngDays * days)
						-- and one more day if any outstanding hours remain
						if lngHours > 0 then set dteEnd to (dteEnd + days)
						
						set value of cell pstrEnd to dteEnd
					end try
				end tell
			end repeat
		end tell
	end tell
end run
--

Last edited by RobTrew; 2010-05-10 at 01:41 PM..
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Turn off "Correct Spelling Automatically" as default setting brygoose OmniOutliner 3 for Mac 1 2013-03-06 10:37 AM
Read tasks of "search term" result? digitalimago OmniFocus Extras 2 2010-03-24 10:49 PM
"Unable to Read Document" kevindaly iDisk/MobileMe/.Mac Syncing 3 2009-09-15 09:29 AM
"unable to read document" error fogboy OmniFocus 1 for Mac 2 2007-05-21 10:13 AM
"Mark all items read" -- why has this never worked? Stormchild OmniWeb General 5 2006-08-22 09:21 PM


All times are GMT -8. The time now is 09:54 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.