I have been using the script below for a few years and I want to enhance it but I’m not sure if its possible and if it is, not sure how.
Anyway, here’s what I want it to do; I use this script to log my miles and steps on a daily basis. I’d like to able to compute a running total for steps and miles as separate line items in the comments section.
In addition, I want to have these totals calculated based on a date that can be edited. The reason for this is my running shoes only last about 5 months and I’d like to be able to see on a daily basis the total miles and total steps since I put the new shoes into service. My present shoes went into service on Feb 15 2020. You will have to enter some test data in order to compute these items. Any help would be most appreciated…
--Edited summary on 10-19 to "Walk Data"
--Started to track weight in the script on 10-17-2019.
--Changed default time to 8:30 AM on 10-15/19
--Edited on 09/03/19, removed calorie entry
--Edited on 04/01/19, added line for comments and removed total time and replaced with calories
--Created on 03/31/19
set calendarName to "Run"
set walkdata to date ((display dialog "Enter date in this format MM DD, yyyy hh:mm:ss AM or PM" default answer "4 20 2020 8:30:00 AM " with title "Set Walk Time" buttons {"Cancel", "Continue"} default button "Continue")'s text returned)
set TotalSteps to (display dialog "Total Steps Taken:" default answer "10,000" with title "Enter Total Steps " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
set TotalDistance to (display dialog "Total Miles Walked:" default answer "5.00" with title "Enter Total Miles Walked" buttons {"Cancel", "Continue"} default button "Continue")'s text returned
set ActualWeight to (display dialog "Today's Weight:" default answer "205" with title "Enter Todays Weight " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
set Comments to (display dialog "Comments For Today:" default answer " " with title "Enter Comments " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
log "Walk Entry: " & TotalSteps & " steps " & "and walked " & TotalDistance & " miles" & " on " & walkdata
set theNotes to TotalSteps & linefeed & TotalDistance & linefeed & ActualWeight & linefeed & Comments & linefeed
tell application "Calendar"
tell (first calendar whose name is calendarName) --assumes it exists
make event at end with properties {summary:"Walk Data", start date:walkdata, end date:walkdata, description:theNotes}
end tell
end tell
As is, your script works but it would be better if you used Shane Stanley’s free “Display Toolkit”.
With it you would be able to have a single dialog with 5 text fields and two buttons.
To achieve your goal, I would insert the datas in Calendar as you do but I would also insert them in a spreadsheet which is the tool of choice for calculations.
Numbers would be a good candidate for that.
I built a quick and dirty draft:
set currDate to current date
set walkDate to date string of currDate & space & time string of currDate
set walkData to (do shell script "date +%m %d, %Y %H:%M:%S") -- CAUTION, use NOBREAK spaces
set TotalSteps to 10000
set TotalDistance to 5
set ActualWeight to 205
set Comments to "pas fameux"
set values to {walkDate, TotalSteps, TotalDistance, ActualWeight, Comments}
try
-- The date with yourvformat is rejected by Calendar in French
set calendarName to "Sports"
set calValues to {walkData} & rest of values
set theNotes to my recolle(calValues, linefeed)
tell application "Calendar"
tell (first calendar whose name is calendarName) --assumes it exists
make event at end with properties {summary:"Walk Data", start date:walkData, end date:walkData, description:theNotes}
end tell
end tell
end try
tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
repeat with r from 1 to count rows
set maybe to value of cell 1 of row r
if maybe is in {missing value, ""} then
tell row r
repeat with c from 1 to count values
set value of cell c to item c of values
end repeat
end tell
exit repeat
end if
end repeat
if r = (count rows) then add row below row r
end tell
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 20 avril 2020 21:27:41
having the steps stored in the description of event, you can tell to calendar “Run” to calculate total steps between 2 indicated dates. No, I have to go at work. I will try to provide more help tomorrow.
Here, I fixed some little mistakes in the OP’s script, and added the calculating total steps and total distance functionality:
set calendarName to "Run"
set fromDate to date ((display dialog "Enter From Date in format MM DD, yyyy hh:mm:ss AM or PM" default answer "2 15 2020 8:30:00 AM " with title "Set Walk Time" buttons {"Cancel", "Continue"} default button "Continue")'s text returned)
set walkData to date ((display dialog "Enter Walk Date in format MM DD, yyyy hh:mm:ss AM or PM" default answer "4 20 2020 8:30:00 AM " with title "Set Walk Time" buttons {"Cancel", "Continue"} default button "Continue")'s text returned)
set Steps to (display dialog "Steps Taken:" default answer "10000" with title "Enter Steps " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
set Distance to (display dialog "TMiles Walked:" default answer "5,00" with title "Enter Miles Walked" buttons {"Cancel", "Continue"} default button "Continue")'s text returned
set ActualWeight to (display dialog "Today's Weight:" default answer "205" with title "Enter Todays Weight " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
set Comments to (display dialog "Comments For Today:" default answer " " with title "Enter Comments " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
log "Walk Entry: " & Steps & " steps " & "and walked " & Distance & " miles" & " on " & walkData
tell application "Calendar"
set chosenEvents to every event of calendar "Run" whose (start date ≥ fromDate) and (start date ≤ walkData) and (description begins with "Steps: ")
set TotalSteps to Steps as integer
set TotalDistance to Distance as real
repeat with theEvent in chosenEvents
set TotalSteps to TotalSteps + (text 8 thru -1 of (paragraph 1 of (get description of theEvent))) as integer
set TotalDistance to TotalDistance + (text 11 thru -1 of (paragraph 2 of (get description of theEvent))) as real
end repeat
end tell
set TotalSteps to TotalSteps as text
set TotalDistance to TotalDistance as text
set theNotes to "Steps: " & Steps & linefeed & "Distance: " & Distance & linefeed & "Actual weight: " & ActualWeight & linefeed & linefeed & "Total Steps: " & TotalSteps & linefeed & "Total Distance: " & TotalDistance & linefeed & Comments
tell application "Calendar" to tell calendar calendarName --assumes it exists
make new event at the end of the events with properties {summary:"Walk Data", start date:walkData, end date:walkData, description:theNotes}
end tell
I appreciate the help.
I ran the script a few times to enter test data and it works somewhat.
What I’d like it to do is to calculate a running total of steps from 02-15-2020 and put those values in the Total Steps & Total Distance section.
Example: New shoes bought on 02-15-2020, I’d like to see total steps & total distance in the Totals section since 02-15-2020. After each day’s entry the Total Steps & Total Distance should reflect cumulative steps & miles since 02-15-2020.
I fixed some mistakes in my last script. So, try this new update. Now it should calculate total steps and the distance correctly.
Here is 1 serious problem with calculating from 15 febrary 2020.
You have since today entered only numbers. And, from today it is keyword: number.
ApplesScript doesn’t take numbers 10,000 as integer. They should be 10000 or 10 000.
the real numbers in the AppleScript has syntax like 12,7 and not 12.7 as you have at old entries.
How to fix this problem? I sugess you to calculate now manually total steps and total distance and to enter manually to today’s entry(20 April 2020). Then enter for fromDate 20 April 2020 instead of 15 Febrary 2020. Because your old entries is problematic for AppleScript.
After doing that, beginning from today all calculations will work correctly.
NOTE: As I see, after I fixed the syntax for Steps, you continue to use 21,561 syntax which is problematic for AppleScript. You should understand this to be able to calculate in the AppleScript. Or, don’t calculate at all…
One more time you forget that the behavior of Applescript is localization dependent.
The real numbers always use the period as decimal separator in Applescriptas long as we speak of NUMBERS.
The comma appears only if we coerce the real values as strings when the operating system use this decimal separator.
Look at one of my favorite piece of code
set {delim, deci} to my get2LocalizedDelimiters()
#=====
(*
Set the parameter delimiters which must be used in Numbers formulas
*)
on get2LocalizedDelimiters()
if text item 2 of (0.5 as string) is "." then
return {",", "."}
else
return {";", ","}
end if
end get2LocalizedDelimiters
#=====
As you see, I pass a real value : 0.5 which I coerce as string.
On my machine using French settings, the extracted text item is a comma but it’s a digit on a system using English settings.
This is why, when we work with French settings, the components of of spreadsheet formulas are separated by semi colon when they are separated by comma on systems applying English settings.
On a machine running with French settings, set x to “10,000” as integer returns 10 because here the comma is the decimal separator
I have to time to test that now but if my memory is right, on a machine running with English settings (at least the US English ones), set x to “10,000” as integer returns 10000 because in such case the comma is the thousands separator.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 21 avril 2020 16:57:39
Exactly. I run English settings and it returns 10. That is the problem with old OP’s entry’s.
In any case, the entries in the OP’s calendar would be nice to put in order. Simple enumeration of numbers, without field names, and even obscure AppleScript numbers leads to problems, now or in the future. Therefore, I proposed to fix it. At least starting from the current date.
I would like somebody to explain this bit of code…I don’t follow the text 8 thru -1 of (paragraph 1
set TotalSteps to TotalSteps + (text 8 thru -1 of (paragraph 1 of (get description of theEvent))) as integer
set TotalDistance to TotalDistance + (text 11 thru -1 of (paragraph 2 of (get description of theEvent))) as real
I’m trying to take this concept and apply it to my golf tracker that this forum has helped me enhance. I will post my code as I get closer but I’m not understanding…where did the text 8 come from and what does it refer to?
Same for text 11…How would I add more lines to add more items?
My plan is to enhance my Golf Tracker to be able to track par’s, birdies, bogeys, how much money I spent on greens fees, how much I won in the same manner that this script tracks my walk data.
As you might expect, (get description of theEvent) gets the ‘description’ of an event in Calendar. That’s the text of the event’s Notes, which you see when you open the event. paragraph 1 is of course the first paragraph of that text, assuming that the text isn’t just an empty string (“”).
text 8 thru -1 is the section of the paragraph from the eighth character to the last. (Negative indices number from the end of a container instead of from the beginning.)
The two lines of code …
set TotalSteps to TotalSteps + (text 8 thru -1 of (paragraph 1 of (get description of theEvent))) as integer
set TotalDistance to TotalDistance + (text 11 thru -1 of (paragraph 2 of (get description of theEvent))) as real
… are written on the assumption that the ‘description’ of every event examined will have at least two paragraphs and that the first paragraph will be seven characters followed by text representing an integer number and that the second will be ten characters followed by text representing a fractional number. The numeric texts are coerced to the equivalent AppleScript number classes and the results are added to the contents of existing variables.
Since the event’s description text has to be obtained from Calendar, it would be more efficient to get it just once and use it twice than to get it both times:
set theDescription to description of theEvent
set TotalSteps to TotalSteps + (text 8 thru -1 of (paragraph 1 of theDescription)) as integer
set TotalDistance to TotalDistance + (text 11 thru -1 of (paragraph 2 of theDescription)) as real
I cobbled together the following code and I’m successful on the first run. When I attempt to run a second time I get this error and I don’t get why it’s happening. An explanation would be helpful…
Error:
Can’t make text 8 thru -1 of paragraph 1 of "Pars: 1
Birdies: 1
Bogeys: 1
Total Pars: 1
Total Birdies: 1
Total Bogeys: 1
all 1’s" into type number.
set calendarName to "Test Golf"
set fromDate to date ((display dialog "Golf Start Date in format MM DD, yyyy hh:mm:ss AM or PM" default answer "5 1 2020 8:30:00 AM " with title "Set Walk Time" buttons {"Cancel", "Continue"} default button "Continue")'s text returned)
set golfData to date ((display dialog "Golf Date in format MM DD, yyyy hh:mm:ss AM or PM" default answer "8 27 2020 8:30:00 AM " with title "Set Walk Time" buttons {"Cancel", "Continue"} default button "Continue")'s text returned)
set Pars to (display dialog "Pars This Round:" default answer "1" with title "How Many Pars " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
set Birdies to (display dialog "Birdies This Round:" default answer "1" with title "How Many Birdies " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
set Bogeys to (display dialog "Bogeys This Round:" default answer "2" with title "How Many Bogeys " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
set Comments to (display dialog "Comments For Today:" default answer " " with title "Enter Comments " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
tell application "Calendar"
set chosenEvents to every event of calendar "Test Golf" whose (start date ≥ fromDate) and (start date ≤ golfData) and (description begins with "Pars: ")
set TotalPars to Pars as integer
set TotalBirdies to Birdies as integer
set TotalBogeys to Bogeys as integer
repeat with theEvent in chosenEvents
set theDescription to description of theEvent
set TotalPars to TotalPars + (text 8 thru -1 of (paragraph 1 of (get description of theEvent))) as real
set TotalBirdies to TotalBirdies + (text 11 thru -1 of (paragraph 2 of (get description of theEvent))) as real
set TotalBogeys to TotalBogeys + (text 11 thru -1 of (paragraph 3 of (get description of theEvent))) as real
end repeat
end tell
set TotalPars to TotalPars as text
set TotalBirdies to TotalBirdies as text
set TotalBogeys to TotalBogeys as text
set theNotes to "Pars: " & Pars & linefeed & "Birdies: " & Birdies & linefeed & "Bogeys: " & Bogeys & linefeed & linefeed & "Total Pars: " & TotalPars & linefeed & "Total Birdies: " & TotalBirdies & linefeed & "Total Bogeys: " & TotalBogeys & linefeed & Comments
tell application "Calendar" to tell calendar calendarName --assumes it exists
make new event at the end of the events with properties {summary:"Golf Data", start date:golfData, end date:golfData, description:theNotes}
end tell
Paragraph 1 of your text is “Pars: 1”, which only contains seven characters. So trying to get the text starting at the eighth character produces an error. Similarly with the second and third paragraphs. They only contain ten characters each and your code attempts to start at the eleventh. Your repeat should look something like this:
repeat with theEvent in chosenEvents
set theDescription to description of theEvent
set TotalPars to TotalPars + (text 7 thru -1 of (paragraph 1 of theDescription)) as real
set TotalBirdies to TotalBirdies + (text 10 thru -1 of (paragraph 2 of theDescription)) as real
set TotalBogeys to TotalBogeys + (text 10 thru -1 of (paragraph 3 of theDewscription)) as real
end repeat
This assumes of course that the text has been entered into Calendar the same way every time — ie. with exactly the same number of characters before the numbers. If there are likely to be variations, a more reliable way of getting the numbers will be needed. Getting the last ‘word’ of each paragraph could be one way if the numbers always come at the ends of the lines:
repeat with theEvent in chosenEvents
set theDescription to description of theEvent
set TotalPars to TotalPars + (word -1 of (paragraph 1 of theDescription)) as real
set TotalBirdies to TotalBirdies + (word -1 of (paragraph 2 of theDescription)) as real
set TotalBogeys to TotalBogeys + (word -1 of (paragraph 3 of theDewscription)) as real
end repeat
I have got this script working, but I have a few questions;
1)Where do the paragraph numbers start. I got messed up between paragraph 6 & 7. See my comments where this $$ is
Any suggestions for performance improvements?
--making progress 09/05/20
--got some of it working now...more work to do to add the rest of my golf tracker info
--test par bogey birdie tracker 09/02/2020
set calendarName to "Test Golf"
set theLocationList to {"Augusta, Pebble Beach, St. Andrews"}
set theLocationList to simple_sort(theLocationList)
set locationChoice to (choose from list theLocationList) -- Returns a list — or 'false' if the "Cancel" button's clicked.
if (locationChoice is false) then error number -128 -- Explicit "Cancel" error.
set theLocation to item 1 of locationChoice -- Otherwise get the single item of text from the list.
-- Handler(s).
on simple_sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end simple_sort
set fromDate to date ((display dialog "Golf Start Date in format MM DD, yyyy hh:mm:ss AM or PM" default answer "5 1 2020 8:30:00 AM " with title "Set Tee Time" buttons {"Cancel", "Continue"} default button "Continue")'s text returned)
set golfData to date ((display dialog "Golf Date in format MM DD, yyyy hh:mm:ss AM or PM" default answer "8 27 2020 12:00:00 PM " with title "Set Tee Time" buttons {"Cancel", "Continue"} default button "Continue")'s text returned)
--This sets the counter up from the first round played in a given timeframe...
set Pars to (display dialog "Pars Today:" default answer "1" with title "How Many Pars Today? " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
--paragraph 1
set Birdies to (display dialog "Birdies Today:" default answer "1" with title "How Many Birdies " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
--paragraph 2
set Bogeys to (display dialog "Bogeys Today:" default answer "1" with title "How Many Bogeys " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
--paragraph 3
set DoubleBogeys to (display dialog "Double Bogeys Today:" default answer "1" with title "How Many Double Bogeys " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
--paragraph 4 added 09/05/20
set GreensFees to (display dialog "Greens Fee:" default answer "35" with title "Enter Greens Fee" buttons {"Cancel", "Continue"} default button "Continue")'s text returned
--> {new code button returned:"Continue", text returned:"$10.00"}
--paragraph 5 added 09/05/20
set CostofGame to (display dialog "Cost of Game:" default answer "10" with title "Enter Cost of Game" buttons {"Cancel", "Continue"} default button "Continue")'s text returned
--$$$$ paragraph 7? added 09/05/20 I thought this should be paragraph 6, but if I use it, the row totals are off by 1...any thoughts?
set Winnings to (display dialog "How Much Did I Win:" default answer "10" with title "How Much Did I Win" buttons {"Cancel", "Continue"} default button "Continue")'s text returned
set theScore to (display dialog "Today's Score:" default answer "92" with title "Enter Score " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
--paragraph added 09/05/20
set TargetPoints to (display dialog "Target Points:" default answer "17" with title "Enter Target Points" buttons {"Cancel", "Continue"} default button "Continue")'s text returned
--paragraph added on 09/04/20
set ActualPoints to (display dialog "Actual Points:" default answer "17" with title "Enter Actual Points" buttons {"Cancel", "Continue"} default button "Continue")'s text returned
--paragraph added on 09/04/2020
set Comments to (display dialog "Comments For Today:" default answer " " with title "Enter Comments " buttons {"Cancel", "Continue"} default button "Continue")'s text returned
tell application "Calendar"
set chosenEvents to every event of calendar "Test Golf" whose (start date ≥ fromDate) and (start date ≤ golfData) and (description begins with "Pars Today: ")
set TotalPars to Pars as integer
set TotalBirdies to Birdies as integer
set TotalBogeys to Bogeys as integer
set TotalDoubleBogeys to DoubleBogeys as integer --added 09/05/20
set TotalGreensFees to GreensFees as integer ----added 09/05/20
set TotalCostofGame to CostofGame as integer ----added 09/05/20
set TotalWinnings to Winnings as integer ---added 09/05/20
repeat with theEvent in chosenEvents
set theDescription to description of theEvent --new code 09-02-2020
set TotalPars to TotalPars + (word -1 of (paragraph 1 of theDescription)) as integer
set TotalBirdies to TotalBirdies + (word -1 of (paragraph 2 of theDescription)) as integer
set TotalBogeys to TotalBogeys + (word -1 of (paragraph 3 of theDescription)) as integer
set TotalDoubleBogeys to TotalDoubleBogeys + (word -1 of (paragraph 4 of theDescription)) as integer --added 09/05/20
set TotalGreensFees to TotalGreensFees + (word -1 of (paragraph 5 of theDescription)) as integer ----added 09/05/20
set TotalCostofGame to TotalCostofGame + (word -1 of (paragraph 7 of theDescription)) as integer ----added 09/05/20 I thought this was paragraph 6 but when I used it, my totals were off by a row???
set TotalWinnings to TotalWinnings + (word -1 of (paragraph 8 of theDescription)) as integer ----added 09/05/20
end repeat
end tell
set TotalPars to TotalPars as text
set TotalBirdies to TotalBirdies as text
set TotalBogeys to TotalBogeys as text
set TotalDoubleBogeys to TotalDoubleBogeys as text --added 09/05/20
set TotalGreensFees to TotalGreensFees as text ---added 09/05/20
set TotalCostofGame to TotalCostofGame as integer ----added 09/05/20
set TotalWinnings to TotalWinnings as integer ---added 09/05/20
set theNotes to "Pars Today: " & Pars & linefeed & "Birdies Today: " & Birdies & linefeed & "Bogeys Today: " & Bogeys & linefeed & "Double Bogeys Today: " & DoubleBogeys & linefeed & "Greens Fee: $" & GreensFees & linefeed & "Today's Round: " & theScore & linefeed & "Cost of Game: $" & CostofGame & linefeed & "Today's Winnings: $" & Winnings & linefeed & linefeed & "Total Pars: " & TotalPars & linefeed & "Total Birdies: " & TotalBirdies & linefeed & "Total Bogeys: " & TotalBogeys & linefeed & "Total Double Bogeys: " & TotalDoubleBogeys & linefeed & "Total Greens Fees: $" & TotalGreensFees & linefeed & "Total Cost of Game: $" & TotalCostofGame & linefeed & "Toal Winnings: $" & TotalWinnings & linefeed & linefeed & linefeed & "Target Points: " & TargetPoints & linefeed & "Actual Points: " & ActualPoints & linefeed & linefeed & Comments
tell application "Calendar" to tell calendar calendarName --assumes it exists
make new event at the end of the events with properties {summary:"Golf Data", start date:golfData, end date:golfData, location:theLocation, description:theNotes}
end tell
Without seeing your description texts, it’s impossible to tell. One guess is that you may not be aware that an AppleScript ‘paragraph’ is any section of text that’s followed by a ‘linefeed’ or ‘return’ character or by the end of the text. So if you have an empty line between two literary paragraphs, that line counts as a ‘paragraph’ in its own right: