FYI AS Studio Numbers' spreadsheet mileage input form

This is a very basic inputform for keeping track of your fuel consumption.

The basis is a Numbers spreadsheet for registration of your mileage and fuel per date. This app provides you with an input dialog to select the spreadsheet and next let you enter the date, mileage, fuel taken and amount paid. After which the app closes, after closing Numbers first.

As you need the .nib files as well, and can’t post those here, you can download the full Applescript Studio project from http://www.chaosgeordend.nl/documents/Mileage.zip

(*
 Mileage.applescript
 Mileage

 Created by J. van Oostrum in may 2009.
 Copyright 2009 Chaos Geordend. All rights reserved.

 Enter your gasoline receipt into a, very basic, mileage administration spreadsheet.
  
 2009.05.10 New script for Leopard (OS X 10.5.6) and Numbers '09
 
*)

-- Script preferences
-- true : quit the app after entering one receipt
-- false : allow more than one receipt to be entered, quit the app using the menu (or command-Q)
property prptyOneReceipt : true

-- The scripts' properties or global values
property prptyFileOpen : false
property prptyMileage : ""
property prptyFuel : ""
property prptyTotalAmount : ""
property prptyDate : (current date)
global prptyDocument

--
-- check whether the document contains a table
--
on doCheckDocument(theDocument)
	tell application "Numbers"
		open (theDocument as alias)
		if (exists first document) then
			tell first document
				tell first sheet
					if (exists table 1) then
						set prptyFileOpen to true -- the spreadsheet opened seems to be OK
					else
						display dialog "The document contains no table" buttons {"Cancel"} default button 1
					end if
				end tell
			end tell
		else
			display dialog "There is no document opened" buttons {"Cancel"} default button 1
		end if
	end tell
end doCheckDocument

--
-- return the value of all cells of the documents' table
--
on doWriteNewEntry()
	local myDate
	local myMileage
	local myLiters
	local myTotalAmount
	
	-- get the data entered in the form window	
	tell window "addNewEntry"
		-- get formatter, then formatted date
		--copy content of control "date" as text to myDate
		copy content of the control "date" to theDateValue
		set theFormatter to call method "formatter" of object (the control "date")
		--log "Got formatter" -- log to Console for test and debugging
		set myDate to (call method "stringForObjectValue:" of object theFormatter with parameter theDateValue) as text
		--log ("Got date: " & myDate)
		
		copy string value of text field "mileage" to myMileage
		copy string value of text field "fuel" to myFuel
		copy string value of text field "totalAmount" to myTotalAmount
	end tell
	
	-- append the data entered to the spreadsheet
	tell application "Numbers"
		tell first document
			tell first sheet
				tell first table
					-- select all tablecells
					set selection range to range (name of first cell of first row & ":" & name of last cell of last row as string)
					-- add a new row...
					add row below the selection range
					-- ...and populate it
					copy the name of cell 2 in the last row to refDate
					set the value of the first cell in the last row to "=Jaar(" & refDate & ")"
					set the value of the second cell in the last row to myDate
					set the value of the third cell in the last row to myMileage
					set the value of the fourth cell in the last row to myFuel
					set the value of the fifth cell in the last row to myTotalAmount
					-- price per fuel unit (e.g. Gallon or Liter)
					copy the name of cell 4 in the last row to refFuel
					copy the name of cell 5 in the last row to refTotalAmount
					set the value of the sixth cell in the last row to "=" & refTotalAmount & "/" & refFuel
					-- usage
					copy the name of cell 3 in row ((address of the last row) - 1) to refLastMileage -- previous' row amount
					copy the name of cell 3 in the last row to refMileage
					copy the name of cell 4 in the last row to refFuel
					set the value of the seventh cell in the last row to "=(" & refMileage & "-" & refLastMileage & ")/" & refFuel
				end tell
			end tell
		end tell
	end tell
end doWriteNewEntry

--
-- clean up file <theFile>
--
on doRemove(theFile)
	tell application "Finder"
		if exists theFile then
			delete theFile
		end if
	end tell
end doRemove

--
-- get <theFile>'s parent path (source: http://www.nobleswan.com/applescript/AS_snippets.html)
--
on parentPath(theFile)
	tell application "Finder" to return container of theFile as text
end parentPath

--
-- Return a list that comprises all text items of <theString>
-- with the text items separator being defined by <theDelimter>
--
on cutDel(theString, theDelimiter)
	local myList
	local saveDelims
	--
	set saveDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set myList to text items of theString
	set AppleScript's text item delimiters to saveDelims
	return myList
end cutDel

--
-- Return a textstring that comprises all items of <theList>
-- the items separator character is defined by <theDelimter>
--
on joinDel(theList, theDelimiter)
	local myText
	local saveDelims
	--
	set saveDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set myText to theList as text
	set AppleScript's text item delimiters to saveDelims
	return myText
end joinDel

--
-- Search and replace string
-- returns the content, as string, of <sourceStr> wherein <searchString> has been replaced with <replaceString>
--
on replace(theSourceString, theSearchString, theReplaceString)
	local mySourceString
	local mySearchString
	local myReplaceString
	--
	set mySourceString to (theSourceString as text)
	set mySearchString to (theSearchString as text)
	set myReplaceString to (theReplaceString as text)
	
	return my joinDel(my cutDel(mySourceString, mySearchString), myReplaceString)
end replace

--
-- prepare and show the Add New Entry form/dialog
--
on doMenuAddEntry()
	(*
	set string value of text field "mileage" in window "addNewEntry" to prptyMileage
	*)
	tell window "addNewEntry"
		set content of control "date" to prptyDate
		set string value of text field "mileage" to prptyMileage
		set string value of text field "fuel" to prptyFuel
		set string value of text field "totalAmount" to prptyTotalAmount
	end tell
	show window "addNewEntry"
end doMenuAddEntry

--
--
--
on will open theObject
	(*Add your script here.*)
end will open

--
--
--
on clicked toolbar item theObject
	(*Add your script here.*)
end clicked toolbar item

--
--
--
on activated theObject
	(*Add your script here.*)
end activated

--
-- Initialization stuff goes here
--
on launched theObject
	set f to resource path of main bundle -- in case the scripts' path is needed, e.g. for a shell script
	-- select the spreadsheet to be updated
	set prptyDocument to choose file with prompt "Choose Mileage document"
	my doCheckDocument(prptyDocument)
	-- init and show entry form
	my doMenuAddEntry()
end launched

--
-- Menu handler
--
on choose menu item theObject
	set which to the name of theObject
	if which is "addNewEntry" then
		my doMenuAddEntry()
	else if which is "CloseWindow" then
		hide window "addNewEntry"
	else if which is "openDocument" then
		set prptyDocument to choose file with prompt "Choose Mileage document"
		my doCheckDocument(prptyDocument)
	end if
end choose menu item
(*
	else if which is "save" then
		tell application "Numbers"
			close the first document saving yes
		end tell
*)

--
-- Button handler
--
on clicked theObject
	if the name of theObject is "addButton" then
		-- write new entry into the spreadsheet then save and close it
		my doWriteNewEntry()
		--hide window "addNewEntry"
		if prptyOneReceipt then
			quit
		end if
	else if the name of theObject is "cancelButton" then
		-- close, i.e. hide, the form window
		hide window "addNewEntry"
		--TODO enable add Entry menu-item
		--set the state of menu item "addNewEntry" in menu "Edit" to on
		--		log "Menu item: " & the name of menu item "addNewEntry" as text
		--		set enabled of menu item "addNewEntry" to true
	end if
end clicked

--
-- intialize button keyboard equivalents
--
on awake from nib theObject
	set the key equivalent of button "addButton" of window "addNewEntry" to (ASCII character 13) -- return
	set the key equivalent of button "cancelButton" of window "addNewEntry" to (ASCII character 27) -- escape
end awake from nib

--
-- Housekeeping before quiting this app
--
on will quit
	my doSaveAndQuit()
end will quit

--
-- Save the document and close the Numbers application
--
on doSaveAndQuit()
	tell application "Numbers"
		if prptyFileOpen then close the first document saving yes
		quit
	end tell
end doSaveAndQuit

--
-- enable menu item(s)
--
on update menu item theObject
	-- enable add entry menu item in sync with the new entry dialog
	if the name of theObject is "addNewEntry" then
		if (the visible of window "addNewEntry" is true) then
			return false -- disable
		else
			return true -- enable
		end if
	end if
end update menu item