Skip to main content
Known Participant
January 27, 2016
Answered

Add Tab Space to text

  • January 27, 2016
  • 2 replies
  • 904 views

Hi guys… I would like a Javascript to add tab space

to the selected text as shown on the image attached.

Could you please help me?

Many thanks in advance 🙂

This topic has been closed for replies.
Correct answer pixxxelschubser

Hi W_J_T,

not bad. Perhaps this additions are also helpful for Puthoskey :

// tabulator_replaceSpaceWithTabulator.jsx

// required: a selected text frame

// https://forums.adobe.com/thread/2076401

function addTabs() {

    var doc = app.activeDocument;

    var txt = doc.selection[0];

   

    if (txt.typename == "TextFrame") {

        // useful complement, based on code IMHO written by Jongware

        tabInf = new Array(1);

        tabInf[0] = new TabStopInfo;

        // set here the tabulator position in pt/px

        tabInf[0].position = 55; 

        curTxtString = txt.contents;

        // curTxtString = curTxtString.replace('NAME:', 'NAME:\t').replace('ADDRESS:', 'ADDRESS:\t').replace('DATE:', 'DATE:\t').replace('PHONE:', 'PHONE:\t');

        // !!! Do not forget to replace the spaces too !!!

        curTxtString = curTxtString.replace('NAME: ', 'NAME:\t').replace('ADDRESS: ', 'ADDRESS:\t').replace('DATE: ', 'DATE:\t').replace('PHONE: ', 'PHONE:\t');

        txt.contents = curTxtString;

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

            txt.paragraphs.tabStops = tabInf;

            }

        }

    }

addTabs();

Have fun

2 replies

PuthoskeyAuthor
Known Participant
February 1, 2016

Many thanks guys.

It works like a charm

Inspiring
January 27, 2016

\t = Tab

So on a basic level here is an example. It assumes you have a tab stop setup, otherwise you may have to add extra tabs '\t' for certain instances to get things evened out.


function addTabs() {

    var doc = app.activeDocument;

    var txt = doc.selection[0];

    if (txt.typename == "TextFrame") {

        curTxtString = txt.contents;

        curTxtString = curTxtString.replace('NAME:', 'NAME:\t').replace('ADDRESS:', 'ADDRESS:\t').replace('DATE:', 'DATE:\t').replace('PHONE:', 'PHONE:\t');

        txt.contents = curTxtString;

    }

}

addTabs();


Hope it helps your efforts.

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
January 27, 2016

Hi W_J_T,

not bad. Perhaps this additions are also helpful for Puthoskey :

// tabulator_replaceSpaceWithTabulator.jsx

// required: a selected text frame

// https://forums.adobe.com/thread/2076401

function addTabs() {

    var doc = app.activeDocument;

    var txt = doc.selection[0];

   

    if (txt.typename == "TextFrame") {

        // useful complement, based on code IMHO written by Jongware

        tabInf = new Array(1);

        tabInf[0] = new TabStopInfo;

        // set here the tabulator position in pt/px

        tabInf[0].position = 55; 

        curTxtString = txt.contents;

        // curTxtString = curTxtString.replace('NAME:', 'NAME:\t').replace('ADDRESS:', 'ADDRESS:\t').replace('DATE:', 'DATE:\t').replace('PHONE:', 'PHONE:\t');

        // !!! Do not forget to replace the spaces too !!!

        curTxtString = curTxtString.replace('NAME: ', 'NAME:\t').replace('ADDRESS: ', 'ADDRESS:\t').replace('DATE: ', 'DATE:\t').replace('PHONE: ', 'PHONE:\t');

        txt.contents = curTxtString;

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

            txt.paragraphs.tabStops = tabInf;

            }

        }

    }

addTabs();

Have fun