So I have the following script that, thanks to several VERY nice folks here, allows me to grab all my gigs from my Palm Desktop calendar for any given month.
to FixDate(b)
set new_Date to {}
repeat with c from 1 to 2
if (b's word c) < 10 then
set end of new_Date to ("0" & (b's word c) & "/")
else
set end of new_Date to ((b's word c) & "/")
end if
end repeat
set end of new_Date to "20" & (b's word 3)
return (new_Date as string)
end FixDate
on sortEvents(a, l, r)
script o
property p : a
end script
using terms from application "Palm Desktop"
repeat with i from l + 1 to r
set thisEvent to o's p's item i
set thisTime to thisEvent's start time
repeat with j from i to l by -1
if (j > l) and (thisTime comes before o's p's item (j - 1)'s start time) then
set o's p's item j to o's p's item (j - 1)
else
set o's p's item j to thisEvent
exit repeat
end if
end repeat
end repeat
end using terms from
end sortEvents
on monthRange(m, y)
tell {(date (m & " 1 " & y)) - 1}
set end to beginning + 32 * days + 1
set end's day to 1
it
end tell
end monthRange
set theYear to text returned of (display dialog "Please enter a year:" default answer (current date)'s year)
tell (choose from list {"January", "February", "March", "April", "May", "June", "July", "August", ¬
"September", "October", "November", "December"} with prompt "Please choose a month:")
if it is false then error number -128
set theMonth to item 1
end tell
tell application "TextEdit"
activate
if not (exists document 1) then
make new document at beginning with properties {name:"NewGigs"}
end if
end tell
set {preDate, postDate} to monthRange(theMonth, theYear)
tell application "Palm Desktop"
set myEvents to properties of events whose start time > preDate and start time < postDate and title contains "gig"
my sortEvents(myEvents, 1, count myEvents)
repeat with i in myEvents
set gigName to title of i
set beginTime to short date string of (start time of i)
set endTime to short date string of (end time of i)
set myHour to time string of start time of i
set myEndHour to time string of end time of i
set start_char to 1
set end_char to length of myHour
set all_chars to (characters of myHour)
set end_char to -6
try
set myTime to text start_char thru end_char of myHour
set myEndTime to text start_char thru end_char of myEndHour
end try
set a to (start time of i)
set aa to a's short date string
set new_Date to my FixDate(aa)
set myStuff to {new_Date & " " & " - " & " " & myTime & ", " & new_Date & " " & " - " & " " & myEndTime & ", " & gigName} as string
tell application "TextEdit"
activate
set last paragraph of document 1 to myStuff & return & return
end tell
end repeat
tell application "TextEdit"
activate
end tell
end tell
This does what I need it to do…except now, I am using CMS Made Simple, a great, opensource web content management system. Its calendar module wants dates as yyyy-mm-dd; the applescript gives them to me as m/dd/yyyy.
Sooo, I need some assistance in making this conversion and having it fit into this applescript.
Any takers?
Many thanks,
Todd Reid
Model: G5 dual 2.0
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)
to FixDate(b)
set new_Date to {}
repeat with c from 1 to 2
if (b's word c) < 10 then
set end of new_Date to ("0" & (b's word c) & "/")
else
set end of new_Date to ((b's word c) & "/")
end if
end repeat
set end of new_Date to "20" & (b's word 3)
return (new_Date as string)
end FixDate
with
to FixDate(b)
tell b to return (its year as string) & "-" & my addZero(its month as integer) & "-" & my addZero(its day)
end FixDate
on addZero(v)
return text -2 thru -1 of ("0" & v as string)
end addZero
and
set a to (start time of i)
set aa to a's short date string
set new_Date to my FixDate(aa)
with
set a to (start time of i)
set new_Date to my FixDate(a)
You should use a different method when doing calculations. (The various string properties of the date are best used when you’re just displaying dates to the user.)
set eventList to {{title:"big gig", start_time:current date, end_time:(current date) + 1 * hours}} -- Example
set gigList to {}
repeat with thisItem in eventList
set thisName to title of thisItem
set thisStart to formatDate(start_time of thisItem)
set thisEnd to formatDate(end_time of thisItem)
set end of gigList to thisStart & ", " & thisEnd & ", " & thisName
end repeat
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to return & return
set gigList to "" & gigList
set AppleScript's text item delimiters to ASTID
tell application "TextEdit"
activate
if not (exists document 1) then
make new document at beginning with properties {name:"NewGigs"}
end if
tell document 1 to set its text to its text & gigList
end tell
on formatDate(theDate)
-- do shell script "/usr/bin/php -r 'echo date(\"m/d/Y - g A\", strtotime(\"" & theDate & "\"));'"
do shell script "/usr/bin/php -r 'echo date(\"Y-m-d - h \", strtotime(\"" & theDate & "\"));'"
end formatDate
Bruce,
If you look at the script above, what I’m doing is grabbing events out of my Palm Desktop calendar, writing these to a CSV file, so that I can post on my website in the format that the calendar module of my CMS requires. It assumes a 24-hour clock, so really, all I need to do at this point is grab the beginning time and end time of each event in a 24-hour format.
set theYear to text returned of (display dialog "Please enter a year:" default answer (current date)'s year)
tell (choose from list {"January", "February", "March", "April", "May", "June", "July", "August", ¬
"September", "October", "November", "December"} with prompt "Please choose a month:")
if it is false then error number -128
set theMonth to item 1
end tell
tell application "TextEdit"
activate
if not (exists document 1) then
make new document at beginning with properties {name:"NewGigs"}
end if
end tell
set {preDate, postDate} to monthRange(theMonth, theYear)
tell application "Palm Desktop"
set myEvents to properties of events whose start time > preDate and start time < postDate and title contains "gig"
my sortEvents(myEvents, 1, count myEvents)
repeat with i in myEvents
set gigName to title of i
set beginTime to formatDate(start time of i) of me
set endTime to formatDate(end time of i) of me
tell application "TextEdit"
activate
set last paragraph of document 1 to beginTime ", " & endTime & ", " & gigName & return & return
end tell
end repeat
tell application "TextEdit" to activate
end tell
on sortEvents(a, l, r)
script o
property p : a
end script
using terms from application "Palm Desktop"
repeat with i from l + 1 to r
set thisEvent to o's p's item i
set thisTime to thisEvent's start time
repeat with j from i to l by -1
if (j > l) and (thisTime comes before o's p's item (j - 1)'s start time) then
set o's p's item j to o's p's item (j - 1)
else
set o's p's item j to thisEvent
exit repeat
end if
end repeat
end repeat
end using terms from
end sortEvents
on monthRange(m, y)
tell {(date (m & " 1 " & y)) - 1}
set end to beginning + 32 * days + 1
set end's day to 1
it
end tell
end monthRange
on formatDate(theDate)
do shell script "/usr/bin/php -r 'echo date(\"Y-m-d - H:i\", strtotime(\"" & theDate & "\"));'"
end formatDate
Bruce,
I have asked a lot of you already, but it seems I’m still getting 12-hour, not 24-hour times out of this script; the events that are supposed to be pm are still am.
I missed that earlier. It’s fixed in the script above.
These should also work:
on formatDate(someDate)
tell someDate to tell ((100000000 + ¬
(its year) * 10000 + ¬
(its month) as integer) * 100 + ¬
(its day)) as string to ¬
set theDate to text 3 thru 6 & "-" & text 9 thru 10 & "-" & text 11 thru 12
tell time of (someDate) to tell (1000000 + ¬
it div hours * 10000 + ¬
it mod hours div minutes * 100 + ¬
it mod minutes) as string to ¬
set theTime to text 2 thru 3 & ":" & text 4 thru 5
return theDate & " - " & theTime
end formatDate
on formatDate(someDate)
tell (someDate as «class isot» as string) to return text 1 thru 10 & " - " & text 12 thru 16
end formatDate