Jump to content
goodkarma

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

Share this post


Link to post

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.

Share this post


Link to post

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?

Share this post


Link to post

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

Share this post


Link to post

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}"

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...