Calculate your work hours

this is a handy little script i wrote up to calculate my pay in an email and list my hours for the week.


-- I am in no way responsible for the use of this script or any financial loss that could be caused by this script. use at your own risk.
on findMinString(num)
	set i to 0
	if num = 0 then return "00"
	repeat until i > 55
		if i = num then return i
		set i to i + 5
	end repeat
end findMinString

on findLeastItem(lst)
	tell application "iCal"
		set theLeast to start date of item 1 of lst
		set theIndex to 1
		set iterater to 1
		repeat with i in lst
			if start date of i ≤ theLeast then
				set theLeast to start date of i
				set theIndex to iterater
			end if
			set iterater to iterater + 1
		end repeat
		
		return theIndex
	end tell
end findLeastItem

on removeItemAtIndex(lst, theIndex)
	set newList to {}
	set theLength to length of lst
	if theLength = 1 then
		set newList to {}
	else if theLength = theIndex then
		set newList to items 1 thru (theLength - 1) of lst
	else if theIndex = 1 then
		set newList to items 2 thru theLength of lst
	else
		set newList to items 1 thru (theIndex - 1) of lst & items (theIndex + 1) thru (theLength) of lst
	end if
	return newList
end removeItemAtIndex

on sortEvents(myList)
	set myNewList to {}
	repeat until length of myList = 0
		set leastIndex to findLeastItem(myList)
		set end of myNewList to item leastIndex of myList
		set myList to removeItemAtIndex(myList, leastIndex)
	end repeat
	return myNewList
end sortEvents

on selectCalendar()
	tell application "iCal"
		set cal_list to get name of every calendar
	end tell
	set the_choice to choose from list cal_list
	if the_choice = false then quit
	return item 1 of the_choice
end selectCalendar

on getSecondsOfDays(Ndays)
	return ((current date) - (3600 * 24 * Ndays))
end getSecondsOfDays

set res to display dialog "How Many Days back ?" default answer "5"
set days_back to text returned of res
set tt to display dialog "What is your hourly rate" default answer "20"
set theHourlyRate to text returned of tt

set theCalendar to selectCalendar()
tell application "iCal"
	
	
	set minInHour to 3600
	set outputStr to "
"
	set totalHours to 0
	set today to current date
	
	set weeksHours to every event of calendar theCalendar whose start date is greater than my getSecondsOfDays(days_back)
	set weeksHours to my sortEvents(weeksHours)
	
	repeat with evt in weeksHours
		set duration to (end date of evt) - (start date of evt)
		set sDate to start date of evt as date
		set eDate to end date of evt as date
		set totalHours to totalHours + duration / minInHour
		
		set sMin to my findMinString(minutes of sDate as number)
		set eMin to my findMinString(minutes of eDate as number)
		
		set timeStr to (day of sDate & " " & weekday of sDate & " " & hours of sDate & ":" & sMin & " - " & hours of eDate & ":" & eMin) as string
		
		-- make columns all line up nice
		repeat until length of timeStr > 28
			set timeStr to timeStr & " "
		end repeat
		set timeStr to timeStr & "	:	" & duration / minInHour & "	 Hours"
		set outputStr to outputStr & timeStr & "	" & summary of evt & "
"
	end repeat
end tell

tell application "Mail"
	set outputStr to outputStr & "
	
total hours = " & totalHours & "  : $" & totalHours * theHourlyRate
	
	set str to day of today & "/" & (month of today as number) & "/" & year of today as string
	set newMsG to make new outgoing message with properties {subject:"Hours for week ending " & str, content:outputStr, visible:true}
	activate
	
end tell
outputStr

!! :slight_smile:

Actually, there’s already an AppleScript “constant” hours with that value. There are also number-of-second equivalents for minutes, days, and weeks, though admittedly, since you’re using the new hours and minutes date properties in the script, the use of the constants as well could cause some visual confusion.

Sorry I’ve nothing more constructive to say than that. I don’t use either iCal or Mail. :slight_smile:

i just realised i named the variable wrong, should be secInHour

Heres My Latest one i’ve been using for the last 6 months to calculate my hours, I’ve Ironed out almost all the bugs and this also includes sorting times properly.

  1. It asks how many days back you would like to go you may have to change this if its missing your first day takes decimal points as well (note if you’ve got more than 30 events then the script could take a while as the algorithm sucks
  2. It asks you your hourly rate
  3. It asks you To pick a calendar from iCal (only one sorry)
  4. Creates an email with all the data in it and a total.

Here it is.


--- See first post i will update that one with any changes.

Model: MacBook Pro 2.16 2G
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Does not seem to work with Leopard :frowning:

Sry I don’t have any knowledge of programming therefore I stuck.

Cheers

nic

Sorry, updated it, now it does.

Hmm The Email thing does work now (tried it with your old version) but the new one stalls now at

set the_choice to choose from list (get name of every calendar)

iCal stalls and I get an Event timer …

iCal has to be Force “quitted”

Cheers

Nic

ok, just worked out that ical doesn’t like the choose from list inside the tell block

I’ve Just fixed it, it doesn’t seem to get the error when ical has been loaded before the script is run.

same goes with Address Book

See this doesn’t work


tell app "iCal" to quit
tell application "iCal"
	choose from list {1, 2, 4, 5}
end tell

but this does


tell application "iCal"
	activate
	choose from list {1, 2, 4, 5}
end tell

Thanks a lot!
You are the best.

Cheers from France … Merry X-Mas and Happy new year!