0

私はAppleScriptに非常に慣れていません。私はそれのコツをつかんでいるように感じます、しかしこの質問の目的のために、私が多くを知らないと仮定します。

毎日のランニングを記録するプログラムを作ろうとしています。これまでのところ、次のコードを使用して、1日の走行距離と所要時間(平均速度もあります)を表示することができます。

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を足したもので割ることでそれを実現できると思いますが、それを行う方法がわからないので、おそらくもっと簡単な方法があります。

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 に答える