I have a Xcode ASobjc app
Beside all the trouble I have with (Monterey) Ventura a shell script causes me massive head aches.
set res to do shell script "/usr/bin/python -c 'import sys; s = unicode(sys.stdin.read(), \"utf8\").encode(\"unicode-internal\").encode(\"base64\"); sys.stdout.write(s)' < " & (quoted form of POSIX path of zPath)
Ventura comes with python 3, so I get an error “/usr/bin/python” not found. In terminal “which -a python3” points to “/usr/bin/python3”, but when I change the python path it’s still not working.
Same with
do shell script "/usr/bin/python -c 'import sys; s = unicode(sys.stdin.read(), \"utf8\").decode(\"base64\").decode(\"unicode-internal\").encode(\"utf8\"); sys.stdout.write(s)' <<<" & (quoted form of t) without altering line endings
Any help is really appreciated - I’m quite desperate
Andreas
macOS no longer ships with Python, but if you installed Xcode then you should have python3 from it, dynamically loaded by /usr/bin/python3. Neither macOS nor Xcode any longer includes python 2.
Not what you are asking for, but maybe you could skip Python altogether on this and just use the built-in base64 command? That would also have the benefit of working on Macs that do not have py installed.
man base64
→ “base64 – Encode and decode using Base64 representation”
Your issue is with python3, not just with the path to the executable.
python3 changed how unicode was handled, making it the default string implementation (it is because the changes were so far-reaching that python2 was kept around for so long).
You need to revise your python code to address the error (NameError: name 'unicode' is not defined), since unicode() is no longer a valid function.
You will need something like this:
do shell script "/usr/bin/python3 -c 'import sys; import codecs; s = codecs.decode(sys.stdin.read().encode(), \"base64\").decode(\"UTF-16\"); sys.stdout.write(s)' <<<" & (quoted form of t) without altering line endings
It looks like unicode-internal is a python-internal encoding & shouldn’t be used. It’s not available in python3 from the looks of things, so I’ve changed this to UTF-16.
The bad thing is that the “internal” is why I’ve to use Python.
Your test code looks promising:)
I wrote this Python stuff years ago with a lot trial and error and no real knowledge about Python.
I’ll try later today what you suggested.
import sys, time, logging, os, subprocess
from datetime import date
Use and IDE like PyCharm then to diagnose your Python rather than trying to figure it out in a shell script. It will help you through issues like encoding.
As always, use Homebrew to install Python and keep your dependencies up to date.