1

I'm working on a Kate plugin written in Python that generates a large amount of text too big to display it in a popup. So I want Kate to open a new unnamed file and display the text in it.

Is there a way to do this in Python (apart from running a subprocess echo text | kate --stdin)?

4

2 回答 2

1

我自己发现了:

import kate
from kate import documentManager as dm
from PyKDE4.kdecore import KUrl


text = "Lorem ipsum dolor sit amet"

# Open a new empty document
doc = dm.openUrl(KUrl())
# Open an existing file
doc = dm.openUrl(KUrl('/path/to/file.ext'))

# Activate view
kate.application.activeMainWindow().activateView(doc)

# Insert text
pos = kate.activeView().cursorPosition()
doc.insertText(pos, text)
于 2013-07-26T19:10:09.123 回答
0

您可以直接使用管道:

>>> f = os.popen("kate --stdin", "w")
>>> f.write("toto")
>>> f.close()

现在 kate 打开一个包含“toto”的文件。

更现代的解决方案是使用subprocess模块:

>>> sp = subprocess.Popen(["/usr/bin/kate", "--stdin"], stdin=subprocess.PIPE, shell=False)
>>> sp.stdin.write("toto")
>>> sp.stdin.close()

正如命令中所指定的,它不使用 shell。

于 2013-07-25T21:51:50.593 回答