Copy link to clipboard
Copied
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);
}
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;
Copy link to clipboard
Copied
add " *"
app.findGrepPreferences.findWhat = "[\\l\\u]+ *\\d+[,.\\d]";
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
thanks very much Mark, but that doesn't work.
now it changes every number
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
In 1 step [to be validated on your document]:
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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, ".");
}
}
Copy link to clipboard
Copied
uhm...absolutely nothing happens now ehe
Copy link to clipboard
Copied
??
It works for me! I've modified the grep code (better!). So:
Before script:
After Script:
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, ".");
}
}
Copy link to clipboard
Copied
I've tested this new code on:
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, ".");
}
}
Copy link to clipboard
Copied
@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
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Do you play directly the script from ESTK?
Copy link to clipboard
Copied
I think YES! The problem is your Target!!! Not correct! For me who use Adobe InDesign CC 2014:
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
OK! I test it with ID CS6 PC and I tell you in some minutes!
Copy link to clipboard
Copied
Sorry!…………………… Works fine!
I've saved the script in .jsx. Give me an email (in private). I send it to you (if you want)!
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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!
I launch the script from ESTK:
ERROR!
I've changed the target: Adobe InDesign CS6!
I launch the script from ESTK:
GOAL!
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
I've tested it and It works the same way.
so what's the difference between the two?
Copy link to clipboard
Copied
The latest version: simple and pure code!