Preview Module Reference

New in version 0.0.1.

Control the macOS Preview application using JXA-like syntax.

Classes:

XAPreviewApplication(properties)

A class for managing and interacting with Preview.app.

XAPreviewDocument(properties)

A class for managing and interacting with documents in Preview.

XAPreviewDocumentList(properties[, filter])

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

XAPreviewWindow(properties)

A class for managing and interacting with Preview windows.

class PyXA.apps.Preview.XAPreviewApplication(properties)[source]

Bases: XASBApplication, XACanOpenPath, XACanPrintPath

A class for managing and interacting with Preview.app.

New in version 0.0.1.

Methods:

documents([filter])

Returns a list of documents matching the filter.

print(path[, show_prompt])

Opens the print dialog for the file at the given path, if the file can be opened in Preview.

Attributes:

frontmost

Whether Preview is the active application.

name

The name of the application.

version

The version of Preview.app.

documents(filter: dict | None = None) XAPreviewDocumentList[source]

Returns a list of documents matching the filter.

Example 1:

List all documents

>>> import PyXA
>>> app = PyXA.Application("Preview")
>>> print(app.documents())
<<class 'PyXA.apps.Preview.XAPreviewDocumentList'>['Example1.pdf', 'Example2.pdf']>

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

New in version 0.0.1.

property frontmost: bool

Whether Preview is the active application.

property name: str

The name of the application.

print(path: str | NSURL, show_prompt: bool = True)[source]

Opens the print dialog for the file at the given path, if the file can be opened in Preview.

Parameters:
  • path (Union[str, AppKit.NSURL]) – The path of the file to print.

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

New in version 0.0.1.

property version: str

The version of Preview.app.

class PyXA.apps.Preview.XAPreviewDocument(properties)[source]

Bases: XATextDocument, XAPrintable, XACloseable, XAClipboardCodable

A class for managing and interacting with documents in Preview.

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.

path

The document's file path.

properties

All properties of the document.

get_clipboard_representation() NSURL[source]

Gets a clipboard-codable representation of the document.

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

Returns:

The document’s file URL

Return type:

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.

property path: XAPath

The document’s file path.

print(print_properties: dict | None = None, show_dialog: bool = True) XAPreviewDocument[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:

XAPreviewDocument

New in version 0.0.4.

property properties: dict

All properties of the document.

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

Saves the document.

If a file path is provided, Preview will attempt to save the file with the specified file extension at that path. If automatic conversion fails, the document will be saved in its original file format. If no path is provided, the document is saved at the current path for the document.

Example 1:

Save a PDF (or any compatible document) as a PNG

>>> import PyXA
>>> app = PyXA.Application("Preview")
>>> doc = app.documents()[0] # A PDF
>>> # Save to Downloads to avoid permission errors
>>> doc.save("/Users/steven/Downloads/Example.png")

New in version 0.0.4.

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

Bases: XAList, 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 documents’s value for the property.

New in version 0.0.4.

Methods:

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()

properties()

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

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

When the clipboard content is set to a document, each documents’s file URL is added to the clipboard.

Returns:

The document’s file URL

Return type:

list[AppKit.NSURL]

New in version 0.0.8.

modified() list[str][source]
name() list[str][source]
path() list[XAPath][source]
properties() list[dict][source]
class PyXA.apps.Preview.XAPreviewWindow(properties)[source]

Bases: XASBWindow, XASBPrintable

A class for managing and interacting with Preview windows.

New in version 0.0.1.

Attributes:

document

The document currently displayed in the window.

floating

Whether the window floats.

modal

Whether the window is a modal view.

properties

All properties of the window.

titled

Whether the window has a title bar.

property document: XAPreviewDocument

The document currently displayed in the window.

property floating: bool

Whether the window floats.

property modal: bool

Whether the window is a modal view.

property properties: dict

All properties of the window.

property titled: bool

Whether the window has a title bar.