Calendar Date Prompt

Newbie needs a nudge in right direction to get a date prompt to work in Calendar. I’d like to have system ask for date, the pass into event properties.



set calendarName to "Golf"

set theSummarylist to (choose from list {"Course 1", "Course 2", "Course 3"})
if theSummarylist is false then display dialog "Select Golf Course From List" with icon stop buttons {"Exit"} default button {"Exit"}
end

--How do I make this a date picker prompt and then pass to event properties??

set startDate to " March 9, 2018 14:30:00 AM "
set endDate to startDate
set alarm to -30

set startDate to date startDate
set endDate to (date endDate) + 16200

set theCost to display dialog "Cost of round:" default answer "$10.00" buttons {"Cancel", "Continue"} default button "Continue"
--> {button returned:"Continue", text returned:"$10.00"}  

display dialog "New Golf Date: " & "@ " & theSummarylist & "on " & startDate


tell application "Calendar"
	tell (first calendar whose name is calendarName)
		make new event at end of events with properties {summary:"Golf", start date:startDate, description:"$35", location:"@ " & theSummarylist}
	end tell
end tell

This is my next attempt to get this working…No luck yet.
Need some pointers please…

error “Calendar got an error: Can’t make {button returned:"Continue", text returned:"03 09, 2018 14:00:00"} into type date.” number -1700 from {button returned:“Continue”, text returned:“03 09, 2018 14:00:00”} to date


set calendarName to "Golf"

set teetime to display dialog "Enter date in this format MM DD, yyyy hh:mm:ss" default answer "03 09 2018 13:00:00" buttons {"Continue"} default button "Continue"

set startdate to teetime

--trying to pass info returned from dialog box...
tell application "Calendar"
	tell (first calendar whose name is calendarName)
		make new event at end of events with properties {summary:"Golf", start date:teetime}
	end tell
end tell

Hi. There are two things in your list. Use the date specifier against just the text portion.

set teetime to date ((display dialog "Enter date in this format MM DD, yyyy hh:mm:ss" default answer "03 09 2018 13:00:00" buttons {"Continue"} default button "Continue")'s text returned)

That work’s.
Thanks !

ok, no more errors, but I’m missing something else. The tee time result always posts to midnight of the date I select. Am I missing how to format the input?

I tried changing the “text returned” to “date returned” but got error…

If so, how would that look?



set teetime to date ((display dialog "Enter date in this format MM DD, yyyy hh:mm:ss" default answer "03 09 2018 13:00:00" buttons {"Continue"} default button "Continue")'s text returned)

Result = date "Friday, March 9, 2018 at 12:00:00 AM"


Hello, again. There is no date returned object; the date specifier changes a properly formatted string into a constant.

display dialog "Enter date in this format MM DD, yyyy hh:mm:ss" default answer "03 09 2018 13:00:00" buttons {"Continue"} default button "Continue"

results is a list comprised of button returned and text returned.

Use 12 hour clock time:

set teetime to date ((display dialog "Enter date in this format MM DD, yyyy hh:mm:ss" default answer "03 09 2018 1:00 PM" buttons {"Continue"} default button "Continue")'s text returned)

Ok, got it working thanks for your explanation.

One more tweak…I want to add 4.5 hours to the tee time and pass the new calculated time to the end time so it blocks out 4.5 hr’s in my calendar. When I run script, new event only shows 11:00 AM to 11:00AM.


set calendarName to "Golf"

set theSummarylist to (choose from list {"Course 1", "Course 2", "Course 3"})
if theSummarylist is false then display dialog "Select Golf Course From List" with icon stop buttons {"Exit"} default button {"Exit"}

set teetime to date ((display dialog "Enter date in this format MM DD, yyyy hh:mm:ss AM or PM" default answer "03 09 2018 11:00:00 AM" buttons {"Continue"} default button "Continue")'s text returned)
end

set startdate to teetime
set endDate to startdate + 16200
set alarm to -60

set theCost to display dialog "Greens Fee:" default answer "$35.00" buttons {"Cancel", "Continue"} default button "Continue"
--> {button returned:"Continue", text returned:"$10.00"}  

display dialog "New Golf Date: " & "@ " & theSummarylist & " on " & teetime


tell application "Calendar"
	tell (first calendar whose name is calendarName)
		make new event at end of events with properties {summary:"Golf", start date:teetime, description:"$35", location:"@ " & theSummarylist}
	end tell
end tell

You aren’t using some of your variables, and your method to calculate the time range can be simplified

set calendarName to "Golf"

set theSummarylist to (choose from list {"Course 1", "Course 2", "Course 3"})
if theSummarylist is false then display dialog "Select Golf Course From List" with icon stop buttons {"Exit"} default button {"Exit"}

set teetime to date ((display dialog "Enter date in this format MM DD, yyyy hh:mm:ss AM or PM" default answer "03 09 2018 11:00:00 AM" buttons {"Continue"} default button "Continue")'s text returned)

--set startdate to teetime --unused
--set endDate to startdate + 16200 --this requires conversion factors/dimensional analysis; see 'make event,' below

set alarm to -60

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

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, location:"@ " & theSummarylist}
	end tell
end tell

Never thought of just adding math to the event…Simple and nice.
Thanks!!

Ok, I have used this script all summer, works great. Now my problem is that I have about 50 golf courses in my selection list…I want them to be sorted by name automatically, not by me updating each time I play a new course…here’s my ever growing list.

Any way to sort this list using code?

set theSummarylist to (choose from list {"Balmoral Woods ", "Big Run ", "Blackberry Oaks ", "Bliss Creek ", "Broken Arrow ", "Carillon ", "Carriage Greens ", "Cinder Ridge ", "Coyote Run ", "Devil’s Head The Glacier ", "Devil’s Head Prairie Glen ", "George W. Dunne ", "Dwight Country Club ", "Fox Bend ", "Glen Eagle ", "Glenwoodie ", "Green Garden ", "Heritage Bluffs ", "Hughes Creek ", "Inwood ", "Joliet Country Club ", "Kankakee Elks Club ", "Lawsonia Woodlands ", "Lawsonia Links ", "Lincoln Oaks ", "Mistwood ", "Myrtle Beach Indian Wells ", "Myrtle Beach Willbrook Plantation ", "Myrtle Beach National South Creek ", "Myrtle Beach Wild Wing Avocet ", "NaperBrook ", "Nettle Creek Country Club ", "Orchard Valley ", "Prairie Bluffs ", "Phillips Park ", "The Sanctuary ", "Silver Lakes ", "Settlers Hill ", "St.Andrews ", "Tamarack ", "Tana Farms ", "Thal Acres Links & Lanes ", "Tuckaway ", "Village Green ", "Wedgewood ", "Whitetail Ridge ", "Wolf Creek ", “Woodruff”

hI

try this,

borrowed the sorting routine from here:

https://macosxautomation.com/applescript/sbrt/sbrt-05.html

set the theSummarylist to {"Balmoral Woods ", "Big Run ", "Blackberry Oaks ", "Bliss Creek ", "Broken Arrow ", "Carillon ", "Carriage Greens ", "Cinder Ridge ", "Coyote Run ", "Devil's Head The Glacier ", "Devil's Head Prairie Glen ", "George W. Dunne ", "Dwight Country Club ", "Fox Bend ", "Glen Eagle ", "Glenwoodie ", "Green Garden ", "Heritage Bluffs ", "Hughes Creek ", "Inwood ", "Joliet Country Club ", "Kankakee Elks Club ", "Lawsonia Woodlands ", "Lawsonia Links ", "Lincoln Oaks ", "Mistwood ", "Myrtle Beach Indian Wells ", "Myrtle Beach Willbrook Plantation ", "Myrtle Beach National South Creek ", "Myrtle Beach Wild Wing Avocet ", "NaperBrook ", "Nettle Creek Country Club ", "Orchard Valley ", "Prairie Bluffs ", "Phillips Park ", "The Sanctuary ", "Silver Lakes ", "Settlers Hill ", "St.Andrews ", "Tamarack ", "Tana Farms ", "Thal Acres Links & Lanes ", "Tuckaway ", "Village Green ", "Wedgewood ", "Whitetail Ridge ", "Wolf Creek ", "Woodruff"}

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 calendarName to "Golf"

set theSummarylist to (choose from list {"Balmoral Woods ", "Big Run ", "Blackberry Oaks ", "Bliss Creek ", "Broken Arrow ", "Carillon ", "Carriage Greens ", "Cinder Ridge ", "Coyote Run ", "Devil's Head The Glacier ", "Devil's Head Prairie Glen ", "George W. Dunne ", "Dwight Country Club ", "Fox Bend ", "Glen Eagle ", "Glenwoodie ", "Green Garden ", "Heritage Bluffs ", "Hughes Creek ", "Inwood ", "Joliet Country Club ", "Kankakee Elks Club ", "Lawsonia Woodlands ", "Lawsonia Links ", "Lincoln Oaks ", "Mistwood ", "Myrtle Beach Indian Wells ", "Myrtle Beach Willbrook Plantation ", "Myrtle Beach National South Creek ", "Myrtle Beach Wild Wing Avocet ", "NaperBrook ", "Nettle Creek Country Club ", "Orchard Valley ", "Prairie Bluffs ", "Phillips Park ", "The Sanctuary ", "Silver Lakes ", "Settlers Hill ", "St.Andrews ", "Tamarack ", "Tana Farms ", "Thal Acres Links & Lanes ", "Tuckaway ", "Village Green ", "Wedgewood ", "Whitetail Ridge ", "Wolf Creek ", "Woodruff
 "})
if theSummarylist is false then display dialog "Select Golf Course From List" with icon stop buttons {"Exit"} default button {"Exit"}

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 "08 09 2018 11:00:00 AM" buttons {"Continue"} default button "Continue")'s text returned)

--set startdate to teetime --unused
--set endDate to startdate + 16200 --this requires conversion factors/dimensional analysis; see 'make event,' below

set alarm to -60

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

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, location:theSummarylist}
	end tell
end tell

Thanks for your help. I added your code to my existing script and it looks like there’s an issue with the sort in the Settlers Hill and Silver Lakes - Silver Lakes should be sorted before Settlers Hill…Any help would be great.
Thanks

My bad, Settlers Hill should be before Silver Lakes… E before I.

I took a few of the names out of the original list order and put them at the end of the original list and ran the code again, it sorted them into the correct order.

"Silver Lakes ", "George W. Dunne "
set the theSummarylist to {"Balmoral Woods ", "Big Run ", "Blackberry Oaks ", "Bliss Creek ", "Broken Arrow ", "Carillon ", "Carriage Greens ", "Cinder Ridge ", "Coyote Run ", "Devil's Head The Glacier ", "Devil's Head Prairie Glen ", "Dwight Country Club ", "Fox Bend ", "Glen Eagle ", "Glenwoodie ", "Green Garden ", "Heritage Bluffs ", "Hughes Creek ", "Inwood ", "Joliet Country Club ", "Kankakee Elks Club ", "Lawsonia Woodlands ", "Lawsonia Links ", "Lincoln Oaks ", "Mistwood ", "Myrtle Beach Indian Wells ", "Myrtle Beach Willbrook Plantation ", "Myrtle Beach National South Creek ", "Myrtle Beach Wild Wing Avocet ", "NaperBrook ", "Nettle Creek Country Club ", "Orchard Valley ", "Prairie Bluffs ", "Phillips Park ", "The Sanctuary ", "Settlers Hill ", "St.Andrews ", "Tamarack ", "Tana Farms ", "Thal Acres Links & Lanes ", "Tuckaway ", "Village Green ", "Wedgewood ", "Whitetail Ridge ", "Wolf Creek ", "Woodruff", "Silver Lakes ", "George W. Dunne "}

@aspaceintime

Your main code is wrong.

set calendarName to "Golf"

set theSummarylist to (choose from list {"Balmoral Woods ", "Big Run ", "Blackberry Oaks ", "Bliss Creek ", "Broken Arrow ", "Carillon ", "Carriage Greens ", "Cinder Ridge ", "Coyote Run ", "Devil's Head The Glacier ", "Devil's Head Prairie Glen ", "George W. Dunne ", "Dwight Country Club ", "Fox Bend ", "Glen Eagle ", "Glenwoodie ", "Green Garden ", "Heritage Bluffs ", "Hughes Creek ", "Inwood ", "Joliet Country Club ", "Kankakee Elks Club ", "Lawsonia Woodlands ", "Lawsonia Links ", "Lincoln Oaks ", "Mistwood ", "Myrtle Beach Indian Wells ", "Myrtle Beach Willbrook Plantation ", "Myrtle Beach National South Creek ", "Myrtle Beach Wild Wing Avocet ", "NaperBrook ", "Nettle Creek Country Club ", "Orchard Valley ", "Prairie Bluffs ", "Phillips Park ", "The Sanctuary ", "Silver Lakes ", "Settlers Hill ", "St.Andrews ", "Tamarack ", "Tana Farms ", "Thal Acres Links & Lanes ", "Tuckaway ", "Village Green ", "Wedgewood ", "Whitetail Ridge ", "Wolf Creek ", "Woodruff
 "})
# This instruction return {"Kankakee Elks Club "} or false

if theSummarylist is false then display dialog "Select Golf Course From List" with icon stop buttons {"Exit"} default button {"Exit"}
# So, when we are here, the handler sorts the single element list {"Kankakee Elks Club "}

simple_sort(the theSummarylist)

set the theSummarylist to choose from list the result

# Continue here

Try with :


set calendarName to "Golf"

set theOriginallist to {"Balmoral Woods ", "Big Run ", "Blackberry Oaks ", "Bliss Creek ", "Broken Arrow ", "Carillon ", "Carriage Greens ", "Cinder Ridge ", "Coyote Run ", "Devil's Head The Glacier ", "Devil's Head Prairie Glen ", "George W. Dunne ", "Dwight Country Club ", "Fox Bend ", "Glen Eagle ", "Glenwoodie ", "Green Garden ", "Heritage Bluffs ", "Hughes Creek ", "Inwood ", "Joliet Country Club ", "Kankakee Elks Club ", "Lawsonia Woodlands ", "Lawsonia Links ", "Lincoln Oaks ", "Mistwood ", "Myrtle Beach Indian Wells ", "Myrtle Beach Willbrook Plantation ", "Myrtle Beach National South Creek ", "Myrtle Beach Wild Wing Avocet ", "NaperBrook ", "Nettle Creek Country Club ", "Orchard Valley ", "Prairie Bluffs ", "Phillips Park ", "The Sanctuary ", "Silver Lakes ", "Settlers Hill ", "St.Andrews ", "Tamarack ", "Tana Farms ", "Thal Acres Links & Lanes ", "Tuckaway ", "Village Green ", "Wedgewood ", "Whitetail Ridge ", "Wolf Creek ", "Woodruff
 "}
simple_sort(theOriginallist)

set theSummarylist to choose from list the result

if theSummarylist is false then display dialog "Select Golf Course From List" with icon stop buttons {"Exit"} default button {"Exit"}

# Continue here

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 9 octobre 2018 11:19:09

Got it working, thank to all.:slight_smile:

So now I want to be able to peek into this calendar and return events with specific course I played. I’d like to get dates, cost and time from the data entered.

Here’s my example…

set calendarname to "Golf"
count "Carillon"

The above returns 15 items, but provides no details, just a raw number. Need a nudge in right direction…

something like this?

tell application "Calendar"
	tell calendar "Golf"
		show (first event where its summary = "Carillon")
	end tell
end tell

I found this https://macscripter.net/viewtopic.php?pid=165851#p165851
and tried to adjust to no avail. I get output, but not what I expected.

Here’s my altered code;

-- 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, theLocations} to {start date, summary} 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()

Here’s the output, but it’s missing location. I ran using AUG to OCT 2018

Wednesday, August 1, 2018
Golf

Friday, August 3, 2018
Golf

Wednesday, August 8, 2018
Golf

Thursday, August 9, 2018
Golf

Tuesday, August 14, 2018
Golf

Friday, August 17, 2018
Golf

Wednesday, August 22, 2018
Golf

Friday, August 24, 2018
Golf

Saturday, August 25, 2018
Golf

Tuesday, August 28, 2018
Golf

Friday, August 31, 2018
Golf

Friday, September 7, 2018
Golf

Saturday, September 8, 2018
Golf

Saturday, September 8, 2018
Golf

Friday, September 14, 2018
Golf

Friday, September 21, 2018
Golf

Thursday, September 27, 2018
Golf

Friday, September 28, 2018
Golf

Wednesday, October 3, 2018
Golf

Friday, October 5, 2018
Golf

Friday, October 12, 2018
Golf

How do I alter to add location to the output? I don’t understand where to put it or what field to use? And, where would one look to find this info? I tried using the dictionary, but when you don’t what what you are looking for it makes it a little difficult. Any help would be most appreciated.

This script is about 90% useful.

Tf you look at the dictionnary you would see:
event n : This class represents an event.
elements
contains attendees, display alarms, mail alarms, open file alarms, sound alarms; contained by calendars.
properties
description (text) : The events notes.
start date (date) : The event start date.
end date (date) : The event end date.
allday event (boolean) : True if the event is an all-day event
recurrence (text) : The iCalendar (RFC 2445) string describing the event recurrence, if defined
sequence (integer, r/o) : The event version.
stamp date (date) : The event modification date.
excluded dates (list of date) : The exception dates.
status (cancelled/confirmed/none/tentative) : The event status.
summary (text) : This is the event summary.
location (text) : This is the event location.
uid (text, r/o) : An unique todo key.
url (text) : The URL associated to the event.

At this time you are extracting the properties start date and summary.
To get what you want you must extract the property named location with :

tell application "Calendar" to set {theStartDates, theLocations} to {start date, location} of events of calendar "Golf"

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 11 octobre 2018 11:29:46