Shell scripts : a bit off topic

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 :frowning:
Andreas

I know nothing about Python but, should not your code start with:
do shell script "/usr/bin/python3

Forgive me if I’m totally off the mark.

The “usr/bin/python” is the original “non ventura”, and the “usr/bin/python3” is not working

:frowning:

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.

I have Xcode installed.
The script is part of an ASobjc app.
Terminal “which -a python3” list the paths
/usr/bin/python3 doesn’t work

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.

Thanks Havard.
Unfortunately base64 is not working in this case cause of the encoding.

Andreas

Thanks tree_frog

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.

Andreas

Well, good luck. :slight_smile:

See this answer on Stack Overflow for more information on Python’s internal string representation. It depends on how Python was compiled.

code your import within a py script.
Just call the py script without extras.
So within your Py script, begin your code like

> #! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
> # -*- coding=<utf-8> -*-

And then your import lines are like

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.

Good luck

Hello btully,

Many thanks for the suggestion.
Unfortunately this won’t work for me - and those who use the app.

Andreas