andrewpetrochenkov / osascript.py

:apple: :snake: osascript (AppleScript) python implementation
The Unlicense
36 stars 3 forks source link

How to return result to calling app? #3

Open pauljvallance opened 5 years ago

pauljvallance commented 5 years ago

you show how to return a result to a dialog box on screen e.g

code,out,err = osascript.run('display dialog "Finished"')

I would like to return a result such as a string of text directly to my calling app ( which is XOJO sending a shell command to Terminal to do script xyz.py. I can get around this by using your osascript.run to call an applescript from within the python script which then sends a custom AppleEvent back to my calling app. it seems that an OSAScript call to run a Python script in MacTerminal returns the id of the Terminal Window.

andrewpetrochenkov commented 5 years ago

if I understand everything correctly, then you need to execute the command in a new Terminal.app window and return the output string?

in that case you need some wrapper. for example, with .command

/tmp/$$.command

#!/usr/bin/env bash

command > /tmp/$$.log
mv /tmp/$$.log ~/app.log

chmod +x /tmp/$$.command open -a Terminal /tmp/$$.command while ! [ -e ~/app.log ]; do sleep 1; done output="$(cat ~/app.log)"