This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Javascript Examples
jasonm23 edited this page Sep 5, 2023
·
26 revisions
Example ~/.cutbox.js
require("~/.cutbox/basic.js");
This will load from ~/.cutbox/basic.js
:
// Save this to ~/.cutbox/basic.js
var stripTags = i => join(i).replace(/<[^>]*>/gm, "");
var join = i => i.join(i.length > 0 ? "\n" : "");
var squeeze = i => join( i.map( s => s.replace(/[ \t]+/gm, " ")));
var operatorSpacing = i => join(i.map(string =>
string
.replace(/\s*([-=+/*%<>])\s*/g," $1 ")
.replace(/([-+=/*%<>])\s*([-+=])/g, "$1$2")
));
var markdownCodeIndent = str => str.replace(/^/mg, " ");
// Important Note: this.cutboxFunctions
this.cutboxFunctions = [
{
// Acts like shell command: tr -s ' '
name: "Squeeze",
fn: squeeze,
},
{
// Strip XML/HTML tags (naive!)
name: "Strip Tags",
fn: stripTags,
},
{
// Fix up operator spacing
name: "Operator spacing",
fn: operatorSpacing
},
{
// Double quote each selected item
name: "Double Quoted",
fn: i => join(i.map(e => `"${e}"`)),
},
{
// Single quote each selected item
name: "Single Quoted",
fn: i => join(i.map(e => `'${e}'`)),
},
{
// Double quote each item and wrap and comma separate as a multi-language array
name: "Array formatted (quoted)",
fn: i => `[
${join(i.map(e => `"${e}",`))}
]`,
},
{
// wrap and comma separate as a multi-language array
name: "Array formatted (unquoted)",
fn: i => `[
${join(i.map(e => `${e},`))}
]`,
},
{
// Add a localized key and localized text as a Localizable.strings definition pair.
name: "Localized Pair",
fn: i => `"${i[0]}" = "${i[1]}";`
},
{
// Wrap Markdown code fence around the selection
name: "Markdown code fenced",
fn: i => `\`\`\`
${join(i)}
\`\`\``
},
{
// Markdown Code indent the selection
// (including at newlines in each item)
name: "Markdown code indented",
fn: i => join(
i.map(e => markdownCodeIndent(e) )
)
},
{
// Upper case all text in selection
name: "Upper cased",
fn: i => join(i.map(e => e.toUpperCase())),
},
{
// Lower case all text in selection
name: "Lower cased",
fn: i => join(i.map(e => e.toLowerCase())),
},
{
// Evalate the selection in the CutBox
// Javascript environment.
// -------------------------------------
// WARN/NOTE: JUST PREVIEWING SELECTED
// JS CODE THROUGH THIS WILL FUNC EXECUTE IT!
name: "Eval",
fn: i => eval(i.join(";"))
}
];
Eval is an interesting way to use JS with CutBox. You can evaluate arithmetic and immediately paste the result:
1 + 2 + 3
->
Eval ->
6
Which is handy, you can evaluate any JS this way, and the objects created in the JS environment will stay there until CutBox is quit.
This makes it very powerful and also a little bit dangerous!
If you manage to crash CutBox doing things with Javascript, I'd like to know about it. Please make sure your bug report describes very clearly how to reproduce the problem.
CutBox a nice little pasteboard time-machine