Skip to content

Selection

src.helpers.selection.select_bounds(bounds: tuple[int, int, int, int], selection: Optional[Selection] = None) -> None

Create a selection using a list of bound values.

Parameters:

Name Type Description Default
bounds tuple[int, int, int, int]

List of bound values (left, top, right, bottom).

required
selection Selection | None

App selection object, pull from active document if not provided.

None
Source code in src\helpers\selection.py
def select_bounds(
    bounds: tuple[int, int, int, int],
    selection: Optional[Selection] = None
) -> None:
    """Create a selection using a list of bound values.

    Args:
        bounds: List of bound values (left, top, right, bottom).
        selection: App selection object, pull from active document if not provided.
    """
    selection = selection or APP.activeDocument.selection
    left, top, right, bottom = bounds
    selection.select([
        [left, top],
        [right, top],
        [right, bottom],
        [left, bottom]])

src.helpers.selection.select_layer_bounds(layer: ArtLayer = None, selection: Optional[Selection] = None) -> None

Select the bounding box of a given layer.

Parameters:

Name Type Description Default
layer ArtLayer

Layer to select the pixels of. Uses active layer if not provided.

None
selection Selection | None

App selection object, pull from active document if not provided.

None
Source code in src\helpers\selection.py
def select_layer_bounds(layer: ArtLayer = None, selection: Optional[Selection] = None) -> None:
    """Select the bounding box of a given layer.

    Args:
        layer: Layer to select the pixels of. Uses active layer if not provided.
        selection: App selection object, pull from active document if not provided.
    """
    if not layer:
        layer = APP.activeDocument.activeLayer
    select_bounds(layer.bounds, selection)

src.helpers.selection.select_overlapping(layer: ArtLayer) -> None

Select pixels in the given layer overlapping the current selection.

Parameters:

Name Type Description Default
layer ArtLayer

Layer with pixels to select.

required
Source code in src\helpers\selection.py
def select_overlapping(layer: ArtLayer) -> None:
    """Select pixels in the given layer overlapping the current selection.

    Args:
        layer: Layer with pixels to select.
    """
    with suppress(PS_EXCEPTIONS):
        idChannel = sID('channel')
        desc1, ref1, ref2 = ActionDescriptor(), ActionReference(), ActionReference()
        ref1.putEnumerated(idChannel, idChannel, sID("transparencyEnum"))
        ref1.putIdentifier(sID("layer"), layer.id)
        desc1.putReference(sID("target"), ref1)
        ref2.putProperty(idChannel, sID("selection"))
        desc1.putReference(sID("with"), ref2)
        APP.executeAction(sID("interfaceIconFrameDimmed"), desc1, NO_DIALOG)

src.helpers.selection.select_canvas(docref: Optional[Document] = None, bleed: int = 0)

Select the entire canvas of a provided or active document.

Parameters:

Name Type Description Default
docref Document | None

Document reference, use active if not provided.

None
bleed int

Amount of bleed edge to leave around selection, defaults to 0.

0
Source code in src\helpers\selection.py
def select_canvas(docref: Optional[Document] = None, bleed: int = 0):
    """Select the entire canvas of a provided or active document.

    Args:
        docref: Document reference, use active if not provided.
        bleed: Amount of bleed edge to leave around selection, defaults to 0.
    """
    docref = docref or APP.activeDocument
    docref.selection.select([
        [0 + bleed, 0 + bleed],
        [docref.width - bleed, 0 + bleed],
        [docref.width - bleed, docref.height - bleed],
        [0 + bleed, docref.height - bleed]
    ])

src.helpers.selection.select_layer_pixels(layer: Optional[ArtLayer] = None) -> None

Select pixels of the active layer, or a target layer.

Parameters:

Name Type Description Default
layer ArtLayer | None

Layer to select. Uses active layer if not provided.

None
Source code in src\helpers\selection.py
def select_layer_pixels(layer: Optional[ArtLayer] = None) -> None:
    """Select pixels of the active layer, or a target layer.

    Args:
        layer: Layer to select. Uses active layer if not provided.
    """
    if layer and layer.kind == LayerKind.SolidFillLayer:
        return select_vector_layer_pixels(layer)
    des1 = ActionDescriptor()
    ref1 = ActionReference()
    ref2 = ActionReference()
    ref1.putProperty(sID("channel"), sID("selection"))
    des1.putReference(sID("target"), ref1)
    ref2.putEnumerated(sID("channel"), sID("channel"), sID("transparencyEnum"))
    if layer:
        ref2.putIdentifier(sID("layer"), layer.id)
    des1.putReference(sID("to"), ref2)
    APP.executeAction(sID("set"), des1, NO_DIALOG)

src.helpers.selection.select_vector_layer_pixels(layer: Optional[ArtLayer] = None) -> None

Select pixels of the active vector layer, or a target layer.

Parameters:

Name Type Description Default
layer ArtLayer | None

Layer to select. Uses active layer if not provided.

None
Source code in src\helpers\selection.py
def select_vector_layer_pixels(layer: Optional[ArtLayer] = None) -> None:
    """Select pixels of the active vector layer, or a target layer.

    Args:
        layer: Layer to select. Uses active layer if not provided.
    """
    desc1 = ActionDescriptor()
    ref1 = ActionReference()
    ref2 = ActionReference()
    ref1.putProperty(sID("channel"), sID("selection"))
    desc1.putReference(sID("target"), ref1)
    ref2.putEnumerated(sID("path"), sID("path"), sID("vectorMask"))
    if layer:
        ref2.putIdentifier(sID("layer"), layer.id)
    desc1.putReference(sID("to"), ref2)
    desc1.putInteger(sID("version"), 1)
    desc1.putBoolean(sID("vectorMaskParams"), True)
    APP.executeAction(sID("set"), desc1, NO_DIALOG)

src.helpers.selection.check_selection_bounds(selection: Optional[Selection] = None) -> Optional[tuple[int, int, int, int]]

Verifies if a selection has valid bounds.

Parameters:

Name Type Description Default
selection Selection | None

Selection object to test, otherwise use current selection of active document.

None

Returns:

Type Description
tuple[int, int, int, int] | None

An empty list if selection is invalid, otherwise return bounds of selection.

Source code in src\helpers\selection.py
def check_selection_bounds(selection: Optional[Selection] = None) -> Optional[tuple[int, int, int, int]]:
    """Verifies if a selection has valid bounds.

    Args:
        selection: Selection object to test, otherwise use current selection of active document.

    Returns:
        An empty list if selection is invalid, otherwise return bounds of selection.
    """
    selection = selection or APP.activeDocument.selection
    with suppress(PS_EXCEPTIONS):
        if selection.bounds != (0, 0, 0, 0):
            return selection.bounds
    return