Skip to main content
Sayed Ali Mousawi
Known Participant
September 20, 2019
Answered

how to get pasted content?

  • September 20, 2019
  • 2 replies
  • 1210 views

Hi, Scripters!

How can i get pasted text? i have some text in clipboard, i can paste it:

app.paste();

But i want to apply to it a paragraphStyle , maybe put a special Glyph at the insertionPoints[0]. so i thought maybe i have to put it to a variable:

var myPastedText = app.paste().contents;

but it does't work like that. and i don't know the solution.

thank you all for helping me with this issue.

This topic has been closed for replies.
Correct answer Mike Bro

Hello sayed_alim9773784

 

Try this to get yourself started.

 

doc = app.documents[0];

app.paste();

var myCopiedText= app.selection[0]

var styleName = "Your Style Name"; // enter the object style name you want to apply

for (i=0; i < myCopiedText.paragraphs.length; i++){

myCopiedText.parentStory.paragraphs[i].appliedParagraphStyle = app.activeDocument.paragraphStyles.item(styleName);

}

 

Regards,

Mike

2 replies

Community Expert
September 22, 2019

Hi Sayed,

if you want to control where exactly you are pasting text from the clipboard to text on the page you have to either:

1. Select an insertion point first to add the text without removing text that is already there by doing app.paste().

2. Select text first then do app.paste() to remove the selected text and add the text of the clipboard.

 

If you want full control: Add a new text frame to the document, select the first insertion point of that frame and do app.paste().

Then go ahead and format the pasted text, that's texts[0] of the parentStory of the added text frame. After done with that you could use methods move() or duplicate() to bring your formatted text or parts of it to other stories in the document.

 

Always work with instances of text like texts[0], paragraphs, lines, words, characters. Do not work with the value of text.contents.

Why? You want to work with formatted text. You want to work with special characters as well. Also with anchored items perhaps.

 

var doc = app.documents[0];
var tempFrame = doc.textFrames.add();
tempFrame.parentStory.insertionPoints[0].select();
app.paste();

 

Regards,
Uwe Laubender
( ACP )

Sayed Ali Mousawi
Known Participant
September 22, 2019
Thank you for your advice, the temporary text frame trick was brilliant.
Mike BroCorrect answer
Legend
September 20, 2019

Hello sayed_alim9773784

 

Try this to get yourself started.

 

doc = app.documents[0];

app.paste();

var myCopiedText= app.selection[0]

var styleName = "Your Style Name"; // enter the object style name you want to apply

for (i=0; i < myCopiedText.paragraphs.length; i++){

myCopiedText.parentStory.paragraphs[i].appliedParagraphStyle = app.activeDocument.paragraphStyles.item(styleName);

}

 

Regards,

Mike

Sayed Ali Mousawi
Known Participant
September 20, 2019
Thank you so much, this was what i need.