Calendar Date Prompt

Thanks for the info…I was able to get it working. Now, I’d like to try and get the text output file records to be numbered sequentialy like this; 1), 2), 3) etc… Any guidance would be most welcomed.

-- Ask the user for the range of dates to be covered.
on getDateRange()
    set today to (current date)
    set d1 to today's short date string
    set d2 to short date string of (today + 6 * days)
    
    set dateRange to text returned of (display dialog "Enter the required date range:" default answer d1 & " - " & d2)
    set dateRangeStart to date (text from word 1 to word 3 of dateRange)
    set dateRangeEnd to date (text from word -3 to word -1 of dateRange)
    set dateRangeEnd's time to days - 1 -- Sets the last date's time to 23:59:59, the last second of the range.
    
    return {dateRangeStart, dateRangeEnd}
end getDateRange

-- Return the start dates and summaries which are in the given date range.
on filterToDateRange(theStartDates, theLocations, dateRangeStart, dateRangeEnd)
    set {eventDatesInRange, eventLocationsInRange} to {{}, {}}
    repeat with i from 1 to (count theStartDates)
        set thisStartDate to item i of theStartDates
        if (not ((thisStartDate comes before dateRangeStart) or (thisStartDate comes after dateRangeEnd))) then
            set end of eventDatesInRange to thisStartDate
            set end of eventLocationsInRange to item i of theLocations
        end if
    end repeat
    
    return {eventDatesInRange, eventLocationsInRange}
end filterToDateRange

-- Sort both the start-date and summary lists by start date.
on sortByDate(eventDatesInRange, eventLocationsInRange)
    -- A sort-customisation object for sorting the summary list in parallel with the date list.
    script custom
        property summaries : eventLocationsInRange
        
        on swap(i, j)
            tell item i of my summaries
                set item i of my summaries to item j of my summaries
                set item j of my summaries to it
            end tell
        end swap
    end script
    
    CustomBubbleSort(eventDatesInRange, 1, -1, {slave:custom})
end sortByDate

-- CustomBubbleSort from "A Dose of Sorts" by Nigel Garvey.
-- The number of items to be sorted here is likely to be small.
on CustomBubbleSort(theList, l, r, customiser)
    script o
        property comparer : me
        property slave : me
        property lst : theList
        
        on bsrt(l, r)
            set l2 to l + 1
            repeat with j from r to l2 by -1
                set a to item l of o's lst
                repeat with i from l2 to j
                    set b to item i of o's lst
                    if (comparer's isGreater(a, b)) then
                        set item (i - 1) of o's lst to b
                        set item i of o's lst to a
                        slave's swap(i - 1, i)
                    else
                        set a to b
                    end if
                end repeat
            end repeat
        end bsrt
        
        -- Default comparison and slave handlers for an ordinary sort.
        on isGreater(a, b)
            (a > b)
        end isGreater
        
        on swap(a, b)
        end swap
    end script
    
    -- Process the input parameters.
    set listLen to (count theList)
    if (listLen > 1) then
        -- Negative and/or transposed range indices.
        if (l < 0) then set l to listLen + l + 1
        if (r < 0) then set r to listLen + r + 1
        if (l > r) then set {l, r} to {r, l}
        
        -- Supplied or default customisation scripts.
        if (customiser's class is record) then set {comparer:o's comparer, slave:o's slave} to (customiser & {comparer:o, slave:o})
        
        -- Do the sort.
        o's bsrt(l, r)
    end if
    
    return -- nothing 
end CustomBubbleSort

-- Compose the text from the items in the start-date and summary lists.
on composeText(eventDatesInRange, eventLocationsInRange)
    set txt to ""
    set gap to linefeed & linefeed
    
    repeat with i from 1 to (count eventDatesInRange)
        set txt to txt & (date string of item i of eventDatesInRange) & (linefeed & item i of eventLocationsInRange & gap)
    end repeat
    
    return text 1 thru -3 of txt
end composeText

on main()
    tell application "Calendar" to set {theStartDates, theDescriptions, theLocations} to {start date, description, location} of events of calendar "Golf"
    
    set {dateRangeStart, dateRangeEnd} to getDateRange()
    set {eventDatesInRange, eventLocationsInRange} to filterToDateRange(theStartDates, theLocations, dateRangeStart, dateRangeEnd)
    sortByDate(eventDatesInRange, eventLocationsInRange)
    set txt to composeText(eventDatesInRange, eventLocationsInRange)
    
    tell application "TextEdit"
        make new document with properties {text:txt}
        activate
    end tell
end main

main()

If I understand well you may try :

on composeText(eventDatesInRange, eventLocationsInRange)
	set txt to ""
	set gap to linefeed & linefeed
	
	repeat with i from 1 to (count eventDatesInRange)
		set txt to txt & i & ") " & (date string of item i of eventDatesInRange) & (linefeed & item i of eventLocationsInRange & gap)
	end repeat
	
	return text 1 thru -3 of txt
end composeText

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 12 octobre 2018 16:28:43

Outstanding! Thanks for the help

I’d like to enhance this script but I don’t know how / where to do it. I want to add a few more rows of data to the notes section but I can’t find any examples of how to add rows to this field.

I’d like to be able to return to data from the theTargetPoints dialog and have it go to the description field, but I don’t know how to add another row to that field. Is it possible ?

There are about 5 more bits of data I’d like to add but if I can get and example of how to add one, I should be ok.

Another possibility is to “bastardize” another field that is not being used. Suggestions please…


set calendarName to "Golf"


set the theSummarylist to {"Balmoral Woods ", "Big Run "}

simple_sort(the theSummarylist)

set the theSummarylist to choose from list the result

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 teetime to date ((display dialog "Enter date in this format MM DD, yyyy hh:mm:ss AM or PM" default answer "03 30 2019 12:00:00 PM " with title "Set New Tee Time" buttons {"Cancel", "Continue"} default button "Continue")'s text returned)

set alarm to -60


set theCost to (display dialog "Greens Fee:" default answer "$35.00" with title "Enter Cost of Greens Fee" buttons {"Cancel", "Continue"} default button "Continue")'s text returned --wasn't originally in use 
--> {button returned:"Continue", text returned:"$10.00"} 

set theTargetPoints to (display dialog "Target Points:" default answer "14.00" with title "Enter Target Points" buttons {"Cancel", "Continue"} default button "Continue")'s text returned

log "New Golf Date: " & theSummarylist & " on " & teetime


tell application "Calendar"
	tell (first calendar whose name is calendarName) --assumes it exists
		make event at end with properties {summary:"Golf", start date:teetime, end date:teetime + 4.5 * hours, description:theCost, theTargetPoints:Target Points, location:theSummarylist}
	--> how do I take the info entered in thetarget points and get it into the notes/description section? I have a few more rows of data I'd like to add like Actual Points, Cost of Game, Winnings, and Score. I know how to add the dialog, but can't figure out the rest...
	
	end tell
end tell

Hi.

Concatenate theTargetPoints to theCost, with a linefeed or some other separator between them, and set the event’s description to the result.

set theNotes to theCost & linefeed & theTargetPoints

tell application "Calendar"
	tell (first calendar whose name is calendarName) --assumes it exists
		make event at end with properties {summary:"Golf", start date:teetime, end date:teetime + 4.5 * hours, description:theNotes, location:theSummarylist}
	end tell
end tell

Your variable theSummaryList does indeed contain a list — the result of the choose from list command earlier on. It happens to be accepted in this form; but since the parameter required is text, not a list, and since it’s used to set the event’s location, not its summary, it would make sense to extract the text from the list before applying it to the event property and to use a more accurate label for the variable. Note too that it’s not considered good form put handlers in the middle of the main running code. They should all be at one end of it.


set calendarName to "@Test"

set theLocationList to {"Balmoral Woods ", "Big Run "}

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.

set teetime to date ((display dialog "Enter date in this format MM DD, yyyy hh:mm:ss AM or PM" default answer "03 30 2019 12:00:00 PM " with title "Set New Tee Time" buttons {"Cancel", "Continue"} default button "Continue")'s text returned)

set alarm to -60


set theCost to (display dialog "Greens Fee:" default answer "$35.00" with title "Enter Cost of Greens Fee" buttons {"Cancel", "Continue"} default button "Continue")'s text returned --wasn't originally in use 
--> {button returned:"Continue", text returned:"$10.00"} 

set theTargetPoints to (display dialog "Target Points:" default answer "14.00" with title "Enter Target Points" buttons {"Cancel", "Continue"} default button "Continue")'s text returned

log "New Golf Date: " & theLocation & " on " & teetime

set theNotes to theCost & linefeed & theTargetPoints

tell application "Calendar"
	tell (first calendar whose name is calendarName) --assumes it exists
		make event at end with properties {summary:"Golf", start date:teetime, end date:teetime + 4.5 * hours, description:theNotes, location:theLocation}
	end tell
end tell


-- 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

Thanks for your help…works excellently!

I can now collect all the data in one place and with the help of the Calendar Export app, I can export this data to Excel & manipulate as needed. :slight_smile:

I took what I learned from the last script and attempted to make a simpler script tool to track my walking data. I used the same code and altered just a bit, but I keep getting this error…

“error “Calendar got an error: Can’t make date "Saturday, March 30, 2019 at 12:00:00 PM" into type text.” number -1700 from date “Saturday, March 30, 2019 at 12:00:00 PM” to text”

For the life of me I can’t see where my issue is…I have spent all morning trying to locate the culprit. What am I missing???:confused:


set calendarName to "Test"

set walkdata to date ((display dialog "Enter date in this format MM DD, yyyy hh:mm:ss AM or PM" default answer "03 30 2019 12:00:00 PM " with title "Set Walk Time" buttons {"Cancel", "Continue"} default button "Continue")'s text returned)


set TotalSteps to (display dialog "Total Steps:" default answer "10,000" with title "Enter Total Steps " buttons {"Cancel", "Continue"} default button "Continue")'s text returned

--set TotalDistance to (display dialog "Total Distance:" default answer "5.00" with title "Enter Total Miles Walked" buttons {"Cancel", "Continue"} default button "Continue")'s text returned

--set TotalTime to (display dialog "Total Time in Minutes:" default answer "60" with title "Enter Total Minutes " buttons {"Cancel", "Continue"} default button "Continue")'s text returned

--log "New Walk Info: " & TotalSteps & " on " & walkdata

set theNotes to walkdata

tell application "Calendar"
	tell (first calendar whose name is calendarName) --assumes it exists
		make event at end with properties {summary:"walk", start date:walkdata, end date:walkdata + 1.5 * hours, description:theNotes}
	end tell
end tell


The event’s description value has to be text. You’re setting it to theNotes, which is set to walkdata, which is set to a date.

Got it…operator error. Forgot to add theNotes and the linefeed bits.
Thanks for the nudge. Here’s the code;


set calendarName to "Test"

set walkdata to date ((display dialog "Enter date in this format MM DD, yyyy hh:mm:ss AM or PM" default answer "03 30 2019 12:00:00 PM " with title "Set Walk Time" buttons {"Cancel", "Continue"} default button "Continue")'s text returned)

set TotalSteps to (display dialog "Total Steps:" default answer "10,000" with title "Enter Total Steps " buttons {"Cancel", "Continue"} default button "Continue")'s text returned

set TotalDistance to (display dialog "Total Distance:" default answer "5.00" with title "Enter Total Miles Walked" buttons {"Cancel", "Continue"} default button "Continue")'s text returned

set TotalTime to (display dialog "Total Time in Minutes:" default answer "60" with title "Enter Total Minutes " buttons {"Cancel", "Continue"} default button "Continue")'s text returned

--log "New Walk Info: " & TotalSteps & " on " & walkdata

set theNotes to TotalSteps & linefeed & TotalDistance & linefeed & TotalTime

tell application "Calendar"
	tell (first calendar whose name is calendarName) --assumes it exists
		make event at end with properties {summary:"Walk", start date:walkdata, end date:walkdata + 1.5 * hours, description:theNotes}
	end tell
end tell


I have another question, hopefully it’s a simple fix…

How do I add a label to the text in the notes linefeed section.
Example, this code -

set theNotes to TotalSteps & linefeed & TotalDistance & linefeed & TotalTime

I want to be able to have a Label for total steps in addition to the value. This way when I look at the record in calendar, I’d see something like this:

Total Steps 10,000
Total Distance 6.2

Currently it only shows me the 10,000 and the 6.2 on separate lines, which is how I want, but would be nice to add the label.
Thanks