1

I have an applescript which starts an python script. The python script calculates an integer. Is it possible to return the result as an integer or string to the applescript?

thx

1
  • 1
    Can you give examples of the code? Commented Oct 31, 2016 at 15:47

2 Answers 2

3

You should be able to return a value from a python script run from an applescript like this:

set scriptResult to do shell script ("python path/to/script.py")

where scriptResult is then set to any output provided to stdout (e.g., from a print statement) in the python script. So in your python script you could simply:

print yourInteger

and scriptResult would then be set to [integer]. Note that scriptResult will be set to all stdout, so everything you print will be included here. You also may need to do some casting in your applescript to make scriptResult a number:

set scriptResult to (do shell script ("python path/to/script.py")) as integer
Sign up to request clarification or add additional context in comments.

3 Comments

Not relevant to OP's particular use case, but worth noting here that do shell script uses UTF8 encoding, so additional encode()/decode() futzing may be required in the Python code when dealing with anything other than ASCII strs.
It's not working for me! :/ error "python: can't open file '[/Users/Usr/Desktop/xMail.py]': [Errno 2] No such file or directory" number 2
Lose the brackets, those were only there to indicate where something should be inserted into the script.
0

In the same genre using do shell script with directly script python

set xy to do shell script "echo   'import sys, time; from Quartz.CoreGraphics import *
def mouseEvent(type, posx, posy): theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft); CGEventPost(kCGHIDEventTap, theEvent);
def mouseclickdn(posx,posy): mouseEvent(kCGEventLeftMouseDown, posx,posy);
def mouseclickup(posx,posy): mouseEvent(kCGEventLeftMouseUp, posx,posy);
ourEvent = CGEventCreate(None); currentpos = CGEventGetLocation(ourEvent); print currentpos[0]; print currentpos[1]; mouseclickdn(currentpos.x, currentpos.y);
mouseclickup(currentpos.x, currentpos.y);' | python "

set xy to do shell script "echo " & xy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.