• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

pilotmoon/PopClip-Extensions: Documentation and source code for PopClip extensio ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

pilotmoon/PopClip-Extensions

开源软件地址(OpenSource Url):

https://github.com/pilotmoon/PopClip-Extensions

开源编程语言(OpenSource Language):

JavaScript 72.1%

开源软件介绍(OpenSource Introduction):

PopClip Extensions

This document applies to PopClip 2022.5 (3895). See also: Changelog

NEW: Check the PopClip Forum to keep up-to date about extensions development, to ask questions, and to help others.

Table of Contents

Introduction

PopClip Extensions add extra actions to PopClip.

Screenshot showing extensions in use.

This repository contains the documentation for making your own extensions (this readme file) as well as the source files for the extensions published on the official PopClip Extensions page.

License

All extension source files are published under the MIT License (see LICENSE.txt) unless noted otherwise in the readme files of individual extensions.

Credits

All the extensions and documentation were created by Nick Moore, except where stated. Individual extension credits are included in a readme file with the extension.

Repository Layout & Contributing

The important folders:

  • source - sources for the published extensions (maintained and supported by me)
  • source-contrib - folder for user-submitted and experimental extensions
  • extensions - signed and zipped .popclipextz files built from the source and source-contrib folders

Bugfixes and new extension submissions are welcome via pull request. Please note the following:

  • New extensions should be submitted as source files in a .popclipext folder inside the source-contrib folder.
  • Please do not submit any zipped .popclipextz files.
  • By contributing to this repo, you agree that your contribution may be published at PopClip Extensions.
  • Submitting an extension does not guarantee publication on the website.
  • I may make changes to any extension submitted.

A good place to tell PopClip users about your work is the Show and Tell section in the PopClip forum.

Extension Signing

By default, PopClip will display a warning dialog when you try to install your own extension, because it is not digitally signed.

Example unsigned warning.

If you find this gets annoying while you are testing your work, you can turn off the warning. Run the following command at the Terminal, then Quit and restart PopClip:

defaults write com.pilotmoon.popclip LoadUnsignedExtensions -bool YES

Please be aware that PopClip extensions can contain arbitrary executable code. Be careful about the extensions you create, and be wary about loading extensions you get from someone else.

(PopClip will never load unsigned extensions whose identifier has a com.pilotmoon. prefix, which is reserved for 'official' extensions. When basing your own extension off such an extension, you will need to change the identifier.)

Debug Output

To help you when creating extensions, PopClip can be configured to write script output and debug info to be viewed with the Console app. To enable it, run this command in Terminal, then Quit and restart PopClip:

defaults write com.pilotmoon.popclip EnableExtensionDebug -bool YES

Extension Snippets

PopClip 2021.11 supports a new, simplified way to create and share simple extensions called "Extension Snippets".

Here is an example extension snippet:

# popclip extension to search Emojipedia
name: Emojipedia
icon: search filled E
url: https://emojipedia.org/search/?q=***

When you select the above text, PopClip will offer an "Install Extension" action. Clicking it will install the above extension directly, without any need for config files or a .popclipext folder.

The format of a snippet is a simply a regular PopClip extension config in YAML format, with the addition of a comment header beginning with # popclip (with or without a space, not case sensitive).

All features of regular extensions can be used, with the limitation that additional files (such as icon files or scripts) cannot be included. Extension snippets can be a maximum of 1000 characters.

If the extension is of type Shortcut, Service, URL, Key Combo or JavaScript (without network entitlement), the extension snippet will install without the usual "unsigned extension" prompt. AppleScript snippets, and JavaScript snippets with the network entitlement, will still give the unsigned warning.

Full Shell Script extensions can't be expressed as snippets, although you can use an AppleScript to run a simple shell script as a string literal (see example below).

In the absence of an explicit identifier field, the extension is identified by its name. Installing another extension with the same name (or identifier) will overwrite an existing one.

Extension Snippets Examples

A Shortcuts example:

# popclip shortcuts example
name: Run My Shortcut
icon: symbol:moon.stars # Apple SF Symbols
macos version: '12.0' # shortcuts only work on Monterey and above!
shortcut name: My Shortcut Name

An AppleScript example, running a shell script:

# popclip shellscript nested in an applescript 
name: Say
applescript: do shell script "say '{popclip text}'"

A multi-line AppleScript example (the pipe character begins a YAML multi-line string, and the following lines must all be indented with two spaces - not tabs!):

# PopClip LaunchBar example
name: LaunchBar
icon: LB
applescript: | # pipe character begins a multi-line string
  tell application "LaunchBar"
    set selection to "{popclip text}"
  end tell
# the above lines are indented with two spaces. no tabs allowed in YAML!

A JavaScript example, including multiple actions:

# popclip js + multi action example
name: Markdown Formatting
requirements: [text, paste]
actions:
- title: Markdown Bold # note: actions have a `title`, not a `name`
  icon: circle filled B
  javascript: popclip.pasteText('**' + popclip.input.text + '**')
- title: Markdown Italic
  icon: circle filled I
  javascript: popclip.pasteText('*' + popclip.input.text + '*')  

A Key Press example:

# popclip
name: Key Press Example
key combo: command option J

A Service example:

# popclip
name: Make Sticky
service name: Make Sticky

A more complex Key Combo example with a raw key code and using some more fields:

# popclip extension snippet - more complex example
name: Paste and Enter
icon: square monospaced ↵
requirements: [paste]
before: paste
key combo: 0x24 # see https://bit.ly/3wSkQ9I

Anatomy of a PopClip Extension

Types of Actions

There are seven kinds of actions supported by PopClip extensions.

Action Type Description Example
Shortcut Send the selected text to a macOS Shortcut. (Requires macOS 12.0 Monterey.) See snippets above.
Service Send the selected text to a macOS Service. MakeSticky
URL Open a URL, with the selected text URL-encoded and inserted. GoogleTranslate
Key Press Press a key combination. Delete
JavaScript Run a JavaScript script. Sort
AppleScript Run an AppleScript script. BBEdit
Shell Script Run a shell script. Say

Filtering

Extensions have access to the following filtering mechanisms, to help prevent actions appearing when they are not useful:

  • Filter by matching a regular expression.
  • Filter by application (either include or exclude).
  • Filter by whether cut, copy or paste is available.
  • Filter by whether the text contains a URL, email address or file path.
  • Filter by the current values of the extensions's options.

The .popclipext package

A PopClip extension consists of a configuration file called either Config.json, Config.yaml or Config.plist, plus (optional) additional files such as icons and scripts, all contained in a directory whose name ends with .popclipext. Such a directory will be treated as a package by Mac OS X. To view the contents of a package, right click it in Finder and choose 'Show Package Contents'.

If you double-click a .popclipext package, PopClip will attempt to load and install it. PopClip stores its installed extensions in ~/Library/Application Support/PopClip/Extensions/.

Here is an example package structure, the 'Say' extension:

    Say.popclipext                  -- Containing folder
       _Signature.plist             -- Signature (official Pilotmoon extensions only)
       Config.plist                 -- Main configuration file
       say.sh                       -- Script file
       speechicon.png               -- Icon file

About .popclipextz files

For distribution, an extension package folder may be zipped and renamed with the extension .popclipextz. You can examine an existing PopClip extension by renaming it with a .zip extension and unzipping it, to reveal a .popclipext package.

The Config file

Every extension must contain a Config file, in either JSON, YAML or plist format.

Format File Name Description
JSON Config.json A file in JSON format. (quickref)
YAML Config.yaml A file in YAML 1.2 format. (quickref)
plist Config.plist An Apple XML Property List.

The Config file must define a single dictionary at its root, which defines the extension. Although the three formats are different, they all can be used to define a dictionary mapping string keys to values. The values can be strings, numbers, booleans, arrays or other dictionaries. (In this documentation the term 'field' is used to refer to a key/value pair in a dictionary.)

The choice of format does not affect the extension functionality in any way, so you can choose whichever format you prefer to work with. (Plist was the original config file format for PopClip extensions for many years, and the JSON and YAML formats were added later.)

Field names

Field names in this document are given in a lowercase form with words separated by spaces, such as required apps. However, PopClip will treat field names in any of the following formats as equivalent:

  • Lowercase with spaces: e.g. required apps
  • Mixed case with spaces: e.g. Required Apps (this is the original naming format prior to PopClip 2021.11)
  • Camel case: e.g. requiredApps
  • Pascal case: e.g. RequiredApps
  • Snake case: e.g. required_apps
  • Kebab case: e.g. required-apps
  • Constant case: e.g. REQUIRED_APPS

Rules for transformation are as defined by case-anything, which PopClip uses.

Older versions of PopClip used different names for some fields. Where there is a new name, the old name is also still accepted. A table of old and new is given in Field name mapping.

Icons

Icons may be specified in the icon fields in a few different ways:

  • As a filename: <filename>.png or <filename>.svg specifies an image file within the extension package, in either PNG or SVG format. You can create your own with an image editor, or you could use icons from a website like The Noun Project or the macOS app IconJar. Please include any applicable copyright attribution in a README file.

  • As an SF Symbol: symbol:<symbol name> specifies an SF Symbols identifier, for example symbol:flame. Symbols are only available on macOS 11.0 and above. Also note that some symbols require higher macOS versions as indicated in the "Availability" panel in Apple's SF Symbols browser app. (If the symbol does not exist on the version of macOS the user is running, it will be as if no icon was specified. Therefore, you should specify an appropriate macos version when using a symbol icon.)

  • As a text-based icon: Using a special format, you can instruct PopClip to generate a text-based icon (see below).

PNG and SVG icons should be square and monochrome. The image should be black, on a transparent background. You can use opacity to create shading. PNG icons should be at least 256x256 pixels in size.

Text-based icons

Text-based icons can up to three characters, on their own or within an enclosing shape. They are specified by space-separated keywords followed by the characters to draw.

The following keywords define an enclosing shape (only one of these should be included):

Keyword Effect
text Draw the text on its own, without a shape. This is the default if no shape keyword is specified.
square Encloses text in a round-cornered square.
circle Encloses text in a circle.
search Encloses text in a magnifying glass shape.

The following keywords modify the way the text is drawn:

Keyword Effect
filled Specifies a solid filled shape (default is an outline shape).
monospaced Specifies that the text be drawn in a monospaced font (default is variable-width font). Punctuation characters often render better in a monospaced font.

Examples of symbols and text-based icons

Text String Icon Generated
symbol:flame
symbol:hand.raised
symbol:signpost.right
A or text A
circle 1
circle filled 本
square xyz
square filled !
square filled monospaced ()
search E
search filled monospaced £

As a handy tool, the extension IconPreview.popclipextz in this repo will display the icon for a text string you select.

The Config file structure

About the "Localizable String" type

Fields shown as "Localizable String" may be either a string or a dictionary. If you supply a string, that string is always used. If you supply a dictionary mapping language codes (en, fr, zh-hans, etc.) to a string, PopClip will display the string for the user's preferred language if possible, with fallback to the en string.

Extension properties

The following fields are used at the top level of the configuration to define properties of the extension itself. All fields are optional.

Key Type Description
name Localizable String This is a display name that appears in the preferences list of extensions. If omitted, a name is generated automatically from the .popclipext package name.
icon String See Icons. If you omit this field, the icon for the first action will be used (if any), or else no icon will be displayed.
identifier String You may provide an identifier string here to uniquely identify this extension. Use your own prefix, which could be a reverse DNS-style prefix based on a domain name you control com.example.myextension. (Do not use the prefix com.pilotmoon. for your own extensions.)If you omit this field, PopClip will identify the extension by its package name (e.g. Name.popclipext) instead.
description Localizable String A short, human readable description of this extension. Appears on the web site but not in the app.
macos version String Minimum version number of Mac OS X needed by this extension. For example 10.8.2 or 11.0.
popclip version Integer Minimum PopClip version required. This is the build number, as shown in brackets in the about pane. You should specify 3895 for new extensions based on this document.
options Array Array of dictionaries defining the options for this extension, if any. See The options array.
options title Localizable String Title to appear at the top of the options window. Default is Options for <extension name>.
entitlements Array Only applies to JavaScript extensions. The possible values are network (allows use of XMLHttpRequest) and dynamic (allows dynamically generated actions).
action Dictionary A dictionary defining a single action for this extension. See Action properties.
actions Array Array of dictionaries defining the actions for this extension.

If neither actions nor action is defined, PopClip will look at the top level of the plist for an action definition.

Action properties

The following fields define properties common to all actions. All fields are optional.

Key Type Required? Description
title Localizable String The title is displayed on the action button if there is no icon. For extensions with icons, the title is displayed in the tooltip. If omitted, the action will take the extension name as its title.
icon String The icon to show on the action button. See Icons for the icon specification format. To explicitly specify no icon, set this field either to boolean false (in a plist) or to null (in JSON/YAML).
identifier String A string to identify this action. In shell script and AppleScript actions, the identifier is passed to the script.
requirements Array Array consisting of zero or more of the strings listed in the requirements array. All the requirements in the array must be satisfied. If the array is omitted, the requirement text is applied by default.
before String String to indicate an action PopClip should take before performing the main action. See The before and after strings.
after String String to indicate an action PopClip should take after performing the main action. See The before and after strings.
excluded apps Array Array of bundle identifiers of applications. The action will not appear when PopClip is being used in any of the the specified apps.
required apps Array Array of bundle identifiers of applications. The action will only appear when PopClip is being used in one of the specified apps. Note: This field does not make PopClip do a check to see if the app is present on the computer. For that, use the App field.
regex String A Regular Expression to be applied to the selected text. The action will appear only if the text matches the regex, and the matching part of the text is passed to the action. The regex engine used is Cocoa's NSRegularExpression, which uses the ICU specification for regular expressions. Note: There is no need to use your own regex to match URLs, email addresses or file paths. Use one of the requirements keys url, urls, email, emails or path instead. Also be careful to avoid badly crafted regexes which never terminate against certain inputs.
app Dictionary Information about the app or website associated with this action. You can use this field to, optionally, specify that a certain app must be present on the system for the action to work. See The app dictionary.
stay visible Boolean If true, the PopClip popup will not disappear after the user clicks the action. (An example is the Formatting extension.) Default is false.
capture html Boolean If true, PopClip will attempt to capture HTML and Markdown for the selection. PopClip makes its best attempt to extract HTML, first of all from the selection's HTML source itself, if available. Failing that, it will convert any RTF text to HTML. And failing that, it will generate an HTML version of the plain text. It will then generate Markdown from the final HTML. Default is false.
preserve image color Boolean If true, the supplied icon will be displayed with its original color instead of being filled in white/black. Default is false.
restore pasteboard Boolean If true, then PopClip will restore the pasteboard to its previous contents after pasting text in the paste-result after-step. Default is false.

Additionally, there will be action-specific properties as described in the sections below.

Shortcut actions

In a Shortcut action, PopClip will invoke a Shortcut by name. Shortcuts are only available on macOS 12.0 and above.

A shortcut action is defined by the presence of a shortcut name field, as follows:

Key Type Description
shortcut name String The name of the macOS Shortcut to call. The name is whatever it is called in the Shortcuts app.

The selected text will be sent as input to the service, and any text returned by the shortcut will be available to the after action.

Service actions

In a Service action, PopClip will invoke a named macOS System Service, passing it the selected text.

A service action is defined by the presence of a service name field, as follows:

Key Type Description
service name String The name of the macOS service to call.

The name is as shown in the Services menu, for example Add to Deliveries. In some cases, you may need to look into the Info.plist of the application to find the name defined in there under NSServicesNSMenuItem. An example of this is the Make New Sticky Note service which must be called as Make Sticky.

URL actions

In a URL action, PopClip will open a URL in the default browser (or if the current app is a known browser, in the current app). PopClip can open any type of URL, not just web URLs.

A URL action is defined by the presence of a url field, as follows:

Key Type Description
url String The URL to open when the user clicks the action. Use {popclip text} as placeholder for the selected text. The inserted string will be automatically URL-encoded by PopClip.

You can also put options in the URL, in the same format as for AppleScripts. For example, http://translate.google.com/#auto%7C{popclip option language}%7C{popclip text}.

The string *** will work as a shorthand for {popclip text}.

Key Press actions

In a Key Press action, PopClip will simulate a key press, or sequence of presses, as if it was performed by the user.

A Key Press action is defined by the presence of a key combo or key combos field, as follows:

Key Type Description
key combo String The key combination to press, as defined in Key Combo String Format.
key combo Integer If a number is given, it is interpreted directly as a virtual key code (see below).
key combos Array Instead of a single key combo, you can supply array of them. PopClip will press all the key combos in sequence.

Key Combo String Format

The string format is a convenient human-readable format that can specify a key and modifiers. It is a space-separated list of one or more modifiers (the order does not matter), followed by the key to press. The key combo string is not case sensitive.

Some examples:

  • command b or command B- Hold command, and press 'b' key
  • option shift . - Hold option and shift, and press the dot key
  • command space - Hold command, and press space bar
  • f1 - The F1 key on its own with no modifiers
  • option 0x4b - 0x4b is the hex numeric code for 'Keypad Divide'

The key is specified in one of the following ways:

  • As a character. For keys which produce a single character. Examples: A, ;, 9.

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap