Moe
Moe

Reputation: 21

Applescript - Get return from python

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

Upvotes: 1

Views: 1086

Answers (2)

deek5
deek5

Reputation: 365

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

Upvotes: 0

matthewjselby
matthewjselby

Reputation: 112

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

Upvotes: 3

Related Questions