Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

How to enable clipboard API in CEP extension?

Explorer ,
Nov 04, 2024 Nov 04, 2024

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?

TOPICS
How to , Scripting
596
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 04, 2024 Nov 04, 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")

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 21, 2024 Nov 21, 2024

This does work, but 

document.execCommand

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 06, 2024 Dec 06, 2024
LATEST

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
       *
       * @Param {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
       *
       * @Param {File} fileObj    File object to write to
       * @Param {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;
      }
  }
}  

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines