Running Python in Automator

I am trying to make a pic to text code for Python. I run it in Atom(compiler) and it works fine. I run it in Terminal and it runs fine, but I'm trying to do it in automator but it isn't working. Here's the error:


User uploaded file

macOS High Sierra (10.13.2)

Posted on Mar 11, 2018 9:38 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 11, 2018 12:16 PM

In the Run Shell Script action, set the Shell selection to /usr/bin/python.


Python does not implicitly know that your PicToText.py script is in your Documents folder, so you must tell it with (the following is all one line and may wrap):


(cd ~/Documents;source ~/.bash_profile;python -d -c ./PicToText.py) > ~/Desktop/PicToText.log

34 replies
Question marked as Top-ranking reply

Mar 11, 2018 12:16 PM in response to mmodi1

In the Run Shell Script action, set the Shell selection to /usr/bin/python.


Python does not implicitly know that your PicToText.py script is in your Documents folder, so you must tell it with (the following is all one line and may wrap):


(cd ~/Documents;source ~/.bash_profile;python -d -c ./PicToText.py) > ~/Desktop/PicToText.log

Mar 11, 2018 1:51 PM in response to mmodi1

Set the Run Shell Script Shell back to /bin/bash, or it will not understand some of the shell syntax here. Also, what is going on with the Automator action that precedes your Run Shell Script? The double ampersand is saying do the following if the result of cd ~/Documents is successful.


(cd ~/Documents && /usr/bin/python -d -c ./PicToText.py) &> ~/Desktop/scriptlog.txt

Mar 11, 2018 4:22 PM in response to mmodi1

For what it's worth, I got this to work. I ran it from within applescript. The process wasn't without a little pain, so you will see some simplifications.


You can see that I simplified what goes to the command line. I have the output be returned directly to applescript. Simplify to get things to work, then add things back if needed.


(*

It is easier to diagnose problems with debug information. I suggest adding
log statements to your script to see what is going on.  Here is an example.


    Author: rccharles

    For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log
          statement
      2) Click on Run

  For running shell commands see:
  http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html



*)


on run


    try
        set fromUnix to do shell script "pwd  "
        log "pwd " & return & fromUnix
        set fromUnix to do shell script "id "
        log "id " & return & fromUnix
        -- /usr/bin/python -d -c
        -- > ~/Desktop/scriptlog.txt
   
        set toUnix to "cd ~/Documents  &&   ~/config/PicToText.py  "
        set fromUnix to do shell script toUnix
        log "first fromUnix is: " & fromUnix
   
   
    on error errMsg
        log "  error..." & errMsg
    end try

end run




You can see I added where to run python on the script. I could not get the -c option to work. Should you not want to modify the script, you can always put a unix ln command where ever the script thinks it should be.


#! /usr/bin/python

print("Hello, World from python!!!")



mac $
mac $ ls -l PicToText.py
-rwxr-xr-x@ 1 mac  staff    56B Mar 11 19:05 PicToText.py*
mac $

Mar 14, 2018 12:32 PM in response to mmodi1

Hello


For your original python code, you may simply try the following automator workflow of single run shell script action. Note that there should not be used -c option in python invocation in this case.



1) Run Shell Script action


shell = /bin/bash

pass input = arguments or stdin (not relevant)

code = as follows:


#!/bin/bash source ~/.bash_profile cd ~/Documents || exit python -d PicToText.py




---

For a little more general code processing arguments, you might try something like the following as python script:



#!/usr/bin/python # coding: utf-8 # # file: # PicToText.py # # function: # OCR image to text # # usage # PicToText.py file [file ...] # # output file is named after input file's basename without extension followed by '.txt' # e.g., file.png is OCR'ed and resulting text is saved in file.txt in the same directory as input # import sys, os import pytesseract from PIL import Image for f in [ a.decode('utf-8') for a in sys.argv[1:] ]: t = pytesseract.image_to_string(Image.open(f)) g, e = os.path.splitext(f) with open(g + '.txt', 'w') as f1: f1.write(t.encode('utf-8'))



And the following workflow of two acitons:


1) Run AppleScript action


code = as follows:


choose file with multiple selections allowed




2) Run Shell Script action


shell = /bin/bash

pass input = arguments

code = as follows:


#!/bin/bash source ~/.bash_profile cd ~/Documents || exit python -d PicToText.py "$@"



Good luck,

H

Mar 11, 2018 11:35 AM in response to mmodi1

Because it is using the default system PATH, and not yours, so it cannot find the Python program. My .bash_profile has a custom PATH statement in it that also includes my $HOME directory, and current (.) directory. You may have to still use ./PicToText.py to force it to look in the Documents folder for that script — or its full path.


Also, if your Python program is written to get ARGV elements passed in from the previous action, then change the pass input to Arguments. If you are using the Python fileinput module to slurp standard input, then leave the current setting to stdin.


(cd ~/Documents; source ~/.bash_profile; python -d -c PicToText.py) > ~/Desktop/PicToText.log

Mar 12, 2018 4:24 AM in response to rccharles

The -c option to Python is telling Python to look for a valid script, or quoted inline Python command. Python could care less about the path associated with the script name — provided that the path, and script are valid, and the latter is without internal syntax errors.


The OP's code is currently not written to receive arguments from stdin, or ARGV (command-line) passed into it from a preceding action.


My scripts all have the #!/usr/bin/python, or #!/usr/bin/env python (if multiple Python versions) as the first line and are marked executable so that -c is not required to run the script.


The syntax that I provided, and shown below — works without error when I substitute one of my own Python scripts in the Automator Run Shell Script action. The following is all one line, and I personally never use the -d switch:


(cd ~/Documents && /usr/bin/python -d -c ./PicToText.py) &> ~/Desktop/scriptlog.txt

Mar 12, 2018 6:06 PM in response to rccharles

This works for me. I run it from applescript editor. Worked in 10.10.5.


(*

It is easier to diagnose problems with debug information. I suggest adding
log statements to your script to see what is going on.  Here is an example.


    Author: rccharles

    For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log
          statement
      2) Click on Run

  For running shell commands see:
  http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html



*)


on run
    
    
    try
        set toUnix to "ls -l ~/Documents/PicToText.py  "
        set fromUnix to do shell script toUnix
        log "fromUnix ls is: " & fromUnix
        set toUnix to "cat ~/Documents/PicToText.py  "
        set fromUnix to do shell script toUnix
        log "fromUnix cat is: " & fromUnix
        
        
        set toUnix to "cd ~/Documents  &&   ./PicToText.py  "
        set fromUnix to do shell script toUnix
        log "fromUnix run icToText.py is: " & fromUnix
        
        
    on error errMsg
        log "  error..." & errMsg
    end try
    
end run




(*fromUnix ls is: -rwxr-xr-x@ 1 mac  staff  56 Mar 11 19:05 /Users/mac/Documents/PicToText.py*)
(*fromUnix cat is: #! /usr/bin/python

print("Hello, World from python!!!")*)
(*fromUnix run icToText.py is: Hello, World from python!!!*)



#! /usr/bin/python

print("Hello, World from python!!!")

Mar 11, 2018 1:25 PM in response to mmodi1

Do not use any ~ in your scripts. Your not running were you think.


Use all explicit paths. Of course, this is a make it work approach. run the id command to see what is going on.


I suggest you first debug this in an actual applescript. It could be that any time a shell command get an error your applescript quits. I think so. See my applescript in the next post for catching errors.


R

Mar 11, 2018 4:39 PM in response to rccharles

I believe the reason -c didn't work is that the -c option is expecting a valid python code. Passing the location of the python script isn't valid python.


See: Medhat Helmy answer for how to call a python script from python code. It got fewer points, but is the more current answer, I think.

Run a python script from another python script, passing in args - Stack Overflow

Mar 14, 2018 8:42 AM in response to mmodi1

Well, that's progress. It's been slow I'll say.


but it didn't change my HellowWorld.txt file like it shouldve.

I'd like to say the obvious. 😕

Well, I don't have enough information to comment on why. We need to debug it. I cannot believe this is the first thing I learned when programming. Put in print statements. It's hard to believe we are still doing this.


You need to put in print statement each step of the way. I leave in the print statement even after the code is "working". Would help if you posted the code. You can post in at pastebin.com if posting here gives you troubles.


How to post code.


click on Use advanced editor. It's just above the reply box to the far right of the reply box.


click on the blue >>. It's next to the smiling face.


click on syntax highlighting.


click on plain




This will open a little box. Drop your code in to this box.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Running Python in Automator

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.