Jump to content

Save and restore current clipboard


Recommended Posts

Hey Alfred community,

 

I have been DuckDuckGo'ing (and Googling) and reading posts on this forum for hours looking for an answer before posting what feels like a very noob question, but I couldn't find what I needed, so here goes...

 

Is there a way to save the current clipboard to a variable in memory and then at the end of the workflow restore that variable to the current clipboard? I'm used to the logic of a programming languages where you would so something like this:

 

$myVar = $CLIPBOARD; // save clipboard

/* Do stuff that uses the clipboard */

$CLIPBOARD = $myVar; // restore clipboard

 

OR.. Is there a way to swap clipboard history item #2 back to item #1 ? That might solve the problem as well.

 

I know I can just use the clipboard history to get to the previous item and I know I could use a shell/script command to save the clipboard to a file and then bring that back. I'm just hoping there is a way to make it quick and easy and keeping the clipboard in memory is probably way faster and smarter if there is a way to do it.

 

Thanks!

Edited by goodkarma
Link to comment

After a little more playing, maybe the better way to do it would be with AppleScript -- unfortunately it's one language I don't know, at all. Any chance somebody can translate the following ideas into AppleScript syntax for me?

 

$myVar = $CLIPBOARD; 
$CLIPBOARD = "Some new text"; // or better yet the contents of a file.
Paste // maybe this requires sending a Cmd-V somehow?
$CLIPBOARD = $myVar;
 
Thanks again.
Link to comment

Your request is starting to become confusing, and it’s starting to look like an XY problem. What do you want, after all? How do you envision this working? Is this something that you easily want to add to an existing workflow, something that you want to insert into a worlflow you’re building, or something that is standalone?

Link to comment

Yah, I can see how it might look like an XY problem, but here is more context/examples to maybe make sense of it.

 

I guess the bottom line is that I want to output some text, probably from a file, without losing the current clipboard contents. There are probably ways to do this that don't involve the clipboard at all. I'm just new to Mac in general, so I am probably unaware of them. On Windows I used AutoHotKey which was amaze-balls!

 

I'm trying to figure out some AppleScript and see if that will solve this. So far I have the following...

set SavedClipVar to (the clipboard as text)
set the clipboard to "Some sample text"
tell application "System Events" to keystroke "v" using {command down}
set the clipboard to SavedClipVar
I don't yet know how to bring in the contents of a text file, which is actually what I'll be doing on line #2. I'm just using sample text to see if the logic works.
When I bind that to a hotkey, I just get a -ding- sound as if I pressed an invalid key or something.
What am I missing? Any AppleScript wizards out there?
 
I also tried using shell commands:
do shell script "pbpaste > ~/temp-clipboard-file.txt"
do shell script "cat ~/texts/commonly-used.txt | pbcopy"
tell application "System Events" to keystroke "v" using {command down}
do shell script "cat ~/temp-clipboard-file.txt | pbcopy"

That doesn't work either. Gives the same -DING- sound as if I hit an invalid key.

 

I'll keep playing. If I figure it out, I'll share the solution. Any help appreciated.

Edited by goodkarma
Link to comment

Emulating keystrokes can lead to trouble, but I’m not sure there’s a way to paste with AppleScript. You can also use it just for what you need and bash for the rest, for example.

starting_clipboard="$(pbpaste)"
pbcopy < /path/to/your/file
osascript -e 'tell application "System Events" to keystroke "v" using {command down}'
pbcopy <<< "${starting_clipboard}"
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...