pasteboard

Item Provider

An Item Provider is an object holding data that can be loaded as multiple file types. It can be returned from item_provider() or from shortcuts_attachments(). When you copy text for example, it may have formatting. So an ItemProvider object can in this case retrieve the clipboard as plain text or as an rtf file containing the text format.

class pasteboard.ItemProvider(foundation_item_provider: None)

A bridge to Foundation’s NSItemProvider class. An ItemProvider object can load data as one or more type of data. Instances of this class are returned by item_provider() and shortcuts_attachments().

data(type_identifier: str) → bytes

Returns the data for the given type identifier as bytes.

Parameters:type_identifier – An UTI. Can be returned from get_type_identifiers().
get_file_path() → str

Returns the file path if the item is a file. If it returns None, you can load its content from data() or open()

get_suggested_name() → str

Returns the name of the file from which the receiver was created or None.

get_type_identifiers() → List[str]

Returns a list of type identifiers (UTI) that can be loaded.

open()

Opens the receiver as a file in 'rb' mode with the first item item returned by get_type_identifiers() as the type identifier. You must use this function with the with keyword:

with item_provider.open() as f:
    f.read()
pasteboard.item_provider() → pasteboard.ItemProvider

Returns an ItemProvider instance storing the data from the pasteboard, if there is any.

pasteboard.shortcuts_attachments() → List[pasteboard.ItemProvider]

If the script is running from Shortcuts, returns a list of files passed to the Attachments parameter.

Return type:List[ItemProvider]

Strings

Functions for working with strings.

pasteboard.string() → str

Returns the text contained in the pasteboard.

pasteboard.strings() → List[str]

Returns all strings contained in the pasteboard.

pasteboard.set_string(text: Union[str, List[str]])

Copies the given text to the pasteboard.

Parameters:text – A string or a list of strings.

Images

Functions for working with images (as PIL images).

pasteboard.image() → PIL.Image.Image

Returns the image contained in the pasteboard as PIL images.

pasteboard.images() → List[PIL.Image.Image]

Returns all images contained in the pasteboard as PIL images.

pasteboard.set_image(image: Union[PIL.Image.Image, List[PIL.Image.Image]])

Copies the given image to the pasteboard.

Parameters:image – A PIL image or a list of PIL images.

URLs

Functions for working with URLs.

pasteboard.url() → str

Returns the URL contained in the pasteboard as a string.

pasteboard.urls() → List[str]

Returns all URLs contained in the pasteboard as strings.

pasteboard.set_url(url: Union[str, List[str]])

Copies the given URL to the pasteboard.

Parameters:url – A string or a list of strings.