I finally upgraded to Monterey last month. There is only one major change as far as scripting goes, and it’s expected. Python 2 is gone, but Python 3 replaces it. Most other scripting languages remain.
Installing Python
If you want Python 2, you’ll need to install it yourself. If you’ve been using Python 3 via homebrew you can continue doing so; just be aware that there is now a default python3 in /usr/bin.
For your older scripts that still use Python 2, the easiest choice is the installer from Python.org. It doesn’t need to be managed through homebrew, because it’s never going to be updated again. Follow the directions to double-click the Install Certificates command as well, but do so from an administrative account.
If you are using homebrew for other things, you may find it easier to install Python 2 using homebrew as well; otherwise, other homebrew installations may try to install it anyway as a dependency. While there technically shouldn’t be any dependencies on Python 2, I found that this isn’t true for some older software. I’m not, apparently, the only one who is still using Python 2 for some important legacy apps.
Both methods of re-installing Python 2 will install it in /usr/local/bin
, so that all you need to do to get your legacy Python scripts running is to replace #!/usr/bin/python
with #!/usr/local/bin/python
. If you were using /usr/bin/env python
it will probably work without any change at all. But be aware that crontab
will not necessarily import your environment, and so will not be able to find Python via env
.
For that reason, I’ve just changed all /usr/bin/python and /usr/bin/env python to /usr/local/bin/python whenever I get the message that it can’t find an executable.
The transition from Python 2 to Python 3 is not seamless. Any command-line script that uses print
, which is just about all of them, will fail.
The Ventura version of 42 Astounding Scripts will continue to use Python, though it will be Python 3.
Python modules
If you’re re-installing Python 2, you may also have to reinstall any modules, such as lxml
, holidays
, and loremipsum
. If you’re switching from Python 2 to Python 3, you’ll definitely have to re-install them: the modules for each major version of Python are stored in different places.
After I reinstalled Python 2, I also had to sort of reinstall the modules I needed as well. They were all still installed—the macOS installer didn’t throw them away—but the new install of Python 2 wasn’t finding them. The easiest way to fix this was to reinstall using pip
.
You may also have to specify a version. I found that holidays
, for example, will not install the latest version. But it will still install version 0.9.9:
- sudo pip install holidays==0.9.9
If you installed Monterey as an upgrade, your old installs should still exist in /Library/Python/2.7/site-packages
. It’s just that whatever let the system know they’re there is gone. Reinstalling is the easiest way to get that link back. You can look in that folder to see which version you had before the upgrade. That’s how I knew I had had holidays version 0.9.9 working before the install, and how I chose which version to downgrade to when I discovered that the latest version would not install using pip
.
PHP
It looks like php has been removed from /usr/bin also. It’s easily installed back using homebrew.
- brew install php
This will place it in /usr/local/bin, so that your paths will change from /usr/bin/php
to /usr/local/bin/php
. If you’re using PHP for command-line scripts, of course. While I do use it on rare occasions, there are no scripts in 42 Astounding Scripts that use it.
PHP is mainly for web scripting, not command-line scripting.
GraphicConverter and Layer Windows
There’s been a change to getting “every window” in GraphicConverter. It either now includes windows of id -1, that is, palettes, or it removes the ability to (pretend to) interact with them. This causes problems with the Layer Windows script in 42 Astounding Scripts.
Because the non-window windows are at the bottom of the list, the script still works. But it reports that GraphicConverter does not support layering windows because the non-window windows error out. You can fix this by adding whose id is not -1
to the line:
- copy every window to windowList
It should now read:
- copy (every window whose id is not -1) to windowList
Looking forward to Ventura
One of the greatest features added to the not-yet-released Ventura is the ability to schedule emails. This will make the schemail
script for scheduling emails far less useful. Assuming that scheduling emails remains a feature in Ventura’s Mail app, I’ll be replacing schemail
with something else, but I haven’t decided what yet.
- 42 Astoundingly Useful Scripts and Automations for the Macintosh
- MacOS uses Perl, Python, AppleScript, and Automator and you can write scripts in all of these. Build a talking alarm. Roll dice. Preflight your social media comments. Play music and create ASCII art. Get your retro on and bring your Macintosh into the world of tomorrow with 42 Astoundingly Useful Scripts and Automations for the Macintosh!
- Homebrew
- “The missing package manager for macOS (or Linux)”
- How to reinstall python@2 from Homebrew? at Stack Overflow
- “It seems that the homebrew staff really makes it as hard as possible to use Python 2.7 on macOS as they can.”
- Python 2.7
- “Python 2.7.18 is the last release of Python 2.”
More Astounding Scripts updates
- Catalina: iTunes Library XML
- What does Catalina mean for 42 Astounding Scripts?
- Random colors in your ASCII art
- One of the great things about writing your own scripts is that when you need new functionality, you can add it. I needed random colors in a single-character ASCII art image. It was easy to add to the asciiArt script. Here’s how.
- Avoiding lockFocus when drawing images in Swift on macOS
- Apple’s recommendation is to avoid lockFocus if you’re not creating images directly for the screen. Here are some examples from my own Swift scripts. You can use this to draw text into an image, and to resize images.
- 42 Astounding Scripts, Catalina edition
- I’ve updated 42 Astounding Scripts for Catalina, and added “one more thing”.
- Catalina vs. Mojave for Scripters
- More detail about the issues I ran into updating the scripts from 42 Astounding Scripts for Catalina.
- Two more pages with the topic Astounding Scripts updates, and other related pages
More GraphicConverter
- Copying album art with GraphicConverter
- One of the many useful features of GraphicConverter is its extensive AppleScript dictionary. I use it, along with Unskew, to use photos of album covers as album art in iTunes.
- Using AppleScript with GraphicConverter
- Automatically trim an image to a set size, set the appropriate transparent color, and then save and upload a png and gif version of the image.
More Python
- Quick-and-dirty old-school island script
- Here’s a Python-based island generator using the tables from the Judges Guild Island Book 1.
- Goodreads: What books did I read last week and last month?
- I occasionally want to look in Goodreads for what I read last month or last week, and that currently means sorting by date read and counting down to the beginning and end of the period in question. This Python script will do that search on an exported Goodreads csv file.
- Test classes and objects in python
- One of the advantages of object-oriented programming is that objects can masquerade as each other.
- Timeout class with retry in Python
- In Paramiko’s ssh client, timeouts don’t seem to work; a signal can handle this—and then can also perform a retry.
- Percentage-based random tables
- Our current random item generator assumes that each item shows up as often as any other item. That’s very OD&D-ish. But AD&D uses percentage dice to weight toward some monsters and items more than others.
- 30 more pages with the topic Python, and other related pages