Skip to main content
Community Expert
February 13, 2023
Answered

Copy link names - script not doing exactly what I want

  • February 13, 2023
  • 4 replies
  • 784 views

I am trying to create a script that will copy the filenames into a string of text separating the text with " || "

This is a technical reason I need all linked files in a single line of text separated this way

 

For example links panel shows
Link 1.ai

Link 2.ai

Link 3.eps

 

and I need them in a single line as "Link 1.ai || Link 2.ai || Link 3.eps" etc.

 

I have it kinda working but can't figure it out it's driving me nuts.

 

I have this but it copies the text frame instead of the text.

I can't figure out how to get just the text out of the text frame to the clipboard.

Everything I've tried results in an error

 

// Get all linked images in active document
var linkedImages = app.activeDocument.links;

// Loop through each image and add its filename to filenames array
var filenames = [];
for (var i = 0; i < linkedImages.length; i++) {
filenames.push(linkedImages[i].name);
}

// Join filenames array with " || " separator
var joinedFilenames = filenames.join(" || ");

// Create a temporary text frame to copy the text to
var tempTextFrame = app.activeDocument.pages[0].textFrames.add({
geometricBounds: [0, 0, 10, 10]
});

// Set the contents of the text frame to the joined filenames
tempTextFrame.contents = joinedFilenames;

// Select the text in the temporary text frame
tempTextFrame.select();

// Copy the text to the clipboard
app.copy();

// Delete the temporary text frame
tempTextFrame.remove();

 

Any help?

This topic has been closed for replies.
Correct answer Peter Kahrel

Because the text frame is so small it gets overset and (unlike in the interface) you can't select both non-overset and overset text. Apparently. Make the frame much bigger and it works:

geometricBounds: [0, 0, 10000, 10000]

(You can make text frames miles wide and tall. Unlike pages, frames aren't limited in size. Which can be extremely useful.)

 

By the way, Eugene, why are you copying to the clipboard?

4 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
February 14, 2023

Because the text frame is so small it gets overset and (unlike in the interface) you can't select both non-overset and overset text. Apparently. Make the frame much bigger and it works:

geometricBounds: [0, 0, 10000, 10000]

(You can make text frames miles wide and tall. Unlike pages, frames aren't limited in size. Which can be extremely useful.)

 

By the way, Eugene, why are you copying to the clipboard?

Community Expert
February 13, 2023

Thank you both I will try this tomorrow.

Community Expert
February 14, 2023

Ok I've hit a wall - I tried to incorporate the changes but I've done something wrong

Error I get now is that Line 15 Source: app.copy(); 

Cannot copy/cut due to invalid selection state

 

Once I change the code @saschak38136531 the script doesn't work at all.

@Peter Kahrel I try using that line and it's not working

 

// Get all linked images in active document
var linkedImages = app.activeDocument.links;

// Get the filenames of all linked images and join them with " || " separator
var joinedFilenames = linkedImages.everyItem().name.join(' || ');

// Create a temporary text frame and set its contents to the joined filenames
var tempTextFrame = app.activeDocument.pages[0].textFrames.add({
geometricBounds: [0, 0, 10, 10]
});
tempTextFrame.contents = joinedFilenames;

// Select the text in the temporary text frame and copy it to the clipboard
tempTextFrame.texts[0].select();
app.copy();

// Remove the temporary text frame
tempTextFrame.remove();

Peter Kahrel
Community Expert
Community Expert
February 13, 2023

Eugene -- As Sacha pointed out, because the frame you placed is so small (I assume that your documents are using points), hardly anything fits in there. So instead of the frame you should use frame.texts[0], as Sacha did, or frame.parentStory. And you'd better do that both when you place the text into the frame and when you read the contents.

 

May I make a suggestion? JS has this great feature evryItem() and getElements(). You can grab a whole lot of information using these as a shortcut. For example, to get your file names, you can use this:

var joinedFilenames = app.activeDocument.links.everyItem().name.join (' || ');

P.

saschak38136531
Known Participant
February 13, 2023

instead of this line

tempTextFrame.select();

try to use this:

tempTextFrame.texts[0].select();

this should select the text inside the textframe.