本文介绍了在不使用 osascript 或 appscript 的情况下从 Python 调用 AppleScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以在不使用 osascript 命令行实用程序或 appscript(我真的不想使用(我认为?)因为 不再开发/支持/推荐)?

基本原理:在 我刚刚发布的另一个问题,我描述了我在通过 osascript 运行一些 AppleScript 时遇到的奇怪/不受欢迎的行为.当我实际上是从 python 脚本调用它时,我想知道是否有办法完全绕过 osascript,因为这似乎是问题所在——但是 appscript(明显的选择?)现在看起来很危险...

解决方案

你可以使用 PyObjC 桥:

>>>从基金会进口 *>>>s = NSAppleScript.alloc().initWithSource_("告诉应用\"Finder\"激活")>>>s.executeAndReturnError_(无)

Is there any way to execute (and obtain the results of) AppleScript code from python without using the osascript command-line utility or appscript (which I don't really want to use (I think?) because it's no longer developed/supported/recommended)?

Rationale: in another question I've just posted, I describe a strange/undesired behaviour I'm experiencing with running some AppleScript via osascript. As I'm actually calling it from a python script, I wondered if there was a way to route around osascript altogether, since that seems to be where the problem lies - but appscript (the obvious choice?) looks risky now...

解决方案

You can use the PyObjC bridge:

>>> from Foundation import *
>>> s = NSAppleScript.alloc().initWithSource_("tell app \"Finder\" to activate")
>>> s.executeAndReturnError_(None)

这篇关于在不使用 osascript 或 appscript 的情况下从 Python 调用 AppleScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 10:11