Help with opening a website with the current date in the url

Hey everyone, I’m completely new here and to Automator. Appreciate the help anyone can provide!

I’m trying to set up a workflow to open a website with the current date in the URL. Therefore, it will need to change the year, month, and day in the URL before opening it. For example:

Date 1: https://www.website.com/example?month=2021-11&date=2021-11-16
Date 2: https://www.website.com/example?month=2021-12&date=2021-12-23

The numbers in bold are the only parts that would have to change depending on the current date. Is this possible?

Okay, here’s how I would do it if I were forced to use the simplest Automator elements. Honestly, doing this in a scripting language (which Automator could run) tends to be quicker and less fiddly, but probably would make little if any sense at all if you were just starting out.

So here’s the workflow. Two steps. One get’s a variable, the other opens the value retrieved from the variable. As you might guess from the steps and name, the variable contains the URL to open.

All the set-up of the variable is defined outside the “flow” part of the workflow. An aspect I always found counter intuitive, but that may just be me.

I created three variables to try and help explain this.

2021-11-02-21.45.56

If I edit URL2Open, you can see it consists of the URL (size means you can only see some of it), plus the other two variables you saw above.

2021-11-02-21.48.15

I dragged these in from the list above when I needed to use them.

They just define the two sets of date data in the formats you specified.

Month ISO

2021-11-02-21.49.36

Today ISO

2021-11-02-21.50.09

To create these, you type in the hyphens and you drag the component parts in the window into the field. But, you may notice that the month is a word, yet I have a number in the field. Note the little drop down marker on the right of the month token in the value field. If you click on it to expand it, you get to choose a different format.

2021-11-02-21.51.24

For completeness, here’s a workflow that does the same thing using a single script line. It works in just the same sort of way, and now I’ve been through the workflow above, it might give you a bit of insight to what this is doing. The algorithm is practically identical. The script is a lot faster to create and edit (Shortcuts is great, but Automator’s dragging of variable tokens I find incredibly painful in comparison), but your starting point is effectively the proverbial blank page, and you have to know how to get there, whereas Automator’s prebuilt actions are giving you everything all in.

If you are running, or planning to move to Monterey (the new version of macOS), the Shortcuts app is available on that OS, and is intended to be Automator’s successor on the Mac. That would be worth you exploring. It’s a more recent creation, but builds strongly on Automator’s approach. It has received a lot more attention than Automator and as a result, I suspect most people find it easier to use. Certainly building this workflow would be almost as fast as writing a line of script … though like Automator, you could even run script from it.

2021-11-02-22.13.04

Here the use of current date (a standard variable) get’s the same formatting effect as in the above options. It opens the page, and you can see the URL that was passed through shown after the Open URLs action. You could put the content of the text action into the Open URL step to make it a one step shortcut … except when I tried that it failed (though it worked on iOS and iPadOS) - it is still very new on the Mac and there are still some rough edges.

I hope the above helps.

2 Likes

Wow, this has to be the most thorough response I’ve ever received. Thank you for all this. I ended up using the Shell Script workflow. I got shortcuts to work but they can’t be saved as a file to run or automated on specific days as far as I could tell.

How can I adjust the Shell Script to add a certain number of days to "+%Y-%m-%d"? For example, if I wanted to add 5 days to the date of the URL:

open https://www.website.com/example\?month=$(date "+%Y-%m")\&date=$(date "+%Y-%m-%d+5")

We were having a discussion about how you can do that just the other day.

Here is an example that will just output the result of that date calculation. Try it, then you should be able to use it to update your URL string.

echo $(date -v +5d "+%Y-%m-%d")

2021-11-03-06.55.33

A useful command to know is man. If you type man date at the command prompt it will give you access to the documentation for the date command. -v is one of the options for this command in Z-shell and it allows you to do basic date arithmetic. man is a fantastic starting point for whenever you are using a command line instruction or utility - all the documentation is built right in; though an Internet search can often bring up the output of man too.

1 Like

I’m sure they’ll add automation (scheduling) for Shortcuts in a future update because you can automate on iOS, I believe.

Thank you so much for all the help and the tip! I was able to automate exactly what I was trying to do. It took a bit of tinkering, but it was fun figuring out what worked and what didn’t.

@sylumer how can I include a delay within the Shell Script to delay the next action until the webpage loads?

Thanks!

That happens in the browser, so it isn’t returning anything about page loads to the shell. You would either want to delay for a specific period of time

E.g. Wait for 10 seconds.

sleep 10

https://ss64.com/bash/sleep.html

If you want more control, you should look at something that can support shell scripting and working with things like browsers - Keyboard Maestro would be a good choice on macOS. You could then run a shell script, monitor the browser, then run another script.

a word of caution, different versions of date have slightly different syntaxes for adding days to the current date, so be sure you know when you are using BSD date (Mac, *BSD) and when you are using gnu date (linux or homebrew).

That sort of thing is why use of the manual (man for shell commands) and testing are key skills for automators.