The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   AppleScripting Omni Apps (http://forums.omnigroup.com/forumdisplay.php?f=46)
-   -   Outliner Pro - Read the "hoursPerday" setting (http://forums.omnigroup.com/showthread.php?t=15035)

Simon Knight 2010-01-14 10:53 AM

Outliner Pro - Read the "hoursPerday" setting
 
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

whpalmer4 2010-01-14 11:42 AM

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
[/code]

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
[/code]

and looking at the result. All this with a document that had the appropriate definition of column 3, of course!

Simon Knight 2010-01-14 08:57 PM

Many thanks.

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

Simon

whpalmer4 2010-01-14 09:03 PM

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 :)

Simon Knight 2010-01-15 12:19 AM

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[/CODE]

RobTrew 2010-05-05 02:46 AM

[QUOTE=Simon Knight;71867]I had totally failed to understand that I had to declare a record variable, then populate it then read it!
Simon[/QUOTE]

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[/CODE]

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[/CODE]

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
[/CODE]

[COLOR="White"]--[/COLOR]


All times are GMT -8. The time now is 08:04 PM.

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