A second clipboard

Sometimes I want to copy and paste code without overwriting what’s already on the clipboard. There are probably several ways this can be done, but these two scripts work by, well, creating a second clipboard. First the copy script:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

tell application id "com.latenightsw.ScriptDebugger7" to set theSel to contents of selection of document 1
set thePasteboard to current application's NSPasteboard's pasteboardWithName:"MyVeryOwnPasteboard2"
thePasteboard's clearContents()
thePasteboard's writeObjects:{theSel}

And then the paste script:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set thePasteboard to current application's NSPasteboard's pasteboardWithName:"MyVeryOwnPasteboard2"
set theText to thePasteboard's stringForType:(current application's NSPasteboardTypeString)
if theText = missing value then
	beep
else
	set theText to theText as text
	tell application id "com.latenightsw.ScriptDebugger7" to set selection of document 1 to theText
end if

You can add them to the Scripts menu and give them suitable shortcuts.

You can also make versions for any application that supports some kind of selection property; if it doesn’t have its own Scripts menu you can add them to FastScripts or the system Scripts menu. Just change the relevant line. For example, my BBEdit versions have these modifications:

tell application id "com.barebones.bbedit" to set theSel to contents of selection of text window 1

and:

	tell application id "com.barebones.bbedit" to set contents of selection of text window 1 to theText

This means I can also copy and paste between Script Debugger and BBEdit without overwriting the normal clipboard.

4 Likes