API Docs

A configured sharing service, wrapping NSSharingService.

Create instances via hs.sharing.createShare() or hs.sharing.servicesFor(). Configure with setCallback(), recipients, and subject as needed, then call shareItems().

  • Example:
hs.sharing.createShare(hs.sharing.builtinServices.mail)
    .setCallback((event, items, error) => {
        if (event === 'didShare') console.log('Shared!')
        else if (event === 'didFail') console.error('Failed: ' + error)
    })
    .shareItems(['Check this out', 'https://www.hammerspoon.org'])

Properties

identifier

string

A unique identifier for this share object (UUID string).

title

string

The user-visible title of the service, e.g. "Mail" or "AirDrop".

alternateImage

HSImage

An alternate icon for the service, if one is provided, otherwise null.

recipients

string[]

Recipients (e.g. email addresses) for services that support them, such as Mail or Messages.

subject

string

The subject line, for services that support one, such as Mail.

messageBody

string

The message body, populated once the share is in progress. Empty until then.

accountName

string

The account name used to perform the share, if applicable. Populated once the share is in progress; otherwise null.

attachments

string[]

File paths of any attachments included in the share, populated once the share completes. Empty until then.

Methods

canShareItems(items) -> boolean

Checks whether this service can share the given items.

Items may be strings (treated as a web/mailto URL if they parse as one, a file path if they start with / or ~ and the file exists, otherwise plain text) or HSImage objects.

canShareItems(items) -> boolean
Name Type Description
items any[] The items to check
boolean
true if this service can share all of the given items
Unsupported items will be ignored
if (share.canShareItems(['hello'])) { ... }

shareItems(items) -> boolean

Attempts to share the given items with this service.

If the service cannot handle the items, this logs a warning and returns false without doing anything further. Otherwise the share is started; it is asynchronous — use setCallback() to find out when it completes.

shareItems(items) -> boolean
Name Type Description
items any[] The items to share
boolean
true if the share was started
Unsupported items will be ignored. A share that has started, and its callback, stay alive until it succeeds or fails, even if you don't keep a reference to it
share.shareItems(['Check this out', 'https://www.hammerspoon.org'])

setCallback(fn) -> HSSharingService

Registers a callback for share lifecycle events.

The callback is called with:

  • event (string): one of "willShare", "didShare", "didFail"
  • items (array): the items involved
  • error (string, "didFail" only): a description of the failure
setCallback(fn) -> HSSharingService
Name Type Description
fn function Called with the lifecycle event name, items, and optional error message
HSSharingService
this share object, for chaining
share.setCallback((event, items, error) => console.log(event))