Skip to main content
Inspiring
March 20, 2015
Answered

How to select a character style instead of the whole selection of text when running a script?

  • March 20, 2015
  • 1 reply
  • 480 views

I have a script, that after selecting text, can modify the appearance of digits. This action, consequently, covers all the numbers in the document through:


    01. if (app.selection.length != 1)

    [... here, the rest of the script]


How to modify this line to apply the changes only to those digits previosuly tagged with an specific character style?


Thanks.


This topic has been closed for replies.
Correct answer Loic.Aigon

I was trying to change this line that is selecting all numbers:

if (app.selection.length != 1)

This line does all but selecting all numbers. It just checks if there are more than one item selected. Texts selection always return length of 1. Hence this check.

And given what I offered previously, all you should have to do in Jongware script to match your needs is to add app.findGrepPreferences.appliedCharacterStyle = theStyle; 

Prior to findGrep().

HTH

Loic

http://www.ozalto.com

1 reply

Loic.Aigon
Legend
March 20, 2015

Hi,

Unless I am wrong your case is a perfect candidate for Find/Change Grep.

//Définition de nos critères de recherche

  app.findGrepPreferences.findWhat = "\d+";

  app.findGrepPreferences.appliedCharacterStyle = theStyle;

  app.changeGrepPreferences.changeTo = "whatever";

  //Modification du contenu

  sel.changeGrep();

HTH,

Loïc

http://www.ozalto.com

Inspiring
March 20, 2015

Loic,

Thanks. it is perfect for that situation.

And avoids the mechanical use of f/change.

What about in this case?

I was trying to change this line that is selecting all numbers:

if (app.selection.length != 1)

It is possible to to integrate in this script by Jongware's script?

//DESCRIPTION:omg the page numbers are all wrong!

// A Jongware Script 18-Aug-2010

if (app.documents.length == 0)

{

     alert ("Select text :'(");

     exit(0);

}

if (app.selection.length != 1)

{

     alert ("We can't go on like this. Select some text first.");

     exit(0);

}

myDialog = app.dialogs.add ({name:"omg the numbers are wrong!",canCancel:true});

with (myDialog)

{

     with (dialogColumns.add())

     {

          with (dialogRows.add())

               staticTexts.add ({staticLabel:"First to change"});

          with (dialogRows.add())

               aBox = integerEditboxes.add({editContents:"1"});

          with (dialogRows.add())

               staticTexts.add ({staticLabel:"Last to change"});

          with (dialogRows.add())

               bBox = integerEditboxes.add({editContents:"99999"});

          with (dialogRows.add())

               staticTexts.add ({staticLabel:"Add or subtract this value"});

          with (dialogRows.add())

               cBox = integerEditboxes.add({editContents:"2"});

     }

}

if (!myDialog.show())

{

     myDialog.destroy();

     exit(0);

}

first = aBox.editValue;

last = bBox.editValue;

step = cBox.editValue;

if (first < 1 || first > last || step == 0)

{

     alert ("Now you're pulling my nose arentya");

     exit(0);

}

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = "\\b\\d+\\b";

list = app.selection[0].findGrep(true);

changes = 0;

for (i=0; i<list.length; i++)

{

     n = Number(list.contents);

     if (n >= first && n <= last)

          changes++, list.contents = String(n+step);

}

alert ("Number of changes: "+changes);

Loic.Aigon
Loic.AigonCorrect answer
Legend
March 20, 2015

I was trying to change this line that is selecting all numbers:

if (app.selection.length != 1)

This line does all but selecting all numbers. It just checks if there are more than one item selected. Texts selection always return length of 1. Hence this check.

And given what I offered previously, all you should have to do in Jongware script to match your needs is to add app.findGrepPreferences.appliedCharacterStyle = theStyle; 

Prior to findGrep().

HTH

Loic

http://www.ozalto.com