AppleScript计数和记录条目

时间:2012-07-18 03:46:51

标签: applescript

我对AppleScript非常陌生;我觉得我已经掌握了它,但是出于这个问题的目的,假设我不知道很多。

我正在尝试创建一个程序来记录我的日常运行。到目前为止,我已经通过以下代码显示了我跑了多长时间以及花了多长时间(我也有平均速度):

display alert "Do you know how far you ran today?" buttons {"Yes", "No"} default button "Yes"
set ans to button returned of the result
if ans is "No" then
    tell application "Safari"
        activate
        do JavaScript "window.open('maps.google.com/')"; in document 1
    end tell
end if
set dis to text returned of (display dialog "How many miles did you run today?" default answer "")
set tim to text returned of (display dialog "How many minutes did that take?" default answer "")
set speed to tim / dis
display dialog "Today you ran " & dis & " miles in " & tim & "minutes" & return & "at a " & speed & " minute mile pace."
end 

我想要的是获得一周的平均距离,时间和速度以及总距离的方法。我还希望它能够跟踪平均距离时间,速度和总距离。理想情况下,脚本会输出如下内容:

  

今天你以x分钟的速度在x分钟内跑了x英里。您最近以x分钟的速度在x分钟内平均x英里。总的来说,你跑了x分钟,以x分钟英里的速度跑x英里

我认为通过将最后一天的数据添加到平均数乘以多个条目然后将其除以条目加一,我会想到这一点,但我不知道如何做到这一点并且可能更容易方式。

1 个答案:

答案 0 :(得分:0)

这似乎是一个有趣的问题所以我写了一个脚本。您的运行记录将保存到名为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