Indesign Javascript复制链接名称到剪贴板



我正在尝试创建一个Adobe Indesign JavaScript,将所选项目的链接名称复制到系统剪贴板。例如,如果我选择了3个帧并在剪贴板上放置了3个不同的图像我将得到

Image01.jpg

Image02.png

Image03.pdf

try {
// Get the selected items
var selectedItems = app.activeDocument.selection;
// Initialize an empty array to store the file names
var fileNames = [];
// Check if there are any selected items
if (selectedItems.length > 0) {
// Loop through the selected items
for (var i = 0; i < selectedItems.length; i++) {
// Check if the selected item is an Link
if (selectedItems[i] instanceof Link) {
// If it's an image, add the file name to the array
fileNames.push(selectedItems[i].itemLink.name);
}
}
// Check if the file names array is empty
if (fileNames.length > 0) {
// Join the file names into a single string
var fileNamesString = fileNames.join("n");
// Copy the string to the clipboard
app.copy(fileNamesString);
}
else {
// If the file names array is empty, display an alert message
alert("None of the selected items are images.");
}
}
else {
// If there are no selected items, display an alert message
alert("Please select at least one item.");
}
}
catch (e) {
// If an exception is thrown, display an alert message
alert("An error occurred:n" + e);
}

到目前为止,我已经尝试运行上面的代码,但它不工作,抛出一个错误,如果我有一个框架选择了一个链接,如果我没有选择。

发生错误:TypeError: undefined不是一个对象

帮助让这个工作非常感谢。

尝试更改行:

if (selectedItems[i] instanceof Link) {
// If it's an image, add the file name to the array
fileNames.push(selectedItems[i].itemLink.name);
}

try { fileNames.push(selectedItems[i].graphics[0].itemLink.name) }
catch(e) { fileNames.push(selectedItems[i].itemLink.name) }

这个明显的方法app.copy(fileNamesString);在InDesign中不起作用。不幸的是。

如果你想把文本放入剪贴板,你必须创建一个文本框架,把文本放入框架,选择文本,然后使用app.copy()方法。

下面是专用函数:

function copy_to_clipboard(text) {
var frame = app.activeDocument.pages[0].textFrames.add();
frame.geometricBounds = [0,0,200,100];
frame.contents = text;
frame.parentStory.texts.everyItem().select();
app.copy();
$.sleep(300); // just in case
frame.remove();
}

现在你要替换行:

app.copy(fileNamesString);

copy_to_clipboard(fileNamesString);

最新更新