| 1 |
# -*- coding: utf-8 -*- |
| 2 |
import os |
| 3 |
from dogtail import tree |
| 4 |
from dogtail.utils import run |
| 5 |
from dogtail.predicate import GenericPredicate |
| 6 |
|
| 7 |
|
| 8 |
def gedit_text_tree_api_demo(): |
| 9 |
os.environ["LANG"] = "en_US.UTF-8" |
| 10 |
|
| 11 |
# Remove the output file, if it's still there from a previous run |
| 12 |
if os.path.isfile(os.path.join("/tmp", "UTF8demo.txt")): |
| 13 |
os.remove(os.path.join("/tmp", "UTF8demo.txt")) |
| 14 |
|
| 15 |
# Start gedit. |
| 16 |
# Get a handle to gedit's application object. |
| 17 |
os.system("killall gedit") # preventing the undesired result if the sript is started multiple times in a row |
| 18 |
run("gedit") |
| 19 |
gedit = tree.root.application("gedit") |
| 20 |
|
| 21 |
# Get a handle to gedit's text object. |
| 22 |
text_buffer = gedit.findChildren(GenericPredicate(roleName="text"))[1] |
| 23 |
# is equal to |
| 24 |
# text_buffer = gedit.findChild(lambda x: x.roleName=="text" and x.showing) # we can specify search by another attribute - showing |
| 25 |
# Load the UTF-8 demo file. |
| 26 |
# Set the attribute to the given text object |
| 27 |
with open(os.path.abspath(".") + "/data/UTF-8-demo.txt", "r") as open_file: |
| 28 |
text_buffer.text = open_file.read() |
| 29 |
|
| 30 |
|
| 31 |
# Get a handle to gedit's Save button and click. |
| 32 |
save_button = gedit.findChild(lambda x: x.name == "Save" and x.roleName in ("button", "push button")) |
| 33 |
save_button.click() |
| 34 |
|
| 35 |
|
| 36 |
# We want to save to the file name 'UTF8demo.txt'. |
| 37 |
dialog = gedit.findChild(lambda x: "Save As" in x.name and x.roleName == "file chooser") |
| 38 |
dialog.child(roleName="text").text = "/tmp/UTF8demo.txt" |
| 39 |
|
| 40 |
|
| 41 |
# Get a handle to gedit's Save dialog and click. |
| 42 |
save_as_button = gedit.findChildren(lambda x: "Save" in x.name)[-1] |
| 43 |
save_as_button.click() |
| 44 |
|
| 45 |
|
| 46 |
# Let's quit now. |
| 47 |
gedit.child("Menu", "toggle button").click() |
| 48 |
# Sometimes the node is not unique so you need to specify another attribute to look for |
| 49 |
gedit.findChild(lambda x: x.name == "Close" and x.showing).click() |
| 50 |
|
| 51 |
|
| 52 |
gedit_text_tree_api_demo() |