docs » hs.sharing
Share items with the macOS Sharing Services under the control of Hammerspoon.
This module will allow you to share Hammerspoon items with registered Sharing Services. Some of the built-in sharing services include sharing through mail, Facebook, AirDrop, etc. Other applications can add additional services as well.
For most sharing services (this has not been tested with all), the user will be prompted with the standard sharing dialog showing what is to be shared and offered a chance to submit or cancel.
This example prepares an email with a screenshot:
mailer = hs.sharing.newShare("com.apple.share.Mail.compose")
mailer:subject("Screenshot generated at " .. os.date()):recipients({ "[email protected]" })
mailer:shareItems({ [[
Add any notes that you wish to add describing the screenshot here and click the Send icon when you are ready to send this
]], hs.screen.mainScreen():snapshot() })
Common item data types that can be shared with Sharing Services include (but are not necessarily limited to):
- basic data types like strings and numbers
- hs.image objects
- hs.styledtext objects
- web sites and other URLs through the use of the hs.sharing.URL function
- local files through the use of file URLs created with the hs.sharing.fileURL function
- Constants - Useful values which cannot be changed
- builtinSharingServices
- Functions - API calls offered directly by the extension
- fileURL
- shareTypesFor
- URL
- Constructors - API calls which return an object, typically one that offers API methods
- newShare
- Methods - API calls which can only be made on an object returned by a constructor
- accountName
- alternateImage
- attachments
- callback
- canShareItems
- image
- messageBody
- permanentLink
- recipients
- serviceName
- shareItems
- subject
- title
Signature | hs.sharing.builtinSharingServices[] |
---|---|
Type | Constant |
Description | A table containing the predefined sharing service labels defined by Apple. |
Signature | hs.sharing.fileURL(path) -> table |
---|---|
Type | Function |
Description | Returns a table representing a file URL for the path specified. |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.sharing.shareTypesFor(items) -> identifiersTable |
---|---|
Type | Function |
Description | Returns a table containing the sharing service identifiers which can share the items specified. |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.sharing.URL(URL, [fileURL]) -> table |
---|---|
Type | Function |
Description | Returns a table representing the URL specified. |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.sharing.newShare(type) -> sharingObject |
---|---|
Type | Constructor |
Description | Creates a new sharing object of the type specified by the identifier provided. |
Parameters |
|
Returns |
|
| Signature | hs.sharing:accountName() -> string | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | The account name used by the sharing service when posting on Twitter or Sina Weibo. |
| Parameters |
- None
- a string containing the account name used by the sharing service, or nil if the sharing service does not provide this.
- According to the Apple API documentation, only the Twitter and Sina Weibo sharing services will set this property, but this has not been fully tested.
| Signature | hs.sharing:alternateImage() -> hs.image object | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns an alternate image, if one exists, representing the sharing service provided by this sharing object. |
| Parameters |
- None
- an hs.image object or nil, if no alternate image representation for the sharing service is defined.
| Signature | hs.sharing:attachments() -> table | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | If the sharing service provides an array of the attachments included when the data was posted, this method will return an array of file URL tables of the attachments. |
| Parameters |
- None
- an array (table) containing the attachment file URLs, or nil if the sharing service selected does not provide this.
- not all sharing services will set a value for this property.
Signature | hs.sharing:callback(fn) -> sharingObject |
---|---|
Type | Method |
Description | Set or clear the callback for the sharingObject. |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.sharing:canShareItems(items) -> boolean |
---|---|
Type | Method |
Description | Returns a boolean specifying whether or not all of the items specified can be shared with the sharing service represented by the sharingObject. |
Parameters |
|
Returns |
|
| Signature | hs.sharing:image() -> hs.image object | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Returns an image, if one exists, representing the sharing service provided by this sharing object. |
| Parameters |
- None
- an hs.image object or nil, if no image representation for the sharing service is defined.
| Signature | hs.sharing:messageBody() -> string | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | If the sharing service provides the message body that was posted when sharing has completed, this method will return the message body as a string. |
| Parameters |
- None
- a string containing the message body, or nil if the sharing service selected does not provide this.
- not all sharing services will set a value for this property.
| Signature | hs.sharing:permanentLink() -> URL table | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | If the sharing service provides a permanent link to the post when sharing has completed, this method will return the corresponding URL. |
| Parameters |
- None
- the URL for the permanent link, or nil if the sharing service selected does not provide this.
- not all sharing services will set a value for this property.
| Signature | hs.sharing:recipients([recipients]) -> current value | sharingObject
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Get or set the subject to be used when the sharing service performs its sharing method. |
| Parameters |
- recipients - an optional array (table) or list of recipient strings separated by commas which specify the recipients of the shared items.
- if an argument is provided, returns the sharingObject; otherwise returns the current value.
- not all sharing services will make use of the value set by this method.
- the individual recipients should be specified as strings in the format expected by the sharing service; e.g. for items being shared in an email, the recipients should be email address, etc.
Signature | hs.sharing:serviceName() -> string |
---|---|
Type | Method |
Description | The service identifier for the sharing service represented by the sharingObject. |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.sharing:shareItems(items) -> sharingObject |
---|---|
Type | Method |
Description | Shares the items specified with the sharing service represented by the sharingObject. |
Parameters |
|
Returns |
|
Notes |
|
| Signature | hs.sharing:subject([subject]) -> current value | sharingObject
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Get or set the subject to be used when the sharing service performs its sharing method. |
| Parameters |
- subject - an optional string specifying the subject for the posting of the shared content
- if an argument is provided, returns the sharingObject; otherwise returns the current value.
- not all sharing services will make use of the value set by this method.
Signature | hs.sharing:title() -> string |
---|---|
Type | Method |
Description | The title for the sharing service represented by the sharingObject. |
Parameters |
|
Returns |
|
Notes |
|