这似乎是一个有趣的问题,所以我给你写了一个脚本。您的跑步记录会保存到应用程序支持文件夹中名为 RunningTracker.txt 的文件中。每条记录都是一个由{日期、英里、分钟}组成的列表。
如果您需要修改运行记录文件,您只需将文件读入applescript,根据需要调整列表列表,然后将其写回文件。可以从下面的代码中复制其中的读写部分。
无论如何,这是代码。祝你好运。
property appName : "RunningTracker"
set saveFile to (path to application support folder from user domain as text) & appName & ".txt"
-- initial question
display dialog "Do you know how far you ran today?" buttons {"Show Statistics", "No", "Yes"} default button "Yes" with title appName
set ans to button returned of the result
if ans is "No" then
open location "http://maps.google.com"
return
else if ans is "Show Statistics" then
set showStatistics to true
else
set showStatistics to false
end if
-- get the list of stored records
set theRecords to {}
try
set theRecords to read file saveFile as list -- get the stored values
end try
-- get todays record and save it to the saveFile
if showStatistics then
if theRecords is {} then
display dialog "There are no running records yet. Please create a record before proceeding!" buttons {"Quit"} default button 1 with title appName
return
else
set todaysRecord to item -1 of theRecords
end if
else
set todaysRecord to getNewRecord()
set end of theRecords to todaysRecord
set success to writeTo(saveFile, theRecords, list, false)
if not success then
display dialog "Error: could not write todays data to the saveFile!" buttons {"Quit"} default button 1 with title appName
return
end if
end if
-- create todays statistics
set todayDate to item 1 of todaysRecord
set todayMiles to item 2 of todaysRecord
set todayMins to item 3 of todaysRecord
set todaysStatistic to dateString(todayDate) & return & todayMiles & " miles in " & todayMins & " mins ==> " & decimalRound(todayMins / todayMiles, 2) & " mins/mile"
-- create weekly and total statistics
set weeklyMiles to 0
set weeklyMins to 0
set totalMiles to 0
set totalMins to 0
set secondsPerWeek to 7 * days
set weeklyDate to missing value
repeat with aRecord in my theRecords
set thisDate to item 1 of aRecord
if (todayDate - thisDate) is less than or equal to secondsPerWeek then
if weeklyDate is missing value then set weeklyDate to (item 1 of aRecord)
set weeklyMiles to weeklyMiles + (item 2 of aRecord)
set weeklyMins to weeklyMins + (item 3 of aRecord)
end if
set totalMiles to totalMiles + (item 2 of aRecord)
set totalMins to totalMins + (item 3 of aRecord)
end repeat
set weeklyStatistic to dateString(weeklyDate) & " - " & dateString(todayDate) & return & weeklyMiles & " miles in " & weeklyMins & " mins ==> " & decimalRound(weeklyMins / weeklyMiles, 2) & " mins/mile"
set totalStatistic to "Total (" & (count of theRecords) & " records)" & return & totalMiles & " miles in " & totalMins & " mins ==> " & decimalRound(totalMins / totalMiles, 2) & " mins/mile"
-- display the statistics
display dialog todaysStatistic & return & return & weeklyStatistic & return & return & totalStatistic buttons {"Quit"} default button 1 with title appName
(*========== SUBROUTINES ============*)
on getNewRecord()
set dis to text returned of (display dialog "How many miles did you run today?" default answer "" with title appName)
set tim to text returned of (display dialog "How many minutes did that take?" default answer "" with title appName)
return {current date, dis as number, tim as number}
end getNewRecord
on writeTo(targetFile, theData, dataType, apendData) -- write to a file
try
set targetFile to targetFile as text
set openFile to open for access file targetFile with write permission
if apendData is false then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeTo
on decimalRound(theNum, sigDigits) -- round a number
try
theNum as number
sigDigits as number
set roundedNum to (round (10 ^ sigDigits * theNum) rounding as taught in school) / (10 ^ sigDigits)
on error
set roundedNum to theNum
end try
return roundedNum
end decimalRound
on dateString(d)
return ((month of d) as text) & space & day of d & ", " & year of d
end dateString