TextEdit Module Reference

New in version 0.0.1.

Control the macOS TextEdit application using JXA-like syntax.

Classes:

XATextEditApplication(properties)

A class for managing and interacting with TextEdit.app.

XATextEditDocument(properties)

A class for managing and interacting with TextEdit documents.

XATextEditDocumentList(properties[, filter])

A wrapper around lists of documents that employs fast enumeration techniques.

XATextEditWindow(properties)

A class for managing and interacting with TextEdit windows.

class PyXA.apps.TextEdit.XATextEditApplication(properties)[source]

Bases: XASBApplication, XACanOpenPath, XACanPrintPath

A class for managing and interacting with TextEdit.app.

New in version 0.0.1.

Classes:

ObjectType(value)

Types of objects that can be created with make().

Methods:

documents([filter])

Returns a list of documents matching the filter.

make(specifier, properties[, data])

Creates a new element of the given specifier class without adding it to any list.

new_document([name, text, location])

Creates a new document with the given name and initializes it with the supplied text.

open(path)

Opens the file/website at the given filepath/URL.

print(file[, print_properties, show_prompt])

Prints a TextEdit document.

Attributes:

frontmost

Whether TextEdit is the active application.

name

The name of the application.

version

The version of the TextEdit application.

class ObjectType(value)[source]

Bases: Enum

Types of objects that can be created with make().

Attributes:

DOCUMENT

WINDOW

DOCUMENT = 'document'
WINDOW = 'window'
documents(filter: dict | None = None) XATextEditDocumentList[source]

Returns a list of documents matching the filter.

Parameters:

filter (dict) – A dictionary specifying property-value pairs that all returned documents will have

Returns:

The list of documents

Return type:

list[XATextEditDocument]

Example 1:

Listing all documents

>>> import PyXA
>>> app = PyXA.Application("TextEdit")
>>> print(list(app.documents()))
[<<class 'PyXA.apps.TextEdit.XATextEditDocument'>Current Document.txt>, <<class 'PyXA.apps.TextEdit.XATextEditDocument'>Another Document.txt>, ...]
Example 2:

List documents after applying a filter

>>> import PyXA
>>> app = PyXA.Application("TextEdit")
>>> print(list(app.documents({"name": "Another Document.txt"})))
[<<class 'PyXA.apps.TextEdit.XATextEditDocument'>Another Document.txt>]
Example 3:

List all paragraphs, words, and characters in all currently open documents

>>> import PyXA
>>> app = PyXA.Application("TextEdit")
>>> documents = app.documents()
>>> print("Paragraphs:", documents.paragraphs())
>>> print("Words:", documents.words())
>>> print("Characters:", documents.characters())
Paragraphs: [This is note 1
, This is note 2
, This is note 3
]
Words: [This, is, note, 1, This, is, note, 2, This, is, note, 3]
Characters: [T, h, i, s,  , i, s, , n, o, t, e,  , 1,
, T, h, i, s, , i, s, , n, o, t, e, , 2,
, T, h, i, s, , i, s, , n, o, t, e, , 3,
]

Changed in version 0.0.4: Now returns an object of XATextEditDocumentList instead of a default list.

New in version 0.0.1.

property frontmost: bool

Whether TextEdit is the active application.

make(specifier: str | ObjectType, properties: dict, data: Any | None = None)[source]

Creates a new element of the given specifier class without adding it to any list.

Use XABase.XAList.push() to push the element onto a list.

Parameters:
  • specifier (Union[str, XATextEditApplication.ObjectType]) – The classname of the object to create

  • properties (dict) – The properties to give the object

  • data (Any, optional) – The data to initialize the object with, defaults to None

Returns:

A PyXA wrapped form of the object

Return type:

XABase.XAObject

Example 1:

Make a new document and push it onto the list of documents

>>> import PyXA
>>> app = PyXA.Application("TextEdit")
>>> properties = {
>>>     "name": "Example.txt",
>>>     "path": "/Users/exampleuser/Downloads/Example.txt",
>>>     "text": "Some example text"
>>> }
>>> new_doc = app.make("document", properties)
>>> app.documents().push(new_doc)

New in version 0.0.3.

property name: str

The name of the application.

new_document(name: str | None = 'Untitled.txt', text: str | None = '', location: str | None = None) XATextEditDocument[source]

Creates a new document with the given name and initializes it with the supplied text. If no location is provided, the document file is created in the user’s Documents folder.

Parameters:
  • name (Union[str, None], optional) – The name (including file extension) of the document, defaults to “Untitled.txt”

  • text (Union[str, None], optional) – The initial text of the document, defaults to “”

  • location (Union[str, None]) – The containing folder of the new document, defaults to None.

Returns:

A reference to the newly created document.

Return type:

XATextEditDocument

Example 1:

Create a new document with a name and initial body content

>>> import PyXA
>>> app = PyXA.Application("TextEdit")
>>> doc = app.new_document("New.txt", "Example text")
>>> print(doc.properties)
{
    modified = 0;
    name = "New.txt";
    objectClass = "<NSAppleEventDescriptor: 'docu'>";
    path = "/Users/exampleuser/Documents/New.txt";
    text = "Example text";
}

New in version 0.0.1.

open(path: str) XATextEditDocument[source]

Opens the file/website at the given filepath/URL.

Child classes of XACanOpenPath should redefine this method as necessary.

Parameters:

target (str) – The path to a file or the URL to a website to open.

Returns:

A reference to the opened document or element, or None if no document/element was created or it cannot be found

Return type:

Any

New in version 0.0.1.

print(file: str | NSURL | XATextEditDocument, print_properties: dict | None = None, show_prompt: bool = True)[source]

Prints a TextEdit document.

Parameters:
  • file (Union[str, AppKit.NSURL, XATextEditDocument]) – The document or path to a document to print

  • print_properties (dict, optional) – Settings to print with or to preset in the print dialog, defaults to None

  • show_prompt (bool, optional) – Whether to show the print dialog, defaults to True

Example 1:

Printing a document with print properties

>>> import PyXA
>>> from datetime import datetime, timedelta
>>> app = PyXA.Application("TextEdit")
>>> doc = app.documents()[0]
>>> print_time = datetime.now() + timedelta(minutes=1)
>>> properties = {
>>>     "copies": 3,
>>>     "collating": False,
>>>     "startingPage": 1,
>>>     "endingPage": 10,
>>>     "pagesAcross": 3,
>>>     "pagesDown": 3,
>>>     "requestedPrintTime": print_time,
>>>     "errorHandling": app.PrintErrorHandling.DETAILED.value,
>>>     "faxNumber": "",
>>>     "targetPrinter": ""
>>> }
>>> app.print(doc, print_properties=properties)

New in version 0.0.3.

property version: str

The version of the TextEdit application.

class PyXA.apps.TextEdit.XATextEditDocument(properties)[source]

Bases: XATextDocument, XAPrintable, XAClipboardCodable, XACloseable

A class for managing and interacting with TextEdit documents.

Changed in version 0.0.2: Added close(), save(), and copy()

New in version 0.0.1.

Methods:

get_clipboard_representation()

Gets a clipboard-codable representation of the document.

print([print_properties, show_dialog])

Prints the document.

save([file_path])

Saves the document.

Attributes:

modified

Whether the document has been modified since the last save.

name

The name of the document, including the file extension.

path

The path at which the document is stored.

properties

All properties of the document.

get_clipboard_representation() list[str | NSURL][source]

Gets a clipboard-codable representation of the document.

When the clipboard content is set to a document, the documents’s file URL and body text are added to the clipboard.

Returns:

The document’s file URL and body text

Return type:

list[Union[str, AppKit.NSURL]]

New in version 0.0.8.

property modified: bool

Whether the document has been modified since the last save.

property name: str

The name of the document, including the file extension.

property path: XAPath

The path at which the document is stored.

print(print_properties: dict | None = None, show_dialog: bool = True) XATextEditDocument[source]

Prints the document.

Parameters:
  • print_properties (Union[dict, None], optional) – Properties to set for printing, defaults to None

  • show_dialog (bool, optional) – Whether to show the print dialog, defaults to True

Returns:

The document object

Return type:

XATextEditDocument

New in version 0.0.8.

property properties: dict

All properties of the document.

save(file_path: str | XAPath | None = None)[source]

Saves the document.

If a file path is provided, TextEdit will attempt to create a new file at the target location and of the specified file extension. If no file path is provided, and the document does not have a current path on the disk, a save dialog for the document will open.

Parameters:

file_path (str, optional) – The path to save the document at, defaults to None

Example 1:

Save all currently open documents

>>> import PyXA
>>> app = PyXA.Application("TextEdit")
>>> for doc in app.documents():
>>>     doc.save()

New in version 0.0.2.

class PyXA.apps.TextEdit.XATextEditDocumentList(properties: dict, filter: dict | None = None)[source]

Bases: XATextDocumentList, XAClipboardCodable

A wrapper around lists of documents that employs fast enumeration techniques.

All properties of documents can be called as methods on the wrapped list, returning a list containing each document’s value for the property.

New in version 0.0.3.

Methods:

append(text)

Appends the provided text to the end of every document in the list.

by_modified(modified)

by_name(name)

by_path(path)

by_properties(properties)

get_clipboard_representation()

Gets a clipboard-codable representation of each document in the list.

modified()

name()

path()

prepend(text)

Inserts the provided text at the beginning of every document in the list.

properties()

push(*documents)

Appends the document to the list.

reverse()

Reverses the text of every document in the list.

append(text: str) XATextEditDocumentList[source]

Appends the provided text to the end of every document in the list.

Parameters:

text (str) – The text to append.

Returns:

A reference to the document object.

Return type:

XATextDocument

Example 1:

Append a string at the end of every open document

>>> import PyXA
>>> app = PyXA.Application("TextEdit")
>>> documents = app.documents()
>>> documents.append("\n\n-- End Of Notes --")

See also

prepend()

New in version 0.0.4.

by_modified(modified: bool) XATextEditDocument | None[source]
by_name(name: str) XATextEditDocument | None[source]
by_path(path: str | XAPath) XATextEditDocument | None[source]
by_properties(properties: dict) XATextDocument | None[source]
get_clipboard_representation() list[str | NSURL][source]

Gets a clipboard-codable representation of each document in the list.

When the clipboard content is set to a list of documents, each documents’s file URL and name are added to the clipboard.

Returns:

A list of each document’s file URL and name

Return type:

list[Union[str, AppKit.NSURL]]

New in version 0.0.8.

modified() list[str][source]
name() list[str][source]
path() list[XAPath][source]
prepend(text: str) XATextEditDocumentList[source]

Inserts the provided text at the beginning of every document in the list.

Parameters:

text (str) – The text to insert.

Returns:

A reference to the document object.

Return type:

XATextDocument

Example 1:

Prepend a string at the beginning of every open document

>>> import PyXA
>>> app = PyXA.Application("TextEdit")
>>> documents = app.documents()
>>> documents.prepend("-- PyXA Notes --\n\n")

See also

append()

New in version 0.0.4.

properties() list[dict][source]
push(*documents: list[XATextEditDocument]) XATextEditDocument | list[XATextEditDocument] | None[source]

Appends the document to the list.

New in version 0.1.1.

reverse() XATextEditDocumentList[source]

Reverses the text of every document in the list.

Returns:

A reference to the document object.

Return type:

XATextDocument

Example 1:

Reverse the text of every open document

>>> import PyXA
>>> app = PyXA.Application("TextEdit")
>>> documents = app.documents()
>>> documents.reverse()

New in version 0.0.4.

class PyXA.apps.TextEdit.XATextEditWindow(properties)[source]

Bases: XASBWindow, XASBPrintable

A class for managing and interacting with TextEdit windows.

New in version 0.0.1.

Attributes:

document

The active document.

floating

Whether the window floats.

modal

Whether the window is a modal window.

titled

Whether the window has a title bar.

property document: XATextEditDocument

The active document.

property floating: bool

Whether the window floats.

property modal: bool

Whether the window is a modal window.

property titled: bool

Whether the window has a title bar.