簡體   English   中英

使用Applescript來解雇python腳本

[英]Using Applescript to fire python script

我正在嘗試使用Applescript來激活python腳本。 當我從終端開火時,一切正常,但是當我從Applescript開火時,它似乎運行但沒有任何反應。

我已經嘗試了各種組合,我可以在搜索和其他帖子中找到使用“python file.py”或“/ usr / bin / python file.py”和“#!/ usr / bin / env python”的所有內容和“#!/ usr / bin / python”。
如果我在終端輸入“which python”,我會得到“/ usr / bin / python”

現在我將這兩個腳本分解為基本組件。 我最終將使用Applescript使用sys.argv [1]將文件路徑傳遞給python(這就是為什么我使用Applescript來激活python腳本)但是我還沒有那么遠,因為下面沒有還沒工作。

AppleScript的

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

蟒蛇

#!/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的消息。

有人想過我哪里出錯了嗎?

如果您使用二進制文件的完整路徑,它是否適用於您? 它在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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM