Extra Clipboard Functions for Mac Shell Scripts
19 Jan 2020Working with shell scripting is in essence very similar to programming in a more traditional compile-to-run language. The key differences for me are the depth of access and that it is interpreted. On occasion the scripting commands available simply don’t do everything you might want in one simple to execute command, and so, just like any other areas of coding, you can go down the path of creating your own functions and shorthand. In this post I’m going to share a few functions I’m using for adding content to the Mac clipboard/pasteboard.
First of all, there’s nothing ground breaking here. The functions I’m sharing are simply convenience functions for clipboard activities I find myself doing on at least a semi-regular basis when I’m scripting. They are provided as is with no warranty, but as you’ll see there isn’t any notable complexity to them, so you should be pretty safe using them.
Appending
There are three functions defined here for adding content to the end of the existing pasteboard content.
pbappend
simply adds the content directly.pbnlappend
adds a new line followed by the additional content.pbspappend
adds a space followed by the additional content.
# pbappend: Append text to general pasteboard
# pbappend <text to append>
function pbappend
{
echo "$(pbpaste)"$1 | pbcopy
}
# pbanlppend: Append text preceded by a new line to general pasteboard
# pbnlappend <text to append>
function pbnlappend
{
echo "$(pbpaste)""\n"$1 | pbcopy
}
# pbspappend: Append text preceded by a space to general pasteboard
# pbspappend <text to append>
function pbspappend
{
echo "$(pbpaste) "$1 | pbcopy
}
Prepending
There are three functions defined here for adding content to the start of the existing pasteboard content.
pbprepend
simply adds the content directly to the pasteboard followed by the pasteboard’s original content.pbprependnl
puts the additional content followed by a new line, followed by the existing pasteboard content on the pasteboard.pbprependsp
puts the additional content, followed by a space, followed by the existing pasteboard content on the pasteboard.
Note the slightly different naming here. Rather than the space (‘sp’) and new line (‘nl’) identifiers appearing after the pb, they appear at the end signifying they come after the prepending content rather than before as in the append functions above.
# pbprepend: Prepend text to general pasteboard
# pbprepend <text to append>
function pbprepend
{
echo $1"$(pbpaste)" | pbcopy
}
# pbprependnl: Prepend text followed by a new line to general pasteboard
# pbprependnl <text to append>
function pbprependnl
{
echo $1"\n""$(pbpaste)" | pbcopy
}
# pbprependsp: Prepend text followed by a space to general pasteboard
# pbprependsp <text to append>
function pbprependsp
{
echo $1" $(pbpaste)" | pbcopy
}
Examples
Here’s a function that demonstrates using the functions and gradually building up the pasteboard content.
function pb_examples
{
# Appending
echo \> Put \"foo\" on pasteboard
echo "foo" | pbcopy
pbpaste
echo
echo \> Append \"bar\" to pasteboard
pbappend "bar"
pbpaste
echo
echo \> Append \"quz\" to pasteboard on a new line
pbnlappend "quz"
pbpaste
echo
echo \> Append \"qux\" to pasteboard preceded by a space
pbspappend "qux"
pbpaste
echo
echo ---
# Prepending
echo \> Put \"foo\" on pasteboard
echo "foo" | pbcopy
pbpaste
echo
echo \> Prepend \"bar\" to pasteboard
pbprepend "bar"
pbpaste
echo
echo \> Prepend \"quz\" to pasteboard followed by a new line
pbprependnl "quz"
pbpaste
echo
echo \> Prepend \"qux\" to pasteboard followed by a space
pbprependsp "qux"
pbpaste
echo
echo ---
# Feed from command
echo \> Put \"foo\" on pasteboard
echo "foo" | pbcopy
pbpaste
echo
echo \> Feed \"bar\" to append to pasteboard
pbappend $(echo "bar")
pbpaste
}
When you combine all of the functions above and run pb_examples
, you should get output like this:
> Put "foo" on pasteboard
foo
> Append "bar" to pasteboard
foobar
> Append "quz" to pasteboard on a new line
foobar
quz
> Append "qux" to pasteboard preceded by a space
foobar
quz qux
---
> Put "foo" on pasteboard
foo
> Prepend "bar" to pasteboard
barfoo
> Prepend "quz" to pasteboard followed by a new line
quz
barfoo
> Prepend "qux" to pasteboard followed by a space
qux quz
barfoo
---
> Put "foo" on pasteboard
foo
> Feed "bar" to append to pasteboard
foobar
Github
I’ve also posted everything here to a (new and quite bare currently) Github repository as pb_functions.sh
. I’ll add more content to this over time, so keep an eye out for that.
Hopefully, you’ll find some use for these functions or the principles applied. Happy shell scripting!