Store and Retrieve Pasteboard (clipboard)

I’m looking for a way to save and restore pre-existing clipboard data.

I have a few Studio projects that use UI scripting. Sending text to a non-scriptable control by pasting from the clipboard is significantly faster than using the Keystroke command. What I’d like to do is store the complete contents of pasteboard “general”, execute my UI stuff, then repopulate the pasteboard with the archived values.

Because my goal is to leave the clipboard data as I found it, this needs to work with any kind of clipboard data. The problems I’m having relate to complex data being dumbed down. For example, an image copied from Safari becomes a plain text URL after storing and retrieving. Conceptually, moving a big lump of data should be possible, regardless of supported formats.

One other related question: Is the list returned by types of pasteboard “general” prioritized? If I had to pick one of the returned types, how would I know which was best?

Thanks in advance,


You may store the types expected by the app where you will paste the clipboard…

Perhaps you could try as a more simple approach using AS’s own clipboard access. But, for example, how do you place the Safari image in the clipboard?

The problem is I want to essentially buffer whatever is in there, use the clipboard for a UI call, then restore the previous contents. I don’t need to use any of the existing pasteboard data, I just need to be sure I can reconstruct it when I’m done. The Safari image was just an example, the pasteboard contents could be any type of data, from any application.

I also played with basic AppleScript clipboard commands, but they’re more limited than Studio’s pasteboard commands.

I don’t understand. You have something random in the clipboard, then paste it using UI Scripting, then what?

Hmmm… Eg:

  1. You have something random in the clipboard.
  2. You save it for later restoring.
  3. You place some text in the clipboard.
  4. You paste it using UI Scripting (aka “Apple + C”).
  5. You restore previous clipboard contents.

If this is what you need, it would be good enough this method:

set x to the clipboard --> store previous contents
set the clipboard to "some text"
--> paste it wherever
set the clipboard to x --> restore previous contents

Doesn’t this work?

If you’re interested in more complex save/restore operations, this package includes a very smart open-source script called “ScliptBoards”: