Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

running python script with system arguments from applescript

Hi,


I am trying to run a python code from within applescript. A string should be passed as an argument to the python script, i.e. from the terminal, I would do the following


cd /Users/thatsme/Library/Scripts/myfolder

python my_python_file.py abcdefg


There are two ways I figure I could do this in applescript. "abcdefg" are contained in the variable strNote


a) directly:


do shell script "cd /Users/thatsme/Library/Scripts/myfolder; python my_python_file.py " & strNote


b) calling Terminal


tell application "Terminal"


activate

do script "cd /Users/claushaslauer/Library/Scripts/OO_latex; python my_python_file.py " & strNote

end tell



I would prefer option a) as I don't really want to have Terminal windows popping up. The strange thing is, that I see in the applescript results window the result of the print statements in the python script, but no output is generated (the python script contains a few os.system() commands.


Option b) does what it is supposed to be doing, however, applescript does not wait until the script is finished running.


Why does option a) nor work, and what do I need to change to make it work?

Posted on Feb 6, 2014 12:39 AM

Reply
14 replies

Feb 6, 2014 5:21 AM in response to Carlossos

Carlossos wrote:


The strange thing is, that I see in the applescript results window the result of the print statements in the python script, but no output is generated


What is the output of the script? Is is just a return of something?

If so, try:


set results to do shell script "cd /Users/thatsme/Library/Scripts/myfolder; python my_python_file.py " & quoted form of strNote

display dialogresults

Feb 6, 2014 5:37 AM in response to VikingOSX

- the string that I am trying to pass as an argument is a string that is a valid latex math expression

- so when I say

set strNoteQ to quoted form of lstNote


then I get


error "Can’t get quoted form of {\"1+1=2\"}." number -1728 from quoted form of {"1+1=2"}


- the point of the python code is to

- generate a valid latex file, where the passed string is a component of

- process that latex file and generate a pdf


I am not sure anymore if it is a good idea to pass a latex-math string as a command line argument. But then, what is a good idea for that? -- I found this related knowledge base entry... so there are sure issues...

- plus sign, minus sign, equal sign should be valid ascii symbols, shouldn't they?

- escaping them with a backslash did not help, anyways

- but what does... ?

Feb 6, 2014 5:54 AM in response to Carlossos

Carlossos wrote:


- the string that I am trying to pass as an argument is a string that is a valid latex math expression

- so when I say

set strNoteQ to quoted form of lstNote


then I get


error "Can’t get quoted form of {\"1+1=2\"}." number -1728 from quoted form of {"1+1=2"}



How did you set lstNote and what does it "look" like? (run display dialoglstNote)

Feb 6, 2014 6:15 AM in response to Carlossos

Using do shell script is different than running a command in terminal. Environment variables (and in particular the $PATH variable) may be different, and other things that you expect in a terminal.app shell may not be present in a do shell script shell. Make sure that you spell out full paths to everything in both the applescript and the python script.


Also, you may be having trouble with applescript quoting. you want the string you feed into applescript to look like "{\"1+1=2\"}" or maybe "'{\"1+1=2\"}'". Applescript will interpret this - {"1+1=2"} - as a list that contains the string "1+1=2", not as the latex expression you think you're entering.

Feb 6, 2014 6:22 AM in response to Carlossos

error "Can’t get quoted form of {\"1+1=2\"}." number -1728 fromquoted formof{"1+1=2"}

lstNote is a list. You can;t get the quoted form of a list.


Either make lstNote a simple string


set lstNote to "1+1=2"


or

get the item out of the sting



set lstNote to {"1+1=2"}

set strNoteQ to quoted form of item 1 of lstNote


Message was edited by: Frank Caggiano - ah latex so you really want the string {"1+1=2"} in that case you need to escape the {}'s


set lstNote to "{\"1+1=2\"}"

Feb 6, 2014 7:07 AM in response to Frank Caggiano

Thank you guys for your help, this is really weird.


Here is my full applescript


set strNoteQ to "1+1=2"

(* for whatever reason, this does not work *)

do shell script "cd /Users/claushaslauer/Library/Scripts/OO_latex; python create_latex_pdf.py " & strNoteQ



Below is my python skript.


When I call it as

python create_latex_pdf.py 1+1=2


it creates a wonderful pdf.


With the applescript above, it prints the print statements, it also generates the latex file temp.tex -- correctly, so I assume the string in this example is ok.


However, it does not execute the latex related commands (the three os.system calls)


Is it not a good idea / not possible to execute os.system in a python script that is called from applescript? <sorry if I had brought all the attention to the string issue>


here is the python script:





#!/usr/bin/env python

# -*- coding: utf-8 -*-


import sys

import os

import datetime

import time


def main():

str_latex_note = sys.argv[1]

print "%s" % str_latex_note


preamble = """\documentclass{article}

\usepackage{amsmath,amssymb}

\pagestyle{empty}



\\begin{document}

{\huge

\[

%s

\]

}

\end{document}"""% (str_latex_note)

## write latex file

cur_path = r'/Users/mypath/Library/Scripts/OO_latex'

cur_file = "temp.tex"

fobj = open(os.path.join(cur_path, cur_file), 'w')

fobj.writelines(preamble)

fobj.close()

## process latex file

print cur_path

os.chdir(cur_path)

cmd = 'latex temp.tex'

print cmd

print "running latex"

os.system(cmd)

cmd2 = 'dvips -E -f -X 1200 -Y 1200 temp.dvi > temp.ps'

print "running dvips"

os.system(cmd2)

cmd3 = 'epstopdf temp.ps'

print "running epstopdf"

os.system(cmd3)

print "done!"

if __name__ == '__main__':

main()

Feb 6, 2014 8:10 AM in response to Carlossos

As there is no user interaction in your AppleScript yet, is this just a test harness until you get the passed LaTeX string working? Any reason why you couldn't run AppleScript (HERE text) as a subprocess from within Python, and get the string back directly from the subprocess into Python?


set strNoteQ to "1+1=2" as text


If this is supplied as an argument to Python in the do shell script as just & strNoteQ, it goes into Python as though it were typed on the Python command line in the shell. Tested.

Feb 6, 2014 2:42 PM in response to Carlossos

Hello


I'd guess it's PATH issue. The "do shell script" command does NOT inherit bash environments unless you explicitly source the profile.


Try something like this, provided that your environments are set in ~/.bash_profile:


set s to "1+1=2"
do shell script "source ~/.bash_profile
cd /Users/claushaslauer/Library/Scripts/OO_latex
python create_latex_pdf.py " & s's quoted form


Regards,

H

Feb 6, 2014 11:56 PM in response to Hiroto

the bash_profile did it!


I had started to run latex, dvips and epstopdf individually as do shell scripts from applescript (and not from python) and by calling them directly via /usr/texbin/latex etc.


This worked ok for latex and dvips, but not for epstopdf, when I got this error message: "Cannot open Ghostscript for piped input"


Thanks everybody for your patience and help! I learned a lot in the process.

running python script with system arguments from applescript

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