0

我对 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 英里

我认为可以通过将最后一天的数据添加到平均数乘以许多条目然后将其除以条目加一来解决此问题,但我不知道如何做到这一点,并且可能有一种更简单的方法。

4

1 回答 1

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
于 2012-07-21T21:02:26.553 回答