Page 1 of 1

Python via shell call: does not work properly; empty result

PostPosted: Thu Mar 14, 2019 12:47 am
by hpak2
Hi!

I do have a workflow where I dictate something into Google docs and, export it as a txt file and then I run python script over this txt file to exchange things like 'comma' for "," or "dot" for ".", just it is in German, the exchange. The python script runs successful if I call it from the shell and produces a new txt file in the same directory with the "new" in front.

But somehow it produces an empty result aka a new file with nothin' in it. :shock:

Any idea what I am doing wrong?

And btw, any idea how I could add these new file (once they are no longer empty) automatically to a certain scrivener file?

This is how I call it in hazel (except for the ".....") :
Code: Select all
python3 /Users/ak/....../batchReplaceGoogleDocs.py $1


and this the python code:
Code: Select all
import sys, os

toRepl = {
"Klammer auf ": "(",
" Klammer zu": ")",
" Punkt": ".",
" Komma": ",",
" Comma": ",",
"neue Zeile": "\n",
"neue Zeilen": "\n",
" Doppelpunkt": ":",
" Ausrufungszeichen": "!",
"Ausrufezeichen": "!",
" comma": ",",
"Stern": "*",
"Bindestrich": "-"
}

filename = sys.argv[1]
newFileName = os.path.join(os.path.split(filename)[0], "new"+os.path.split(filename)[1])

file = open(filename, 'r')
fileNew = open(newFileName, 'w')

for line in file:
    for k, v in toRepl.items():
        line = line.replace(k, v)
    fileNew.write(line)

fileNew.close()
file.close()

Re: Python via shell call: does not work properly; empty res

PostPosted: Thu Mar 14, 2019 11:05 am
by Mr_Noodle
Is there a reason why you aren't using the Text Replacement option in Hazel?

If you still need to use the script, make sure you use full paths for everything; you can't assume Hazel has access to the same environment that you do in Terminal.

Re: Python via shell call: does not work properly; empty res

PostPosted: Thu Mar 14, 2019 2:16 pm
by hpak2
Thanks.

Mr_Noodle wrote:Is there a reason why you aren't using the Text Replacement option in Hazel?


What? - I looked through all my hazel possibilities but this is now option can find...

Mr_Noodle wrote:If you still need to use the script, make sure you use full paths for everything; you can't assume Hazel has access to the same environment that you do in Terminal.


I do so. I think.
But the funny thing is: why does it run but delivers no result within the newly created file while it does work like charm when called from the command line directly?

Re: Python via shell call: does not work properly; empty res

PostPosted: Fri Mar 15, 2019 11:47 am
by Mr_Noodle
As mentioned, the commandline will have different environment variables set that may not be there when run in Hazel.

Back to Hazel's built-in text replacement, I just realized your script is doing the replacement in the contents, not the filename. So, it's not applicable in this case. Sorry about that.