简体   繁体   English

使用Applescript来解雇python脚本

[英]Using Applescript to fire python script

I'm trying to use Applescript to fire a python script. 我正在尝试使用Applescript来激活python脚本。 When I fire it from terminal, everything works fine, but when I fire from Applescript, it appears to run but nothing happens. 当我从终端开火时,一切正常,但是当我从Applescript开火时,它似乎运行但没有任何反应。

I've tried all manner of combinations for everything I can find in searches and other posts for using "python file.py" or "/usr/bin/python file.py" with "#!/usr/bin/env python" and "#!/usr/bin/python". 我已经尝试了各种组合,我可以在搜索和其他帖子中找到使用“python file.py”或“/ usr / bin / python file.py”和“#!/ usr / bin / env python”的所有内容和“#!/ usr / bin / python”。
If I enter "which python" in terminal, I get "/usr/bin/python" 如果我在终端输入“which python”,我会得到“/ usr / bin / python”

Right now I have both scripts broken down to their base components. 现在我将这两个脚本分解为基本组件。 I'll eventually be using Applescript to pass a file path into python using sys.argv[1] (which is why I'm using Applescript to fire the python script) but I'm not even that far along yet as the below doesn't work yet. 我最终将使用Applescript使用sys.argv [1]将文件路径传递给python(这就是为什么我使用Applescript来激活python脚本)但是我还没有那么远,因为下面没有还没工作。

Applescript AppleScript的

do shell script "/usr/bin/python $HOME/Desktop/test.py"

Python 蟒蛇

#!/usr/bin/python

import sys
import os

# The notifier function
def notify(title, subtitle, message):
    t = '-title {!r}'.format(title)
    s = '-subtitle {!r}'.format(subtitle)
    m = '-message {!r}'.format(message)
    os.system('terminal-notifier {}'.format(' '.join([m, t, s])))

# Calling the function
notify(title    = 'Message Test',
       subtitle = 'Test1:',
       message  = 'Test2')

sys.exit(0)

The python script sends a notifier message. python脚本发送通知程序消息。 Every time I run in terminal, I receive the message without issue. 每次我在终端运行时,都会收到消息而没有问题。 Every time I run the applescript to do as shell script it runs without error-ing in AS, but no message comes from Python. 每次我运行applescript作为shell脚本时,它都会在AS中运行而不会出现错误,但是没有来自Python的消息。

Anyone have thoughts on where I've gone wrong? 有人想过我哪里出错了吗?

Does it work for you if you use a full path to the binary? 如果您使用二进制文件的完整路径,它是否适用于您? It worked for me in both BBEdit and Smile (script editor). 它在BBEdit和Smile(脚本编辑器)中都适合我。 My path is: 我的道路是:

/Applications/terminal-notifier-2.0.0/terminal-notifier.app/Contents/MacOS/terminal-notifier

So I used: 所以我用过:

#!/usr/bin/python

import sys
import os

# The notifier function
def notify(title, subtitle, message):
    t = '-title {!r}'.format(title)
    s = '-subtitle {!r}'.format(subtitle)
    m = '-message {!r}'.format(message)
    os.system('/Applications/terminal-notifier-2.0.0/terminal-notifier.app/Contents/MacOS/terminal-notifier {}'.format(' '.join([m, t, s])))

# Calling the function
notify(title    = 'Message Test',
       subtitle = 'Test1:',
       message  = 'Test2')

sys.exit(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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