使用Applescript触发python脚本

时间:2019-04-27 23:24:13

标签: python applescript

我正在尝试使用Applescript触发python脚本。当我从终端启动时,一切正常,但是当我从Applescript启动时,它似乎可以运行,但是什么也没发生。

我已经尝试了所有形式的组合,以便在搜索和其他帖子中找到所有使用“ python file.py”或“ / usr / bin / python file.py”和“#!/ usr / bin / env python”和“#!/ usr / bin / python”。
如果在终端中输入“哪个python”,则会得到“ / usr / bin / python”

现在,我已经将两个脚本分解为它们的基本组件。我最终将使用Applescript使用sys.argv [1]将文件路径传递到python(这就是为什么我使用Applescript触发python脚本的原因),但我还没有走太远,因为以下内容没有还没工作。

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)

python脚本发送通知程序消息。每次我在终端中运行时,我都会收到没有问题的消息。每次我运行applescript作为shell脚本时,它都在AS中运行而不会出错,但是Python没有消息。

有人在想我哪里出错了吗?

1 个答案:

答案 0 :(得分:0)

如果使用二进制文件的完整路径,对您有用吗?它在BBEdit和Smile(脚本编辑器)中都对我有用。我的路径是:

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

所以我用过:

#!/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)