Skip to main content
Participating Frequently
September 1, 2014
Answered

switch numbers comma-dot and viceversa

  • September 1, 2014
  • 1 reply
  • 3729 views

hello

I have this script that changes the numbers format from european to UK/US  (for example 1.000 to 1,000) and doesn’t apply to the numbers preceded by any letter.

at the moment it partially works fine, changing only those numbers preceded by letters  without the space between them. for example: machine model TSK4.500 won’t be changed into TSK4,500 and that’s ok.

I need  it to recognize and not change also those numbers preceded by letters with a space:

TSK 4.500 must not be switched.

So in few words, I need this script to work only with numbers avoiding those that might be part of a name etc.

can someone please help me?

thank you very much

Cinzia

try {main ()}

catch (_){alert ("Can't run.")}

function main (){

  function FindItems (){

  if (app.selection.length == 0) {return app.activeDocument.findGrep();}

  if (app.selection[0].hasOwnProperty ("baseline")) {return app.selection[0].findGrep();}

  if (app.selection[0] instanceof TextFrame) {return app.selection[0].parentStory.findGrep();}

  exit ();

  } // FindItems

  // BEGIN main

  app.findGrepPreferences = app.changeGrepPreferences = null;

  app.findGrepPreferences.findWhat = "\\d+[,.\\d]+\\d+";

    //app.findGrepPreferences.findWhat = "[?\\l\\u]+\\d+[,.\\d]";

    //app.findGrepPreferences.findWhat = "[![\\l\\u]]+\\d+[,.\\d]";

    //app.findGrepPreferences.findWhat = "\\d+[,.\\d]";

  var Found = FindItems ();

  var Temp;

  for (var i = Found.length-1; i >= 0; i--)

  {

  Temp = Found.contents.replace (/,/g, "#");

  Temp = Temp.replace (/\./g, ",");

  Found.contents = Temp.replace (/#/g, ".");

         //alert(Temp);

  }

      

        app.findGrepPreferences.findWhat = "[\\l\\u]+\\d+[,.\\d]";

    //app.findGrepPreferences.findWhat = "\\d+[,.\\d]";

  var Found = FindItems ();

  var Temp;

  for (var i = Found.length-1; i >= 0; i--)

  {

  Temp = Found.contents.replace (/,/g, "#");

  Temp = Temp.replace (/\./g, ",");

  Found.contents = Temp.replace (/#/g, ".");

         //alert(Temp);

  }

Correct answer Obi-wan Kenobi

ok so now it works, finally! thank you very much Obi!

let's make it clearer  if anyone else may need it :  here is the script I use for changing ONLY "real" numbers in a table and those preceded by letters, that are part of name or sentence or whatever,  are not switched. (see for example the pic of the table I posted above)

try {main ()}

catch (_){alert ("Can't run.")}

function main (){

  function FindItems (){

  if (app.selection.length == 0) {return app.activeDocument.findGrep();}

  if (app.selection[0].hasOwnProperty ("baseline")) {return app.selection[0].findGrep();}

  if (app.selection[0] instanceof TextFrame) {return app.selection[0].parentStory.findGrep();}

  exit ();

  }

  app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = /(?<=^)-*((\d+)([,\.](\d+))+)/.source;

  var Found = FindItems ();

  var Temp;

  for (var i = Found.length-1; i >= 0; i--)

  {

  Temp = Found.contents.replace (/,/g, "#");

  Temp = Temp.replace (/\./g, ",");

  Found.contents = Temp.replace (/#/g, ".");

  }

}

app.findGrepPreferences = app.changeGrepPreferences = null;


Hi Cinzia,

As I told you in private, I think this grep code is better. Only this line to change:

app.findGrepPreferences.findWhat = /^-*[\d,.]+\d/.source;

1 reply

mdegamo
Inspiring
September 2, 2014

add " *"
app.findGrepPreferences.findWhat = "[\\l\\u]+ *\\d+[,.\\d]";

cinzia_pnAuthor
Participating Frequently
September 3, 2014

thanks very much Mark, but that doesn't work.

now it changes every number 

Obi-wan Kenobi
Legend
September 3, 2014

Hi,

I think the way is to play with the CAPITALS!

I don't know the content of your book, but I treat the problem with 2 regex:

1/ I treat all:

2/ I correct the entries: