Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Paste Pipeline

Jason Milkins edited this page Sep 17, 2023 · 1 revision

(Through JavaScript and other languages!)

CutBox can send your selected item(s) through JavaScript functions before it's pasted. You will need to create ~/.cutbox.js first.

touch ~/.cutbox.js

Add functions to ~/.cutbox.js

CutBox will read this file when it starts up.

See an example ~/.cutbox.js here... Javascript Examples

How do I paste through JavaScript?

To send your paste through JavaScript, select one or more item(s) in CutBox and press Cmd + Enter

You'll see a new list showing your function names and preview of their output.

CutBox will look for functions in an array (this.cutboxFunctions).

See below:

// function definitions in ~/.cutbox.js

this.cutboxFunctions = [
  {
    name: "Join", // Names should be unique (if not the first named fn will win.)
    fn: items => items.join(items.length > 0 ? "\n" : "")
  },
  {
    name: "Join spaced",
    fn: i => i.join(" ")
  },
]

Fuzzy search will filter down the functions by name, move up and down to select a function, pressing enter will paste.

Using shell commands

You can access shell commands through the shellCommand (since CutBox v1.5.4):

shellCommand("command args")

for example:

let files = function(dir) {
  return shellCommand(`ls ${dir}`)
}

This example will return the output of ls $dir which will be pasted.

This opens up the use of any programming language to process your selected CutBox items, while pasting them.

Using existing JavaScript libraries

require any JavaScript file into CutBox.

For example, if you had mustache.js in a folder called: ~/.cutbox/

// Load the mustache library.

require("~/.cutbox/mustache.js")

// Now we can use Mustache (read more at https://github.com/janl/mustache.js/)

Reloading

(Reload JavaScript in CutBox's preferences.)

Defining functions

CutBox expects your JavaScript functions to reduce arrays of strings, to a single string.

  • They must accept a single argument, an array of strings e.g. ["item", "item"] or ["item"]
  • They must return a string -> String "item\nitem" or "item"

CutBox.js specification

Here's a simple example of such a function in Javascript ES6:

var fn = items => items.join()

fn reads items and joins them into a single string.

Some examples

CutBox will let you do anything you want with the text it's about to paste.

Here's a few examples:

Squeeze text to just one space between words.

var squeeze = items => items.map(s => s.replace(/\s+/g, ' ')
                            .join(items.length > 0 ? "\n" : "")

Turn this:

My    example text   has    too    many      spaces

into this...

My example text has too many spaces

Quote selected items

var  = items => items.map(s => `"${s}"`)
                     .join(items.length > 0 ? "\n" : "")

Turn this:

My text

into this...

"My text"

or turn this:

My selected item
Another one
etc

into this...

"My selected item"
"Another one"
"etc"

Quote and comma separate:

var  = items => (items.map(s => `"${s}"`))
                      .join(items.length > 0 ? ", " : "")

turn this:

My selected item
Another one
etc

into this...

"My selected item", "Another one", "etc"