Highlighted

add selected text to a variable

Community Beginner ,
Sep 01, 2020

Copy link to clipboard

Copied

Hey there,

 

I am trying to grab selected text and when running the script it cuts the text and pastes into a new textframe. Here is what I have:

 

    var myDoc = app.activeDocument;
    var sel = app.selection[0];
    var selectedText = "";

    if(app.selection == 0 || sel.characters.length == 0 || !sel.hasOwnProperty("baseline")) {
        alert("Please select some text");
        exit();
    } else {
        selectedText = sel.characters.length;      
        alert(selectedText);
}

I know when I run this I get the count of characters that are selected as a test. not sure how to get the characters into the variable selectedText. would a for loop be what I need? 

 

thanks in advance for any help!

 

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

How about the following

selectedText = sel.contents

 

-Manan

TOPICS
Scripting

Views

38

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

add selected text to a variable

Community Beginner ,
Sep 01, 2020

Copy link to clipboard

Copied

Hey there,

 

I am trying to grab selected text and when running the script it cuts the text and pastes into a new textframe. Here is what I have:

 

    var myDoc = app.activeDocument;
    var sel = app.selection[0];
    var selectedText = "";

    if(app.selection == 0 || sel.characters.length == 0 || !sel.hasOwnProperty("baseline")) {
        alert("Please select some text");
        exit();
    } else {
        selectedText = sel.characters.length;      
        alert(selectedText);
}

I know when I run this I get the count of characters that are selected as a test. not sure how to get the characters into the variable selectedText. would a for loop be what I need? 

 

thanks in advance for any help!

 

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

How about the following

selectedText = sel.contents

 

-Manan

TOPICS
Scripting

Views

39

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 01, 2020 0
Adobe Community Professional ,
Sep 01, 2020

Copy link to clipboard

Copied

How about the following

selectedText = sel.contents

 

-Manan

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 01, 2020 0
Community Beginner ,
Sep 01, 2020

Copy link to clipboard

Copied

haha of course. I tried contents, but I must have used it a different way! Thanks for your help!

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 01, 2020 0
Adobe Community Professional ,
Sep 02, 2020

Copy link to clipboard

Copied

Hi kyleo,

if you selected text your text in your script is stored in variable sel.

 

You want to move that text to a different text frame? If you like to do this with all its formatting you better use method move() with your selected text and not hunt for the contents of property contents.

 

 

// Text is selected.
// A single text frame is positioned on the first spread of your document.
// Move selected text to the first insertion point of that text frame:

app.selection[0].move
(
LocationOptions.AFTER ,
app.documents[0].spreads[0].textFrames[0].insertionPoints[0]
);

 

 

Regards,
Uwe Laubender

( ACP )

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 02, 2020 0