简体   繁体   中英

How do I print to terminal with an AppleScript executed with Python?

I have a Python script that executes an apple script. I'd like to print to terminal from within the apple script.

Here is my Python code.

import applescript

myfunction = """

do shell script "echo " & "words to terminal"

"""

def runfunction():
    applescript.tell.app("Terminal", myfunction, background = False)

And then I execute this with python -c 'import myapplescript; print myapplescript.runfunction()' python -c 'import myapplescript; print myapplescript.runfunction()'

I've tried to print to terminal from within the apple script using the "do shell script" phrase and also copy "Hello World!" to stdout copy "Hello World!" to stdout

import applescript

That is not a very good library. If your needs are simple, I would just use subprocess directly .

Also be aware that osascript is limited in its own IO support. There's no built-in way to access stdin, and the only data that gets written to stdout is the last value (if any) returned at the end of the script. (You can write to stderr at any time using the log command, though that will have its own set of issues.)

If you want to access stdin/stdout directly in your AppleScript code, you'll have to use Cocoa's NSFileHandle class. I wrote a File library some years back that provided easy-to-use wrappers around that, though I don't maintain or support it.

If your needs are more advanced—eg you want to call one or more AppleScript handlers or pass anything more complex than simple (short) strings—and you have PyObjC installed, you'd be better using [this library] ( https://pypi.org/project/py-applescript/ ) or the AppleScript-ObjC bridge .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM