如何从Python执行OS X应用程序(包)? [英] How do you execute an OS X application (bundle) from Python?

查看:60
本文介绍了如何从Python执行OS X应用程序(包)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在一个Nutshell的Python中,Alex Martelli展示了如何使用

os.spawnv(os.P_WAIT,编辑,[文本文件])

但是你怎么称呼OS X文本编辑器/Applications/TextEdit.app

- 看起来是整个目录/应用程序?


如果答案非常明显,我很抱歉。我一个人工作,有时只是卡住b $ b,然后不得不在公共场合询问风险

出现面条大脑。


David

For example, in Python in a Nutshell, Alex Martelli shows how you can
run a Windows (notepad.exe) or Unix-like (/bin/vim) text editor using
os.spawnv(os.P_WAIT, editor, [textfile])
But how would you call the OS X text editor /Applications/TextEdit.app
- which appears to be a whole directory inside /Applications?

I''m sorry if the answer is blindingly obvious. I work alone and
sometimes just get stuck, then have to ask in public and risk
appearing a noodle brain.

David

推荐答案

>>>>> df*@forestfield.co.uk (David Hughes)(DH)写道:


DH>例如,在果壳中的Python中,Alex Martelli展示了如何使用

DH>使用

DH>运行Windows(notepad.exe)或类Unix(/ bin / vim)文本编辑器。 os.spawnv(os.P_WAIT,编辑,[文本文件])

DH>但是你怎么称呼OS X文本编辑器/Applications/TextEdit.app

DH> - 它似乎是/ Applications中的整个目录?


DH>如果答案非常明显,我很抱歉。我独自工作,

DH>有时候只是卡住,然后不得不公开询问风险

DH>出现一个面条大脑。


os.system(" open -a TextEdit test.txt")



os .system(" /Applications/TextEdit.app/Contents/MacOS/TextEdit test.text")

我想你也可以把它翻译成产生的电话。

-

Piet van Oostrum< pi ** @ cs.uu.nl>

网址: http://www.cs.uu.nl/~piet [PGP]

私人电子邮件: P.***********@hccnet.nl
>>>>> df*@forestfield.co.uk (David Hughes) (DH) wrote:

DH> For example, in Python in a Nutshell, Alex Martelli shows how you can
DH> run a Windows (notepad.exe) or Unix-like (/bin/vim) text editor using
DH> os.spawnv(os.P_WAIT, editor, [textfile])
DH> But how would you call the OS X text editor /Applications/TextEdit.app
DH> - which appears to be a whole directory inside /Applications?

DH> I''m sorry if the answer is blindingly obvious. I work alone and
DH> sometimes just get stuck, then have to ask in public and risk
DH> appearing a noodle brain.

os.system("open -a TextEdit test.txt")
or
os.system("/Applications/TextEdit.app/Contents/MacOS/TextEdit test.text")

I suppose you can translate this also to spawn calls.
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.***********@hccnet.nl


David Hughes< df*@forestfield.co.uk>写道:
David Hughes <df*@forestfield.co.uk> wrote:
例如,在一个Nutshell的Python中,Alex Martelli展示了如何运行Windows(notepad.exe)或类Unix(/ bin / vim) )文本编辑器使用
os.spawnv(os.P_WAIT,编辑器,[文本文件])
但是如何调用OS X文本编辑器/Applications/TextEdit.app
- 这似乎是是一个完整的目录/应用程序?

如果答案非常明显,我很抱歉。我一个人工作,有时候只是卡住,然后不得不公开询问并冒险出现面条大脑。
For example, in Python in a Nutshell, Alex Martelli shows how you can
run a Windows (notepad.exe) or Unix-like (/bin/vim) text editor using
os.spawnv(os.P_WAIT, editor, [textfile])
But how would you call the OS X text editor /Applications/TextEdit.app
- which appears to be a whole directory inside /Applications?

I''m sorry if the answer is blindingly obvious. I work alone and
sometimes just get stuck, then have to ask in public and risk
appearing a noodle brain.




你可以产生''open''命令(/ usr / bin / open),你也用它来打开OSX终端的各种文件。
打开各种文件。不幸的是,在这种情况下,P_WAIT

无法以预期的方式工作 - 它不等待

应用程序关闭(以便用户已完成编辑

文件),但它可以让你立刻控制。


不幸的是,我不知道如何获得P_WAIT OSX上的功能

用于.app目录/ bundle - 你可能想在pythonmac-sig上询问!

Alex



You could spawn the ''open'' command (/usr/bin/open) which you also use to
open all kinds of files from OSX''s Terminal. Unfortunately, the P_WAIT
doesn''t work in the intended way in this case -- it doesn''t wait for the
application to be closed (so that the user has finished editing the
file), but rather it gives you control at once.

Unfortunately, I don''t know how to get the P_WAIT functionality on OSX
for a .app directory/bundle -- you may want to ask on the pythonmac-sig!
Alex


df*@forestfield.co.uk (David Hughes)在留言中写道:< 70 **** *********************@posting.google.c om> ...
df*@forestfield.co.uk (David Hughes) wrote in message news:<70*************************@posting.google.c om>...
例如,在果壳中的Python中,Alex Martelli展示了如何使用
os.spawnv(os.P_WAIT,editor,[textfile])运行Windows(notepad.exe)或类Unix(/ bin / vim)文本编辑器。但是你怎么称呼OS X文本编辑器/Applications/TextEdit.app
- 这似乎是一个完整的指导里面/应用程序?
For example, in Python in a Nutshell, Alex Martelli shows how you can
run a Windows (notepad.exe) or Unix-like (/bin/vim) text editor using
os.spawnv(os.P_WAIT, editor, [textfile])
But how would you call the OS X text editor /Applications/TextEdit.app
- which appears to be a whole directory inside /Applications?




使用os.system执行open非常简单,因为其他人已经指出了
。另一种方法是使用Apple事件,Mac GUI应用程序使用的标准

高级IPC系统。目前标准库中的AE支持

还有一些不足之处,需要更换

。很多改进,虽然未完成,AE支持可用

来自我的网站:

http://freespace.virgin.net/hamish.s...appscript.html


最快打开文档的方法是通过较低级别的aem包

(较低的开销,但你必须使用原始AE代码):

来自aem的
。发送导入应用程序

来自Carbon.File导入FSSpec

应用程序(''/ Applications / TextEdit.app'')。event(''aevt'',''odoc '',

{''----'':FSSpec(pathToFile)})。send()

或者,高级别的appscript包裹包装所有这些东西

类似于OO的语法糖和人类可读的术语(因为必须检索和解析应用程序需要更长时间才能初始化

术语):

来自appscript导入的
*

app(''TextEdit.app' ')。open(FSSpec(pathToFile))

HTH



Using os.system to execute open is pretty simple, as other folks have
pointed out. The other way is to use Apple events, the standard
high-level IPC system used by Mac GUI apps. The AE support currently
in the standard library leaves something to be desired and is due for
replacement. Much improved, though unfinished, AE support is available
from my site:

http://freespace.virgin.net/hamish.s...appscript.html

Fastest way to open a document is via the lower-level aem package
(lower overheads, though you have to use raw AE codes):

from aem.send import Application
from Carbon.File import FSSpec
Application(''/Applications/TextEdit.app'').event(''aevt'', ''odoc'',
{''----'':FSSpec(pathToFile)}).send()
Alternatively, the high-level appscript package wraps all this stuff
in OO-like syntactic sugar and human-readable terminology (takes
longer to initialise as it has to retrieve and parse the application
terminology):

from appscript import *
app(''TextEdit.app'').open(FSSpec(pathToFile))
HTH


这篇关于如何从Python执行OS X应用程序(包)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆