How to save text to clipboard?

Hi,
I want to save a String of text to clipboard. Is it possible to do it while the program is running in the background?
Thank you.

Here is a solution if you are using Java mode, it makes use of a class from the G4P library

  1. Create a new tab and name it- GClip.java (It is important that the you name the tab exactly as shown!!!)
  2. Copy this code into the new tab
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import sun.security.util.SecurityConstants;
/**
 * Clipboard functionality for plain text <br>
 * 
 * This provides clipboard functionality for text and is currently only used by the 
 * GTextField and GTextArea classes.
 * 
 * @author Peter Lager
 *
 */
public class GClip implements ClipboardOwner {

  /**
   * Static reference to enforce singleton pattern
   */
  private static GClip gclip = null;

  /**
   * Class attribute to reference the programs clipboard
   */
  private Clipboard clipboard = null;


  /**
   * Copy a string to the clipboard
   * @param chars the characters to be stored on the clipboard
   * @return true for a successful copy to clipboard
   */
  public static boolean copy(String chars) {
    if (gclip == null)
      gclip = new GClip();
    return gclip.copyString(chars);
  }

  /**
   * Get a string from the clipboard
   * @return the string on the clipboard
   */
  public static String paste() {
    if (gclip == null)
      gclip = new GClip();
    return gclip.pasteString();
  }

  /**
   * Ctor is private so clipboard is only created when a copy or paste is 
   * attempted and one does not exist already.
   */
  private GClip() {
    if (clipboard == null) {
      makeClipboardObject();
    }
  }

  /**
   * If security permits use the system clipboard otherwise create 
   * our own application clipboard.
   */
  private void makeClipboardObject() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
      try {
        security.checkPermission(SecurityConstants.ALL_PERMISSION);
        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
      } 
      catch (SecurityException e) {
        clipboard = new Clipboard("Application Clipboard");
      }
    } else {
      try {
        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
      } 
      catch (Exception e) {
        // THIS IS DUMB - true but is there another way - I think not
      }
    }
  }

  /**
   * Copy a string to the clipboard. If the Clipboard has not been created
   * then create it.
   * @param chars the characters to be stored on the clipboard
   * @return true for a successful copy to clipboard
   */
  private boolean copyString(String chars) {
    if (clipboard == null)
      makeClipboardObject();
    if (clipboard != null) {
      StringSelection fieldContent = new StringSelection (chars);
      clipboard.setContents (fieldContent, this);
      return true;
    }
    return false;
  }

  /**
   * Gets a string from the clipboard. If there is no Clipboard
   * then create it.
   * @return if possible the string on the clipboard else an empty string
   */
  private String pasteString() {
    // If there is no clipboard then there is nothing to paste
    if (clipboard == null) {
      makeClipboardObject();
      return "";
    }
    // We have a clipboard so get the string if we can
    Transferable clipboardContent = clipboard.getContents(this);

    if ((clipboardContent != null) &&
      (clipboardContent.isDataFlavorSupported(DataFlavor.stringFlavor))) {
      try {
        String tempString;
        tempString = (String) clipboardContent.getTransferData(DataFlavor.stringFlavor);
        return tempString;
      }
      catch (Exception e) {
        e.printStackTrace ();
      }
    }
    return "";
  }

  /**
   * Reqd by ClipboardOwner interface
   */
  public void lostOwnership(Clipboard clipboard, Transferable contents) {
  }
}
  1. Now test it by adding this code to the main tab
void setup(){
  String text = "Once upon a time...";
  boolean success = GClip.copy(text);
  if(success) {
    println("Text successfully copied to clipboard");
    String recoveredText = GClip.paste();
    println("Recovered test -");
    println(recoveredText);
  }
  else {
    println("Unable to use clipboard");
  }
}

If all went well you should see

Text successfully copied to clipboard
Recoveered test -
Once upon a time...
4 Likes

@quark

very thank you. good~

1 Like