Thank you Nigel, this is good!
Rams Free: = total days from when the Rams have been removed from the paddock to the scheduled event date.
The days are being reduced/subtracted if the event is moved into the future (or increased/added if event moved back in time), which isn’t quite right, but that’s easily fixed.
My script referenced two dates (provided by the farmer) that lie in the note of the event. I prefer this method of checking if the event summary needs to be updated, in case the “stay-open” application happens to not be running and an event (somehow) gets moved.
here is my original script that I’ll modify and merge in your changes. (be kind, it’s not exactly “pretty”)
tell application "iCal" to activate
delay 1.5
tell application "System Events"
tell process "iCal"
keystroke "c" using {command down}
set the_string to the clipboard
end tell
end tell
-- get current appointment date
set appointment_date to extractBetween(the_string, "Scheduled ", " from") as string
date appointment_date
--get RamsOut/Free Date from note field
set ramsOut to extractBetween(the_string, "Rams Out: ", return & "Misc Notes:") as string
date ramsOut
set ROdiffseconds to ((date appointment_date) - (date ramsOut)) as integer
set ROdiffdays to round (ROdiffseconds / 86400) as string
--get RamsIn/Into Date from note field
set RamsIn to extractBetween(the_string, "Rams In: ", return & "Rams Out:") as string
date RamsIn
set RIdiffseconds to ((date appointment_date) - (date RamsIn)) as integer
set RIdiffdays to round (RIdiffseconds / 86400) as string
-- get event summary from initial clipboard and assign to variables
-- summary format and example is:
--J. Bloggs
--Timbuktu
--Scan for: Single
--Rams Intro: 85 days
--Rams Free: 46 days
set ical_event to paragraphs of the_string
set ical_event_name to item 1 of ical_event
set ical_event_location to item 2 of ical_event
set ical_event_scanFor to item 3 of ical_event
set ical_event_Intro to item 4 of ical_event
set ical_event_Free to item 5 of ical_event
-- update the days, regardless if they've changed or not
set item 4 of ical_event to "Intro: " & RIdiffdays & " days"
set item 5 of ical_event to "Free: " & ROdiffdays & " days"
set ical_event_Intro_updated to (item 4 of ical_event)
set ical_event_Free_updated to (item 5 of ical_event)
--reconstruct the event summary and send it to the system clipboard
set newclipboard to ical_event_name & return & ical_event_location & return & ical_event_scanFor & return & ical_event_Intro_updated & return & ical_event_Free_updated
set the clipboard to newclipboard
-- update Rams In and Out days and paste into/over summary of event.
tell application "System Events"
tell process "iCal"
delay 0.25
keystroke return
delay 0.25
keystroke "v" using {command down}
delay 0.25
keystroke return
end tell
end tell
---- The handler ----
to extractBetween(SearchText, startText, endText)
set tid to AppleScript's text item delimiters -- save them for later.
set AppleScript's text item delimiters to startText -- find the first one.
set liste to text items of SearchText
set AppleScript's text item delimiters to endText -- find the end one.
set extracts to {}
repeat with subText in liste
if subText contains endText then
copy text item 1 of subText to end of extracts
end if
end repeat
set AppleScript's text item delimiters to tid -- back to original values.
return extracts
end extractBetween
Example data in an event note is:
Job No: 20101231-113352
Name: Joe Bloggs
Contacts:
“03 1234 1234”,
“03 4321 4321”
Address:
1/23 blah hwy
timbuktu
1234
state
Joining No.: 1
Scan For: Single
Sheep Number: 900
Require Stockman & dogs: false
Rams In: Sunday, 7 November 2010
Rams Out: Thursday, 16 December 2010
Misc Notes: Enter via south gate.
The Rams In: and Rams Out: are the two dates I use to re-calculate the event summary day values.
Thanks again Nigel for your help, I find your method a far better solution.
I’ll stress test it with a couple of hundred events and report back.
Cheers,
Model: intel
AppleScript: 2.1.2?
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)