View Single Post
Calendared in the next seven days (by day of week, from iCal)

Code:
#!/bin/sh
# ver 0.15
# Allows for flexible horizon - edit value of FIRSTDAY and LASTDAY in first line
# FIRSTDAY=0 starts from today (midnight last night)
# FIRSTDAY=1 is from midnight tonight, etc.

FIRSTDAY=0
LASTDAY=7
OSXVER=$(sw_vers -productVersion | cut -d . -f 2)

if [ $OSXVER -eq 7 ]; then
    CALTABLE=zicselement
    REMCODE=24
    LOCN=zlocation1
elif [ $OSXVER -ge 8 ]; then
    CALTABLE=zicselement
    REMCODE=24
    LOCN=zlocation
else
    CALTABLE=zcalendaritem
    REMCODE=9
    LOCN=zlocation
fi

NOW=$(date +%s)
ZONERESET=$(date +%z | awk '
{if (substr($1,1,1)!="+") {printf "+"} else {printf "-"} print substr($1,2,4)}') 
YEARZERO=$(date -j -f "%Y-%m-%d %H:%M:%S %z" "2001-01-01 0:0:0 $ZONERESET" "+%s")
STARTDATE="($YEARZERO + zstartdate)";
ENDDATE="($YEARZERO + zenddate)";
DAY=86400;
HORIZON=$(($NOW + ($LASTDAY * $DAY)));

TODAY=$(date -v0H -v0M -v0S +%s) #Midnight: set the time component to 00:00
START=$(($TODAY+($FIRSTDAY * $DAY)))

MATCHES="(z_ent!=$REMCODE) and ($STARTDATE >= $START) and ($STARTDATE < $HORIZON)"
TOTAL=$(sqlite3 $HOME/Library/Calendars/Calendar\ Cache  "
SELECT count(*) FROM $CALTABLE WHERE $MATCHES")

if [ $FIRSTDAY -eq 0 ]; then
	printf "%s\n\n" "CALENDAR ($TOTAL events in next $LASTDAY days)"
else
	printf "%s\n\n" "CALENDAR ($TOTAL events between $FIRSTDAY and $LASTDAY days ahead)"
fi

# %w day of week, %m month, %d day of month, %H %M Hour Month
sqlite3 $HOME/Library/Calendars/Calendar\ Cache "
SELECT  strftime('%w|%m|%d|%H:%M',$STARTDATE, 'unixepoch'),
ztitle, replace($LOCN,'
',' '), zisallday, strftime('%H:%M',$ENDDATE, 'unixepoch') 
FROM $CALTABLE 
WHERE $MATCHES
ORDER BY zstartdate
" | awk '
function monthname(i) {
	return substr("JanFebMarAprMayJunJulAugSepOctNovDec",((i-1)*3)+1,3)
}
function dayname(i) {
	return substr("SunMonTueWedThuFriSat",(i*3)+1,3)
}
BEGIN {FS="\|"; mday=0; wkday=0}
{
	if (mday!=$3) {mday=$3; wkday=$1;
		print dayname(wkday), monthname($2), mday
	}
	printf "     "; if ($7!=1) {printf $4 "-" $8 " "}; printf $5
	if ($6!="") {printf " [" $6 "]"}
	printf "\n"
}'

Last edited by RobTrew; 2012-10-21 at 08:55 AM.. Reason: Ver 0.10 corrects issue with multi-line location fields & facilitates editing of horizon of M to N days [edit values of FIRSTDAY, LASTDAY in first lines]