Schedule opening location of a URL via Calendar Alert

I am attempting to set up a calendar event that will send a message to a Url at a defined time in the future.

I would like to

  1. make a new calendar event listing a youtube URL
  2. have that calendar event open an applescript script file at its alert time
  3. pass the events’ listed URL to the script as a parameter
  4. have the applescript open the youtube URL location to play music

I wanted to make the script universal, so that the url and time of the calendar event could be changed, but without changing the applescript.

When the event at the alert time opened the script file, the designated You tube URL location would be opened and the Youtube contents would be played.

If you know a method to pass that URL to an applescript as a parameter, please help.

Thanks.

Hi akim.

Unfortunately, ‘open file alarms’ don’t pass parameters to the files they open. The best a file can do (if it’s a script applet!) to find out which Calendar event triggered it is to get the current date/time from the system and then search Calendar’s events for any having ‘open file alarms’ due to trigger at about that time. It’s not a very satisfactory process, especially if there are a lot of events in your calendars.

I did write such a script nearly six years ago, when Calendar was called iCal, but I don’t know if it still works. You’d have to change “iCal” to “Calendar” and it would probably go faster if you could limit the search to a particular calendar. You also have to write code to extract the information you want from the candidate event(s).

Nigel,
Thanks for your ideas. I tried that approach but it was too slow. I tried another approach using an Automator work flow.

I made a calendar with name “Automator”.
I then made a Calendar event with an alert to open the Automator Calendar Alarm and to list the target URL. I named the event “Open Location”

I wrote an Automator Calendar Alarm that gets the specified calendar item from Calendar Automator when the automator application file is opened by the Calendar Alert of that event.

Then the Automator work flow pipes the information to Filter Calendar Events in which all events are filtered when all of the following are true:
Any content contains the name of the calendar event. For this example I called the calendar event “Open Location”
Any date starting is today.

The data is then piped to Event Summary.

Finally the data is piped to Run AppleScript

The Applescript is


on run {input, parameters}
	
	set anItem to item 1 of input
	set AppleScript's text item delimiters to "URL:" & tab
	set URL_line_with_return to text item 2 of anItem
	set target_URL to text 1 thru -2 of URL_line_with_return
	set AppleScript's text item delimiters to ""
	open location target_URL
	
end run

I thought that method could be used to open a target URL at a future time.

I would enjoy your input on my attempt and any method to improve it. I would it easier to show the entire process if could show the Automator work flow, but I do not know how to do so on this forum.

Hi Akim.

It will certainly speed things up if it’s known in advance that the triggering event is in a certain calendar and has a certain name (‘summary’ in AppleScript; probably what’s meant by ‘Title’ in the Automator action) and that the trigger window is as wide as a day. This is true even in AppleScript, because it’s not then necessary to look at the ‘open file alarms’ at all.

I’m not all that au fait with Automator myself and can’t tell if it would be faster or not than a script app doing the same thing when there are a lot of similar events in the calendar. But it does seem to have the advantage that Calendar itself doesn’t need to be open at the time. The workflow you’ve described is easy to set up and appears to work ” although the question mark in the YouTube URL I tried got changed to “%3F” somewhere along the way. :confused:

In the AppleScript part of the workflow, I’d suggest setting target_URL to paragraph 1 of URL_line_with_return rather than text 1 thru -2 thereof. This will work even if there are additional entries after the URL line.

Nigel,
Thanks for your ideas.

I named the Automator workflow Open URL from Calendar.app. It can be viewed at:

https://drive.google.com/file/d/0B-te8OIv5aLJVWU1ZVIzTGJfbUk/view?usp=sharing.

That workflow and its applescript are triggered by a calendar event named Open Location in a calendar named Automator. It can be viewed at:

https://drive.google.com/file/d/0B-te8OIv5aLJbmZ5YmVsYTROOWc/view?usp=sharing

The Open Location event has alert options set as shown in the following image:

https://drive.google.com/file/d/0B-te8OIv5aLJZjl6OTgxRGZqT0E/view?usp=sharing

By sharing the images of the workflow, I thought that my current script might be easier to explain.

My overall goal, however is to replicate the workflow in Applescript and avoid Automator. I would appreciate any ideas on writing the functions in Applescript.

Hi akim.

Yes. Those are exactly the workflow and event detail I set up myself based on your description, except that I used a YouTube URL as mentioned in your first post and I changed the line in the AppleScript code as per my suggestion in post #4. The set-up works (ie. Safari opens the URL at the event’s start time), but my test URL “https://www.youtube.com/watch?v=DWzleOtDv30” is rendered as “https://www.youtube.com/watch%3Fv=DWzleOtDv30” in the “Event Summary” result and so isn’t recognised by the YouTube server. In practice, the “Run AppleScript” code has to correct this before applying ‘open location’.

The workflow can be replicated using vanilla and Calendar AppleScripting as below. It has the advantage that the time range for the event filter can be made narrower than the Automator action allows, so you’re not limited to one URL per day. On the other hand, since it involves scripting the Calendar application, even though minimally, it’s slower. And if Calendar isn’t open when the script triggers, the time taken to open it also adds slightly to the delay.

I imagine some ASObjC code would, like the Automator actions, be able to extract the required information without using Calendar itself. However, that’s beyond my current competence and time to research. :expressionless:

-- This assumes that the relevant open file alarms are set to trigger at the events' start times.

on openURLfromCalendar()
	script eventsData
		property summaries : missing value
		property startDates : missing value
		property urls : missing value
	end script
	
	set calendarName to "Automator"
	set relevantSummary to "Open location"
	
	set now to (current date)
	-- Allow, say, 5 seconds since the trigger for the script to have started up and reached this point.
	set windowStart to now - 5
	
	-- Dump the relevant data from all the events in the calendar.
	tell application "Calendar"
		set {eventsData's summaries, eventsData's startDates, eventsData's urls} to {summary, start date, url} of events of calendar calendarName
	end tell
	
	-- Parse the data for an event named "Open location" whose start date is within a few seconds of the current time.
	repeat with i from 1 to (count eventsData's summaries)
		if (item i of eventsData's summaries is relevantSummary) then
			-- If an item in the summary list is "Open location", get the equivalent item from the start date list and check it out.
			set thisStartDate to (item i of eventsData's startDates)
			if ((thisStartDate ≥ windowStart) and (thisStartDate ≤ now)) then
				-- If the start date suggests this is the event which triggered the script, get the equivalent item from the URL list.
				set theURL to item i of eventsData's urls
				-- Correct any pecularities in the URL and open it.
				processAndOpen(theURL)
				-- No need to check any more data.
				exit repeat
			end if
		end if
	end repeat
end openURLfromCalendar

on processAndOpen(theURL)
	set astid to AppleScript's text item delimiters
	-- Question marks in event URLs are returned as percent codes. Correct this.
	set AppleScript's text item delimiters to "%3F"
	set TIs to theURL's text items
	set AppleScript's text item delimiters to "?"
	set theURL to TIs as text
	set AppleScript's text item delimiters to astid
	
	open location theURL
end processAndOpen

openURLfromCalendar()

Nigel,

Thanks for demonstrating your method in Applescript. I like that your script narrows the start time to a five second interval, rather than the entire day like the one I submitted, and that it does not rely on Automator.

I also appreciate the issues you raised as to converting URLs in Applescript, and your use of the paragraph property rather than text 1 through -2 to avoid capturing the paragraph return.

I find , however, that Applescript wants to parse through all of a specific calendar’s events, which can occupy a significant amount of time. I guess that delay depends accessing the calendar’s events, calling on some underlying framework or element, and upon parsing through a large number of events contained in a certain calendar.

That is where I found Automator less time consuming to perform. To find a more efficient method, I will pursue your advice to write this script using ASObj and the Event Kit framework.

Thanks for all of your help.