AppleScript: Etwas eigenartige Datumsliste

Es ging darum, eine Liste von Terminen zu bauen und zwar auf Grundlage von etwas eigenwillig formatierten Strings… wem auch immer das helfen mag =)

Code zum markieren einmal anklicken Code im Skript-Editor öffnen

–1/29

–2/22, 24, 26 (to return three different dates)

–2/22-25 (return four different dates)

–2/23-25, 27-28, 3/30 (return six different dates)

–2/21, 23, 25-27 (return five different dates)

–12/22-25, 1/1-2 works too =)

— and even 1/25-2/1

set mydates to my makeDateList(“1/25-2/1”)

repeat with mydate in mydates

my addToiCal(“MyDate2StringCalendar”, mydate, mydate, “blaBla”, “mydesc”)

end repeat

——————————

on makeDateList(datestring)

set myoutput to {}

set AppleScript‘s text item delimiters to “,”

set mydates to every text item of datestring

set AppleScript‘s text item delimiters to “,”

set cd to (current date) – (time of (current date))

set day of cd to 1

set month of cd to 1

repeat with mydate_string in mydates

set mydate_string to delete_space(mydate_string as text)

if mydate_string contains “-” then

set AppleScript‘s text item delimiters to “-“

set StartDate to text item 1 of mydate_string

set EndDate to text item 2 of mydate_string

set AppleScript‘s text item delimiters to “”

copy string2date(StartDate, cd) to StartDate

copy string2date(EndDate, cd) to EndDate

copy {StartDate} to tempDateList

if StartDate < EndDate then

repeat until StartDate = EndDate

set StartDate to StartDate + 60 * 60 * 24

set tempDateList to tempDateList & StartDate

copy StartDate to cd

end repeat

set myoutput to myoutput & tempDateList

else

error “StartDate is not < EndDate!!”

end if

else

copy string2date(mydate_string, cd) to currentDate

set myoutput to myoutput & currentDate

end if

end repeat

return myoutput

end makeDateList

on delete_space(thestring)

repeat with i from 1 to count of characters of thestring

if last character of thestring = ” ” then

set thestring to (((characters 1 through -2 of thestring) as text) as text)

set is_modified to true

else

exit repeat

end if

end repeat

repeat with i from 1 to count of characters of thestring

if first character of thestring = ” ” then

set thestring to (((characters 2 through -1 of thestring) as text) as text)

set is_modified to true

else

exit repeat

end if

end repeat

return thestring

end delete_space

on string2date(thestring, datedummy)

–works with 23 or 1/12 (so day only or month/day)

copy datedummy to whereIStarted

if thestring contains “/” then

set AppleScript‘s text item delimiters to “/”

set theday to (text item 2 of thestring) as integer

set themonth to (text item 1 of thestring) as integer

set AppleScript‘s text item delimiters to “”

set month of datedummy to themonth

set day of datedummy to theday

— if we started last year and should end up in next year… so what

if datedummy < whereIStarted then

set year of datedummy to (year of datedummy) + 1

end if

else

set theday to thestring as integer

set day of datedummy to theday

end if

return datedummy

end string2date

on addToiCal(CalendarTitle, StartDate, EndDate, mySummary, mydescription)

tell application “iCal”

if calendar CalendarTitle exists then

else

make new calendar with properties {name:CalendarTitle}

end if

tell calendar CalendarTitle

make new event at end of events with properties {start date:StartDate, end date:EndDate, summary:mySummary, description:mydescription, allday event:true}

end tell

end tell

end addToiCal

Kleines Update des Codes, da war noch ein Bug drin, der zu einem Endlos-Loop führen konnte…
Zudem ist das jetzt mal ein RealLive-Beispiel, mit Eintragung in iCal =)

Leave a Reply