Skip to main content
Participating Frequently
November 4, 2024
Question

How to enable clipboard API in CEP extension?

  • November 4, 2024
  • 1 reply
  • 722 views

I have a CEP extension for After Effects, and I try to write to the clipboard buffer using the native JavaScript clipboard API as follows:

await navigator.clipboard.writeText("test-clipboard-cep");

Executing that line of code throws this error:

NotAllowedError: Write permission denied.

How can I enable the usage of that API?

This topic has been closed for replies.

1 reply

Brainiac
November 4, 2024

Try this old method:

 

  function copyToClipboard(txt) {
    var textarea = document.createElement("textarea")
    document.body.appendChild(textarea)
    textarea.value = txt
    textarea.select()
    document.execCommand("Copy")
    document.body.removeChild(textarea)
  }

copyToClipboard("test-clipboard-cep")

 

New Participant
November 21, 2024

This does work, but 

document.execCommand

is deprecated. Is there not a way to obtain clipboard permissions?

Participating Frequently
December 6, 2024

I use the extendscript side to pass the clipboard to CEP enviroment. You can use this function on AE side (not my work). You could alter it to pass something other than strings but you will always have to write something to disk using bash/powershell. I have not find a way to work around that:

export function getClipboardAsString() {
  var clipboardContent = getClipboardContent();
  return clipboardContent;

  /**
   * Gets the contents of the clipboard as a string
   *
   * @Returns {String} Clipboard contents
   */
  function getClipboardContent() {
      var isWin = $.os.indexOf("Windows") !== -1;
      var tempFolder = Folder.desktop;
      var tempFile = tempFolder.fsName + '/' + Date.now().toString() + '.txt';

      writeClipboardContentToFile(tempFile);

      var fileContent = readContent(tempFile);
      File(tempFile).remove();

      return fileContent;


      /**
       * Writes clipboard contents to file
       *
       * @9397041 {String} tempFilePath Temporary file path
       */
      function writeClipboardContentToFile(tempFilePath) {
          var cmd = 'LANG=en_US.UTF-8 pbpaste > "' + tempFilePath + '"';

          if (isWin)
              cmd = "cmd /c \"for /f \"eol=; tokens=* delims= \" %I in ('powershell Get-Clipboard') do echo %I >> " + tempFilePath + "\"";

          system.callSystem(cmd);
      }

      /**
       * Reads the contents of a file
       *
       * @9397041 {File} fileObj    File object to write to
       * @9397041 {String} encoding Encoding type
       * @Returns {String}        File contents
       */
      function readContent(fileObj, encoding) {
          var fileContent;

          encoding = encoding || "UTF-8";
          fileObj = (fileObj instanceof File) ? fileObj : new File(fileObj);

          if (!fileObj.exists) {
              throw "File " + fileObj.fsName + " doesn't exist!";
          }

          if (File.isEncodingAvailable(encoding)) {
              fileObj.encoding = encoding;
          }

          fileObj.open("r");
          fileContent = fileObj.read();
          fileObj.close();

          if (fileContent === null) {
              throw "Unable to read contents of " + fileObj.fsName;
          }

          return fileContent;
      }
  }
}