• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Apply Text variable to selected text?

Advocate ,
May 05, 2017 May 05, 2017

Copy link to clipboard

Copied

Right now I have the Text variable where it will add to the document.  And i have it set to a shortcut so that way if i need it i can add it My question is. How can i make it apply to the text i have selected?

var doc = app.activeDocument;

var tvLn = doc.textVariables.item("L#Date");  

    !tvLn.isValid && tvLn = doc.textVariables.add({name:"L#Date", variableType:VariableTypes.CUSTOM_TEXT_TYPE});   

    tvLn.variableOptions.contents = String( getLDate() );

 

function getLDate() {

    var date = new Date(); 

    var month = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]; 

    var formatDate = ("/" + date.getFullYear()).substr(-2); 

        return month[date.getMonth()] +"/" + formatDate; 

}

TOPICS
Scripting

Views

451

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
community guidelines

correct answers 1 Correct answer

Community Expert , May 08, 2017 May 08, 2017

Hi,

if you want to simulate the menu in the UI where selected text is exchanged with the text variable you could use this:

var doc = app.documents[0];

var selection = app.selection[0];

var selectedText = selection.texts[0];

var tvLn = doc.textVariables.item("L#Date");

if(tvLn.isValid)

{

    selection.textVariableInstances.add

    (

        undefined,

        undefined,

        { associatedTextVariable: tvLn }

    );

    if(selectedText.insertionPoints.length > 1)

    {

        selectedText.remove();

    };

};

Rega

...

Votes

Translate

Translate
Community Expert ,
May 08, 2017 May 08, 2017

Copy link to clipboard

Copied

Hi,

if you want to simulate the menu in the UI where selected text is exchanged with the text variable you could use this:

var doc = app.documents[0];

var selection = app.selection[0];

var selectedText = selection.texts[0];

var tvLn = doc.textVariables.item("L#Date");

if(tvLn.isValid)

{

    selection.textVariableInstances.add

    (

        undefined,

        undefined,

        { associatedTextVariable: tvLn }

    );

    if(selectedText.insertionPoints.length > 1)

    {

        selectedText.remove();

    };

};

Regards,
Uwe

Votes

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
community guidelines
Advocate ,
May 12, 2017 May 12, 2017

Copy link to clipboard

Copied

LATEST

Thank you works like a charm. 

Votes

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
community guidelines