Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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")
Copy link to clipboard
Copied
This does work, but
document.execCommand
is deprecated. Is there not a way to obtain clipboard permissions?
Copy link to clipboard
Copied
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;
}
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now