Updated on 2021-04-03: An updated version of the script is described in this post.
Glacier National Park is really beautiful! The website has a dozen of webcams in different parts of the park and winter views are amazing (from the working cameras). All the webcams with their descriptions are on this page, but I like the current thumbnails page more because it provides a compact view of the current webcam images. Sometimes I want to save a picture, but I have to manually change the filename or append a suffix so that previous images from the same webcam aren’t overwritten — this is annoying. How could we automate this?
Opening the image
I use the great
Tridactyl Firefox extension so that I have some more vim-like navigation (
vimperator was even more powerful). The
vimperator-like hint mode is on in
:tutor in Firefox to see what it means):
1 2 3
So on the webcams page, to open an image I can type
;i and then a number of the image.
Saving the image
Tridactyl has a native messenger that allows it to communicate with the local host (beware of the security implications), e.g. to be able to
~/.tridactylrc file. Well, it can also run arbitrary shell commands (
:h exclaim), which is exactly what we need.
I’ve written a
zsh script that uses z-shell’s advanced expressions to figure out the next filename (this is the original version; the updated version is here):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
URL is the parameter.
BASENAME is the base filename, say
LATEST_FILE is the newest file with this basename, say
smptz031.jpg. The magic in
PADDED_NEW_NUMBER extracts the number by removing the length of the basename and removing the extension, increments it by one, and pads it to three decimal places. Then
curl downloads the image by the
URL and saves to the correct filename. You can
man zshexpn for all the details on the variable expansions.
Note: I haven’t tested this script in the case when you don’t have any of these images saved yet, it’ll likely fail. You can manually save the first image by appending
001 to the filename.
This is the line I added to my
Now I can type
zs on any of the opened webcam images (all of them are hosted on
https://www.nps.gov/webcams-glac/), which will pass the current URL to the shell script.
Organizing the existing filenames
Initially I had been saving the images, appending a suffix manually. I wanted the names to be consistent with the script above. To rename them, I used
zsh to append a counter to the base filename:
1 2 3
and so on (this could be generalized for all the base names, but wasn’t practical for one time).
zmv is a complicated tool, there are useful examples in the gist.
I’m happy with the solution overall, the friction to save a beautiful image is much lower now.
Of course, visiting the park in person is magnitudes better than just looking at the images. So go there if you can!