开发者

Using Third-Party Modules with Python in an Automator Service

开发者 https://www.devze.com 2022-12-17 00:52 出处:网络
I have installed Py-Appscript on my machine and it can be used with the Python installation at /Library/Frame开发者_运维技巧works/Python.framework/Versions/Current/bin/python.

I have installed Py-Appscript on my machine and it can be used with the Python installation at /Library/Frame开发者_运维技巧works/Python.framework/Versions/Current/bin/python.

I am trying to use this installation of Py-Appscript with an Automator service. To do this, I use the Run Shell Script action and then set the Shell to usr/bin/python (which is my only choice for Python, unfortunately).

The usr/bin/python does not appear to have access to my third-party modules and crashes on the line:

from appscript import *

Is there a way for me to give usr/bin/python access to my third-party modules? OR Is there a way to tell Automator to use /Library/Frameworks/Python.framework/Versions/Current/bin/python instead?

I need Automator to run the Python directly from the Run Shell Script action. Any action that calls Python scripts that are external to Automator (via bin/bash, for example) does not perform quickly enough to be useful.


Okay, I was able to get it working using a hack found at How do I execute a PHP shell script as an Automator action on Mac OS X.

Inside of the Run Shell Script action, I used the /bin/sh/ shell with <<EOF ... EOF to the proper Python installation.

So for example, entering

/Library/Frameworks/Python.framework/Versions/Current/bin/python <<EOF
from appscript import *
Numbers = app('Numbers')
EOF

Into the code section of the Run Shell Script action will work. So one can call the proper installation (/Library/Frameworks/Python.framework/Versions/Current/bin/python above) and put their program between the <<EOF ... EOF delimeters.

Alfred

This trick works with Alfred also. If you want to use appscript with Alfred, just make sure that you pass {query} to the python version above, like this:

/Library/Frameworks/Python.framework/Versions/Current/bin/python script.py {query}


When you install modules, you typically install them per Python instance. So in this case you have installed them for the Python in /Library/Frameworks/Python.framework/Versions/Current/bin/python, and it will then be available only for that Python. /usr/bin/python is then apparently another Python installation (I'm not an OS X expert).

To make it available for the /usr/bin/python installation, install it for /usr/bin/python.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号