J'essaie d'intégrer un AppleScript dans un script Python. Je ne veux pas avoir à sauvegarder l'AppleScript en tant que fichier, puis le charger dans mon script Python. Est-il possible d'entrer l'AppleScript en tant que chaîne en Python et Python peut-il exécuter l'AppleScript? Merci beaucoup.Comment intégrer un AppleScript dans un script Python?
Voici mon 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()
Pour votre information, il y a un petit module python sur PyPI qui font l'affaire: http://pypi.python.org/pypi/python-applescript/0.1 – ohe