如何在 Python 脚本中嵌入 AppleScript?
我正在尝试将 AppleScript 嵌入到 Python 脚本中。我不想将 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()
I am trying to embed an AppleScript in a Python script. I don't want to have to save the AppleScript as a file and then load it in my Python script. Is there a way to enter the AppleScript as a string in Python and have Python execute the AppleScript? Thanks a bunch.
Here is my script:
import subprocess
import re
import os
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用子进程:
Use subprocess:
在 python 3 中,情况略有不同:
Popen 现在需要一个类似字节的对象,要传递字符串,需要
universal_newlines=True
参数。In python 3 it would be slightly different:
Popen now expects a byte-like object, to pass a string, the
universal_newlines=True
parameter is needed.这篇文章 建议:
然而,如今,
subsystem.Popen
通常比os.system
更受欢迎(文章是从三年前开始的,当时没有人在看到 os.system 调用时尖叫;-)。Example 3 in this article suggests:
These days, however,
subsystem.Popen
is usually preferred overos.system
(the article is from three years ago, when nobody screamed on seeing anos.system
call;-).如果您希望您的 python 代码不等待 Applescript 完成,这里有一个简单的 python3 同步示例。在此示例中,两个 say 命令都是并行执行的。
Here's a simple python3 synchronous example, if you want your python code not to wait for Applescript to finish. In this example, both
say
commands are executed in parallel.subprocess.run()
是 现在优先优于subprocess.popen()
。这是运行 AppleScript 代码并获取结果的非常简单的方法。subprocess.run()
is now preferred oversubprocess.popen()
. Here is a pretty simple way to run AppleScript code and get the results back.请参阅 https://pypi.org/project/applescript/
等。
大多数建议,包括我引用的“applescript”,都缺少 osascript 的一项重要设置——将 -s 选项设置为“s”,否则您将难以解析输出。
See https://pypi.org/project/applescript/
etc.
Most of suggestions, including "applescript" I quoted, are missing one important setting to osascript -- setting an -s option to "s", otherwise you will be having difficulty parsing the output.
我不会嵌入 AppleScript,而是使用 appscript。我从未使用过 Python 版本,但 Ruby 版本非常好。 并确保,如果您在 Snow Leopard 上安装它,您拥有最新版本版本的 XCode。
但是,到目前为止我还无法在 Snow Leopard 上安装它。但我只使用 Snow Leopard 大约 1 天,所以您的里程可能会有所不同。Rather than embedding AppleScript, I would instead use appscript. I've never used the Python version, but it was very nice in Ruby. And make sure that, if you're installing it on Snow Leopard, you have the latest version of XCode.
However, I've so far been unable to install it on Snow Leopard. But I've only had Snow Leopard for ~1 day, so your mileage may vary.您可以使用 os.system:
或者,按照 Alex Martelli 的建议,您可以使用变量:
You can use
os.system
:or, as suggested by Alex Martelli you can use a variable:
这是 python 中的通用函数。只需传递带/不带参数的 applescript 代码,然后以字符串形式返回值。感谢这个答案。
Here's a generic function in python. Just pass your applescript code with/without args and get back the value as a string. Thanks to this answer.