Applescript: Collect Daily Statistics

I keep a journal of sorts, but wanted to filter out some of the quantitative or repetitive data I occasionally record about myself (how much I’m sleeping, my weight, exercise stats, etc.) into separate files that can be easily manipulated in something like Excel and displayed in charts if I ever choose to do so. Since I hate Excel, I want to do this without having to open it, or anything else if possible. I created a simple applescript, while not elegant, helps me with this task. I simply launch the script whenever I want to record this information for the day and it saves the data in separate text files as tab delimited data by date.

See the script below.

Remember to change the “[PATH TO FILE]” to the location in your home where you want this info to be saved (for example /Users/ephialtes/Documents/statistics”

You can easily modify this to ask your own questions. This script is not very efficient either and doesn’t check for bad input on your part.

–Choose day to record this will start each line of the log files for this day
set my_date to ( (current date)’s year as string) & “.” & ( (current date)’s month as integer) as string) & “.” & ( (current date)’s day as string)
set chosen_date to display dialog “Which day are you recording stats for?” default answer my_date
set chosen_date to text returned of chosen_date

–Ask the questions you want to record data for and save them in variables
set bedtime to text returned of (display dialog “What time did you go to bed yesterday?” default answer “”)
set hours_slept to text returned of (display dialog “How many hours did you sleep?” default answer “”)

set health_notes to text returned of (display dialog “How do you feel today?” default answer “”)

set bfast to text returned of (display dialog “What did you eat for breakfast?” default answer “”)
set lunch to text returned of (display dialog “What did you have for lunch?” default answer “”)
set dinner to text returned of (display dialog “What did you have for dinner?” default answer “”)
set snack to text returned of (display dialog “Did you have any snacks or other food?” default answer “”)

set exercise to text returned of (display dialog “Did you exercise? If so, what did you do?” default answer “”)
set my_weight to text returned of (display dialog “Did you weight yourself? If so, how do you weigh in kg?” default answer “”)

–Write the files. I have chosen to save the answers in different files according to my own needs
set sleep_loc to “/Users/[PATH TO FILE]/sleep.txt”
set sleep_file to POSIX file sleep_loc
write_to_file(((chosen_date & tab & bedtime) & tab & hours_slept & tab & sleep_notes & return), sleep_file, true)

if health_notes is not “” then
   set health_loc to “/Users/[PATH TO FILE]/health.txt”
   set health_file to POSIX file health_loc
   write_to_file( (chosen_date & tab & health_notes & return), health_file, true)
end if

set food_loc to “/Users/[PATH TO FILE]/food.txt”
set food_file to POSIX file food_loc
write_to_file( (chosen_date & tab & bfast & tab & lunch & tab & dinner & tab & snack & return), food_file, true)

if exercise is not “” then
   set exercise_loc to “/Users/[PATH TO FILE]/exercise.txt”
   set exercise_file to POSIX file exercise_loc
   write_to_file( (chosen_date & tab & exercise & return), exercise_file, true)
end if

if my_weight is not “” then
   set weight_loc to “/Users/[PATH TO FILE]/weight.txt”
   set weight_file to POSIX file weight_loc
   write_to_file( (chosen_date & tab & my_weight & return), weight_file, true)
end if

–The following useful file writing routine found on Apple’s website.
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file