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

Talk, speculate, discuss, pontificate. As long as it pertains to Hazel.

Moderators: Mr_Noodle, Moderators

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()
hpak2
 
Posts: 3
Joined: Mon Mar 11, 2013 7:12 am

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.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

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?
hpak2
 
Posts: 3
Joined: Mon Mar 11, 2013 7:12 am

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.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Open Discussion