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

switch numbers comma-dot and viceversa

New Here ,
Sep 01, 2014 Sep 01, 2014

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);

  }

TOPICS
Scripting
2.7K
Translate
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

LEGEND , Sep 06, 2014 Sep 06, 2014

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;

Translate
Explorer ,
Sep 02, 2014 Sep 02, 2014

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

Translate
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
LEGEND ,
Sep 02, 2014 Sep 02, 2014

Hi,

If somebody can tell me the difference between:

"I see 1.000 kinds of solutions!"

"TSK 4.500 must not be switched."

  For me: None!

Translate
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
New Here ,
Sep 03, 2014 Sep 03, 2014

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

now it changes every number 

Translate
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
LEGEND ,
Sep 03, 2014 Sep 03, 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:

Capture d’écran 2014-09-03 à 10.30.48.png

2/ I correct the entries:

Capture d’écran 2014-09-03 à 10.31.11.png

Translate
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
LEGEND ,
Sep 03, 2014 Sep 03, 2014

In 1 step [to be validated on your document]: 

Capture d’écran 2014-09-03 à 10.55.30.png

Translate
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
New Here ,
Sep 04, 2014 Sep 04, 2014

thank you for the suggestion Obi,

but unfortunately I don't know much about grepping and coding , and I think that working on that script I posted would be the best solution for the indesign documents I have.

i have attached a screenshot of the table I'm working on to make it a bit more clear.

the script I have works fine, changing all the formats of all numbers, and NOT changing the TSK212.1 for example.and  that's perfect .but if I get a TSK 212.1 (with a space in the middle) that will be switched to TSK 212,1 which is not good. that is not a number. it's name. (this is not the case, but I know that will happen in the upcoming tables...)

I will have to send the script to another person if I get it work so it should be in a script that only needs a click to make everything ok.

thanks very much to anyone who can help me!

table.jpg

Translate
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
LEGEND ,
Sep 04, 2014 Sep 04, 2014

Hi,


Not a [JS] specialist! 


You can try this:

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 = "((?<=\\l\\s)|(?<=\\l)|(?<=^)|(?<=\\())(\\d+)\\.(\\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, ".");

  }

}

Translate
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
New Here ,
Sep 04, 2014 Sep 04, 2014

uhm...absolutely nothing happens now ehe

Translate
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
LEGEND ,
Sep 04, 2014 Sep 04, 2014

??

It works for me!  I've modified the grep code (better!). So:

Before script:

Capture d’écran 2014-09-05 à 08.09.43.png

Capture d’écran 2014-09-05 à 08.09.55.png

After Script:

Capture d’écran 2014-09-05 à 08.10.15.png

Code:

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 = "((?<=\\l\\s)|(?<=\\l))((\\d)\\.(\\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, ".");

  }

}

Translate
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
LEGEND ,
Sep 05, 2014 Sep 05, 2014

I've tested this new code on:

Capture d’écran 2014-09-05 à 10.56.26.png

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 = "((?<=\\l\\s)|(?<=\\l)|(?<=^))((\\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, ".");

  }

}

Translate
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
Community Expert ,
Sep 05, 2014 Sep 05, 2014

@Obi-wan – Or you could write it this way:

app.findGrepPreferences.findWhat = /((?<=\l\s)|(?<=\l))((\d+)(\.(\d+))+)/.source;

Best practice is to clean the GREP preferences also *after* running the main script with:

app.findGrepPreferences = app.changeGrepPreferences = null;

(In case another script from someone else will immediately run after yours with GREP Search/Replace not wiping the preferences; or the user want to do a GREP Search/Replace in the GUI and finds it annoying removing your GREP first…)

Uwe

Translate
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
LEGEND ,
Sep 05, 2014 Sep 05, 2014

Hi Uwe,

So:

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 = /((?<=\l\s)|(?<=\l)|(?<=^))((\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;

Thanks! 

Translate
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
New Here ,
Sep 05, 2014 Sep 05, 2014

at this point it must be something wrong with me...and how I edit that script

I'm attaching the screenshot from the javascript editor in indesign .

i have pasted your script above but again it doesn't work properly, changing every dot into comma and viceversa.Schermata 2014-09-05 a 14.13.37.png

Translate
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
LEGEND ,
Sep 05, 2014 Sep 05, 2014

Do you play directly the script from ESTK?

Translate
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
LEGEND ,
Sep 05, 2014 Sep 05, 2014

I think YES! The problem is your Target!!! Not correct! For me who use Adobe InDesign CC 2014:

Capture d’écran 2014-09-05 à 14.20.07.png

Translate
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
New Here ,
Sep 05, 2014 Sep 05, 2014

no, I click on the script name from the script panel in indesign .

anyway I have changed the target to ID6 but it keeps changing everything

Translate
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
LEGEND ,
Sep 05, 2014 Sep 05, 2014

OK! I test it with ID CS6 PC and I tell you in some minutes! 

Translate
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
LEGEND ,
Sep 05, 2014 Sep 05, 2014

Sorry!…………………… Works fine!

I've saved the script in .jsx. Give me an email (in private). I send it to you (if you want)!

Translate
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
New Here ,
Sep 05, 2014 Sep 05, 2014

AH na can't believe it !

but glad you made it work !

i'm writing you in pvt.

i'll test it at home and send to someone else to see if it works.

thanks very very very much for your help!

Translate
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
LEGEND ,
Sep 05, 2014 Sep 05, 2014

As I've already said, I pilot here the script with ESTK but if I launch it from InDesign, it works fine too (with ID CS6 PC, ID CC and ID CC 2014 MAC)!

The target is not correct: here, ExtendScript Toolkit CS6!

Capture1.JPG

I launch the script from ESTK:

Capture3.JPG

ERROR!

I've changed the target: Adobe InDesign CS6!

Capture2.JPG

Capture4.JPG

I launch the script from ESTK:

Capture5.JPG

GOAL! 

Translate
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
New Here ,
Sep 06, 2014 Sep 06, 2014

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;

Translate
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
LEGEND ,
Sep 06, 2014 Sep 06, 2014

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;

Translate
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
New Here ,
Sep 06, 2014 Sep 06, 2014

I've tested it and It works the same way.

so what's the difference between the two?

Translate
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
LEGEND ,
Sep 06, 2014 Sep 06, 2014

The latest version: simple and pure code!

Translate
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