我试图在Apple脚本中嵌入AppleScript.我不想将AppleScript保存为文件,然后将其加载到我的Python脚本中.有没有办法在Apple中将AppleScript作为字符串输入并让Python执行AppleScript?谢谢一堆.
这是我的脚本: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()
Run Code Online (Sandbox Code Playgroud) 有一个 AppleScript 方法:
on displayError(theErrorMessage)
display dialog theErrorMessage
return "done"
end displayError
Run Code Online (Sandbox Code Playgroud)
我想通过传递参数来编译这个脚本(不要用 osascript 运行它!)My_Application.app
就像是
osacompile - o My_Application.app My_Script.applescript "This is error message as parameter"
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我将编译可以运行的应用程序。寻找有关如何使用传递参数准确编译脚本的命令。由于编译需要很多时间 - 我只想做一个。运行My_Application.app后,比通过 osascript运行的速度要快很多倍。如果输入参数改变 - 只需重新编译应用程序。
一个不错的选择是从运行的应用程序中以某种方式收集返回值,但这是另一个问题