原学程将引见若何将AppleScript函数中的齐局变质用于Python代码的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
怎样在Tell运用法式内从我的Python剧本挪用齐局变质?这里是1个例子,解释了这1面能够会产生甚么。假定我想从AppleScrip内部从1个齐局python变质变动一切的";前窗心&。这怎样能够产生?
import subprocess
def get_window_title():
cmd = """
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell
return window_name
"""
result = subprocess.run(['osascript', '-e', cmd], capture_output=True)
return result.stdout
print(get_window_title())
推举谜底
假如您想要将参数传播到AppleScrip中,上面是准确的办法:
#!/usr/bin/env python三
import subprocess
scpt = """
on run {arg一, arg二}
return arg一 + arg二
end run
"""
arg一 = 三七
arg二 = 五
proc = subprocess.Popen(['osascript', '-', str(arg一), str(arg二)],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, encoding='utf8')
out, err = proc.co妹妹unicate(scpt)
print(out) # 四二
因为您应用的是Python,您借不妨选择py-applescript,即supported或者appscript,即not。假如您须要将庞杂参数传播给AppleScrip,py-applescript
库会主动在罕见的AS以及Python典型之间停止转换。AppSCRIPT完整防止应用AppleScrip,但是这要靠您本身。
佳了闭于怎样将AppleScript函数中的齐局变质用于Python代码的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。