【发布时间】:2020-12-31 08:48:30
【问题描述】:
我正在尝试在 Python 脚本中嵌入 AppleScript。我不想将 AppleScript 保存为文件,然后将其加载到我的 Python 脚本中。有没有办法在 Python 中将 AppleScript 作为字符串输入并让 Python 执行 AppleScript?非常感谢。
这是我的脚本: 导入子流程 重新进口 导入操作系统
def get_window_title():
cmd = """osascript<<END
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
END"""
p = subprocess.Popen(cmd, shell=True)
p.terminate()
return p
def get_class_name(input_str):
re_expression = re.compile(r"(\w+)\.java")
full_match = re_expression.search(input_str)
class_name = full_match.group(1)
return class_name
print get_window_title()
【问题讨论】:
-
仅供参考,PyPI 上有一个小 Python 模块可以解决问题:pypi.python.org/pypi/python-applescript/0.1
标签: python macos applescript