Highlighted

Set focus to textFrame to paste text?

Engaged ,
Sep 22, 2020

Copy link to clipboard

Copied

Hi guys. I have a project where I'd like to be able to use a copy command multiple times on a document, then be able to paste multiple times in a new document, all while not switching between the two as activeDocument while doing so.

 

My initial thought was that I could get around this by selecting what I wanted, running a copy command, creating a new textFrame, setting focus to it (or selecting all it's textRanges) and pasting the clipboard contents inside. But for some reason, when I create a new TextFrame the TextFrame.selected property does nothing while turning true -- I have to create it, then iterate over the document TextFrames, then find it again and toggle to true. And even then, a paste command creates a new Text Frame instead of inserting the content into the selected one.

 

Is there any way to effectively paste inside a given TextFrame?

Adobe Community Professional
Correct answer by Charu Rajput | Adobe Community Professional

For the above code, when you create the textframe, it does not refresh. Therefore

temp.selected

returns false in alert. Try using below code

var doc = app.activeDocument;
createFrame('Test');

function createFrame(text) {
  var temp = app.activeDocument.textFrames.add(); // Creates the frame
  temp.contents = text; // Sets text contents inside to temp value
  app.redraw()  // Or $.sleeep(1000); 
  temp.selected = true; // This does not work.
  alert(temp.selected); // Reports false despite assigning above
  alert(temp.typename); // Reports "textFrame", subclass of PageItem
  // selectAllTextFrames(); // Only now will affect selected property 
}

 

app.redraw(), will refresh and it will return true when you do temp.selected or I just tried to use $.sleep(1000), this also works. 

 

Please try once and let me know how it goes at your end.

TOPICS
Scripting

Views

109

Likes

Translate

Translate

Report

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

Set focus to textFrame to paste text?

Engaged ,
Sep 22, 2020

Copy link to clipboard

Copied

Hi guys. I have a project where I'd like to be able to use a copy command multiple times on a document, then be able to paste multiple times in a new document, all while not switching between the two as activeDocument while doing so.

 

My initial thought was that I could get around this by selecting what I wanted, running a copy command, creating a new textFrame, setting focus to it (or selecting all it's textRanges) and pasting the clipboard contents inside. But for some reason, when I create a new TextFrame the TextFrame.selected property does nothing while turning true -- I have to create it, then iterate over the document TextFrames, then find it again and toggle to true. And even then, a paste command creates a new Text Frame instead of inserting the content into the selected one.

 

Is there any way to effectively paste inside a given TextFrame?

Adobe Community Professional
Correct answer by Charu Rajput | Adobe Community Professional

For the above code, when you create the textframe, it does not refresh. Therefore

temp.selected

returns false in alert. Try using below code

var doc = app.activeDocument;
createFrame('Test');

function createFrame(text) {
  var temp = app.activeDocument.textFrames.add(); // Creates the frame
  temp.contents = text; // Sets text contents inside to temp value
  app.redraw()  // Or $.sleeep(1000); 
  temp.selected = true; // This does not work.
  alert(temp.selected); // Reports false despite assigning above
  alert(temp.typename); // Reports "textFrame", subclass of PageItem
  // selectAllTextFrames(); // Only now will affect selected property 
}

 

app.redraw(), will refresh and it will return true when you do temp.selected or I just tried to use $.sleep(1000), this also works. 

 

Please try once and let me know how it goes at your end.

TOPICS
Scripting

Views

110

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 22, 2020 1
Adobe Community Professional ,
Sep 22, 2020

Copy link to clipboard

Copied

Hi,

Could you please share your sample code, so that we can start from your code.

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 22, 2020 0
Engaged ,
Sep 22, 2020

Copy link to clipboard

Copied

 

var doc = app.activeDocument;
createFrame('Test');

function createFrame(text) {
    var temp = app.activeDocument.textFrames.add(); // Creates the frame
    temp.contents = text; // Sets text contents inside to temp value
    //
    temp.selected = true; // This does not work.
    //
    alert(temp.selected); // Reports false despite assigning above
    alert(temp.typename); // Reports "textFrame", subclass of PageItem
    selectAllTextFrames(); // Only now will affect selected property 
}

function selectAllTextFrames() {
    for (var i = 0; i < doc.textFrames.length; i++) {
        var item = doc.textFrames[i];
        item.selected = true;
        alert(item.typename); // Reports "textFrame", same as on creation
        alert(item.selected); // Reports true this time?
    }
}

 

 

I know I could save the uuid of the pageItem, then iterate through and select any pageItem that matches this uuid.

 

This part is kind of inconsequential to the original question, since I have a large document where I want to select all items on a particular artboard, copy, and save the contents of my clipboard somewhere else. Hence the reason why I'm trying to select a textFrame and paste the clipboard contents inside, but I did notice how the above code wasn't working as expected. The real issue is either: 1) Being able to read clipboard contents, 2) Being able to set application focus inside of a textFrame to paste inside the contents, or 3) Being able to transfer large amounts of layers and data between documents without either switching between them for each copy/paste action or using a saveAs to generate the new file and iterating through the document to delete all unwanted items/artboards (since I'd be better off generating the document from scratch and setting artboards/layers in a completely different way).

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 22, 2020 0
Engaged ,
Sep 22, 2020

Copy link to clipboard

Copied

In a TL;DR for clarity: I want to create a temporary textFrame, paste the contents of my clipboard inside, then continue with the rest of my script. I need to copy/paste 4 - 10 times per file, and ideally I'd make 4 - 10 temporary textFrames during this then at the end, join all their contents together, send this data back to CEP and delete the textFrames, create a new document, begin to construct it and set my clipboard in CEP (very easy, but not so for reading of clipboard) as I go so I can simply paste large sections of layers/art in as I need to.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 22, 2020 0
Adobe Community Professional ,
Sep 22, 2020

Copy link to clipboard

Copied

For the above code, when you create the textframe, it does not refresh. Therefore

temp.selected

returns false in alert. Try using below code

var doc = app.activeDocument;
createFrame('Test');

function createFrame(text) {
  var temp = app.activeDocument.textFrames.add(); // Creates the frame
  temp.contents = text; // Sets text contents inside to temp value
  app.redraw()  // Or $.sleeep(1000); 
  temp.selected = true; // This does not work.
  alert(temp.selected); // Reports false despite assigning above
  alert(temp.typename); // Reports "textFrame", subclass of PageItem
  // selectAllTextFrames(); // Only now will affect selected property 
}

 

app.redraw(), will refresh and it will return true when you do temp.selected or I just tried to use $.sleep(1000), this also works. 

 

Please try once and let me know how it goes at your end.

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 22, 2020 0
Engaged ,
Sep 22, 2020

Copy link to clipboard

Copied

Okay thanks, that does work. But how would I paste my clipboard contents inside this textFrame? I'm trying to retrieve the large amount of SVG code set to the clipboard when you would copy layers/items in the application. I figure the only way I can effectively read the clipboard is through some kind of hack like setting textFrame contents to be the same as the clipboard, doing it a few times, concatenating them together, and then going to the new document.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 22, 2020 0
Adobe Community Professional ,
Sep 22, 2020

Copy link to clipboard

Copied

Instead of paste, can't you use contents property. I mean you have selected textframe, you have data in your clipboard. so you can set the contents of selected textframe to the clipboard data? Correct me if I am missing some part in understandingf the flow.

 

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 22, 2020 0
Engaged ,
Sep 22, 2020

Copy link to clipboard

Copied

Yeah, I understand that. I'm using the contents property just to have the textFrame visible so I don't have to keep watch on the Layers panel to know a textFrame was created.

 

Okay. You know when you copy from Illustrator, your clipboard contents become SVG like this

 

<!-- Generator: Adobe Illustrator 24.2.3, SVG Export Plug-In  -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 width="229.9231px" height="229.9231px" viewBox="0 0 229.9231 229.9231"
	 overflow="visible" enable-background="new 0 0 229.9231 229.9231" xml:space="preserve">
<defs>
</defs>
<rect x="0.5" y="0.5" fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" width="228.9231" height="228.9231"/>
</svg>

 

I'd like to create a textFrame with the above as plaintext, as the value of its contents property. When you paste the above in Illustrator, you'll get the art equivalent, not the text. I want to paste it in as art later, but be able to save it somewhere else first. The idea is that I want to copy multiple sections, save them as plaintext, send them to CEP, then move to a new document and begin pasting them in as art since I can easily set my clipboard contents from CEP. The issue is that there's no conventional way to access clipboard data in web dev since it's a security issue, so I thought there may be a workaround or hack possible inside Illustrator.

 

I can't find a way to effectively paste the above SVG into a specified textFrame as plaintext instead of the app just generating a rectangle from the SVG code itself. If, as a user, I set my focus inside of a textFrame and then paste -- that's exactly the result I'm looking for. The code becomes plaintext. But I don't know of any programmatic way to set focus inside a textFrame to have a paste command affect the contents instead of just creating new items and art.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 22, 2020 0
Adobe Community Professional ,
Sep 22, 2020

Copy link to clipboard

Copied

Ok, here is one sample code, assuming you have data in your clipboard, try following script. This will copy data from clipboard to the textframe that you will create.

 

var doc = app.activeDocument;
var _newTextFrame = doc.textFrames.add();
_newTextFrame.contents = 'Test';
_newTextFrame.textRange.select();
app.executeMenuCommand('paste');

Let me know how it works in your workflow.

 

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 22, 2020 0
Adobe Community Professional ,
Sep 23, 2020

Copy link to clipboard

Copied

Well,

I tried more, so here is what I found, above script sometimes works, and sometimes it does not work.

If we create a textframe manually first, then exceute the above script, it paste the content from the clipboard as a string but in case another tool is selected and we run the above script it will not paste the content in the newTextframe, instead, it paste in application as an art(which is happening in your case).

Secondly, I thought to record the action to create the textframe and found that Textframe creation is not recorded in the action. So currently as per I know contents is a property by which we can add clipboard data to the textframe that we have created. But I am amazed why Text Tool is not recorded in the actions?

 

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 23, 2020 0