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

Mar 11, 2018 1:23 PM in response to rccharles

An Applescript to show user info.


(*

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
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    --  debug lines
    set desktopPath to (path to desktop) as string
    log "desktopPath = " & desktopPath

    set unixDesktopPath to POSIX path of desktopPath
    log "unixDesktopPath = " & unixDesktopPath

Here is an applescript to display some system info.
    set quotedUnixDesktopPath to quoted form of unixDesktopPath
    log "quoted form is " & quotedUnixDesktopPath

    try
        set fromUnix to do shell script "pwd  "
        display dialog "pwd " & return & fromUnix giving up after 7
        set fromUnix to do shell script "id "
        display dialog "id " & return & fromUnix giving up after 7
     
     
        set fromUnix to do shell script "ls -l  " & quotedUnixDesktopPath
        display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix giving up after 10
    on error errMsg
        log "ls -l error..." & errMsg
    end try

end run




User uploaded file


User uploaded file


Third display not recorded.

Mar 12, 2018 5:46 PM in response to mmodi1

I get the same error when I run this script in Yosemite and High Sierra.


(* 

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
    
    
    
    set fromUnix to do shell script "pwd  "
    log "pwd " & return & fromUnix
    set fromUnix to do shell script "id "
    log "id " & return & fromUnix
    set fromUnix to do shell script "python -V"
    log "python " & return & fromUnix
    set fromUnix to do shell script " /bin/ls -l  /"
    log "ls " & return & fromUnix
    set fromUnix to do shell script "/bin/ls -l  /Users/rmac/"
    log "ls " & return & fromUnix
    set fromUnix to do shell script "/bin/ls -l ~/Documents/PicToText.py"
    log "ls " & return & fromUnix
    try
        -- /usr/bin/python -d -c
        -- > ~/Desktop/scriptlog.txt
        log "trying PicToText.py"
        set toUnix to "(cd ~/Documents && /usr/bin/python -d -c '~/Documents/PicToText.py') &> ~/Desktop/scriptlog.txt "
        set fromUnix to do shell script toUnix
        log "first fromUnix is: " & fromUnix
        
        
    on error errMsg
        log "  error..." & errMsg
    end try
    set fromUnix to do shell script "/bin/cat  ~/Documents/PicToText.py"
    log "cat " & return & fromUnix
    set fromUnix to do shell script "/bin/cat   ~/Desktop/scriptlog.txt "
    log "cat " & return & fromUnix
end run



(*pwd 
/*)
(*id 
uid=501(rmac) gid=20(staff) groups=20(staff),12(everyone),61(localaccounts),79(_appserverusr),80(admin),81(_appserveradm),98(_lpadmin),33(_appstore),100(_lpoperator),204(_developer),250(_analyticsusers),395(com.apple.access_ftp),398(com.apple.access_screensharing),399(com.apple.access_ssh),701(1)*)
(*python 
*)
(*ls 
total 45
drwxrwxr-x+ 39 root  admin  1326 Mar 12 19:34 Applications
drwxr-xr-x+ 62 root  wheel  2108 Mar 12 19:36 Library
drwxr-xr-x@  2 root  wheel    68 Oct  2 20:33 Network
drwxr-xr-x@  4 root  wheel   136 Jan 19 02:18 System
drwxr-xr-x   5 root  admin   170 Feb 21 16:21 Users
drwxr-xr-x@  7 root  wheel   238 Mar 12 20:12 Volumes
drwxr-xr-x@ 38 root  wheel  1292 Jan 19 02:22 bin
drwxrwxr-t@  2 root  admin    68 Oct  2 20:33 cores
dr-xr-xr-x   3 root  wheel  4339 Mar 12 19:21 dev
lrwxr-xr-x@  1 root  wheel    11 Feb 20 21:30 etc -> private/etc
dr-xr-xr-x   2 root  wheel     1 Mar 12 19:43 home
-rw-r--r--@  1 root  wheel   313 Oct  4 17:05 installer.failurerequests
dr-xr-xr-x   2 root  wheel     1 Mar 12 19:43 net
drwxr-xr-x@  6 root  wheel   204 Feb 20 21:31 private
drwxr-xr-x@ 63 root  wheel  2142 Feb 20 21:31 sbin
lrwxr-xr-x@  1 root  wheel    11 Feb 20 21:30 tmp -> private/tmp
drwxr-xr-x@  9 root  wheel   306 Jan 19 02:13 usr
lrwxr-xr-x@  1 root  wheel    11 Feb 20 21:31 var -> private/var*)
(*ls 
total 0
drwx------+  8 rmac  staff   272 Mar 12 20:21 Desktop
drwx---r-x+  9 rmac  staff   306 Mar 12 20:27 Documents
drwx------+  6 rmac  staff   204 Mar 12 19:34 Downloads
drwx------@ 54 rmac  staff  1836 Mar  5 14:42 Library
drwx------+  3 rmac  staff   102 Feb 21 16:21 Movies
drwx------+  3 rmac  staff   102 Feb 21 16:21 Music
drwx------+  3 rmac  staff   102 Feb 21 16:21 Pictures
drwxr-xr-x+  4 rmac  staff   136 Feb 21 16:21 Public*)
(*ls 
-rwxrwxrwx@ 1 rmac  staff  56 Mar 11 18:05 /Users/rmac/Documents/PicToText.py*)
(*trying PicToText.py*)
(*  error...The command exited with a non-zero status.*)
(*cat 
#! /usr/bin/python

print("Hello, World from python!!!")*)
(*cat 
  File "<string>", line 1
    ~/Documents/PicToText.py
     ^
SyntaxError: invalid syntax*)

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.