Skip to main content
Known Participant
May 3, 2022
Answered

A script that substitutes place of numbers

  • May 3, 2022
  • 3 replies
  • 749 views

In my document, there are plenty of texts with this look: 
(XXXX-YYYY)
two four-digit numbers that are separated with a hyphen. I'm looking for a script that substitute the place of these two numbers, so the final result would be:
(YYYY-XXXX)
Could someone please help me with that?

This topic has been closed for replies.
Correct answer Laubender

Could you post the URL where this happened?

 

You could also use my suggestion for Find GREP that is a bit tighter.

 

For the script exchange this:

var f = "(\\d{4})-(\\d{4})";

with:

var f = "\\<\\d{4}-\\d{4}\\>" ;

 

Regards,
Uwe Laubender

( ACP )

3 replies

brian_p_dts
Community Expert
Community Expert
May 3, 2022

Reworking my similar response here: https://community.adobe.com/t5/indesign-discussions/id-script-to-increase-numbers/m-p/12884070#M473934

Edit, sorry, had my logic wrong. It's fixed now. 

 

var d = app.activeDocument;
var f = "(\\d{4})-(\\d{4})";
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = f;
var fs = d.findGrep();
var c = 0;
var t, i, fir, sec;
for (var i = 0; i < fs.length; i++) {
    t = fs[i].contents.split("-");
    fir = t[0];
    sec = t[1];
    if (Number(fir) > Number(sec)) { 
        fs[i].contents = sec + "-" + fir;
        c++;
    }
}
alert(c + " changes made");

 

 

Known Participant
May 4, 2022

Thanks for your code. It works great, but there's a slight problem. The code doesn't look for digits only in the prantheses, as you see in the picture that it founded two four-digit numbers that are actually numbers of a URL. 
The digits that only need swapping are in the parentheses. It must only go through the digits that are between parentheses, which means that (XXXX-YYYY) must be swapped, but XXXX-YYYY must not. 
I tried to modify yours, but since I'm a newbie I couldn't.

LaubenderCommunity ExpertCorrect answer
Community Expert
May 4, 2022

Could you post the URL where this happened?

 

You could also use my suggestion for Find GREP that is a bit tighter.

 

For the script exchange this:

var f = "(\\d{4})-(\\d{4})";

with:

var f = "\\<\\d{4}-\\d{4}\\>" ;

 

Regards,
Uwe Laubender

( ACP )

Community Expert
May 3, 2022

You want to switch the order of the two number blocks?

 

Should be doable with a GREP Find/Change action if the formatting for both number blocks is exactly the same.

Is that the case?

 

Try this pattern for GREP Find:

\<(\d{4})(-)(\d{4})\>

And this one for GREP Change:

$3$2$1

 

Regards,
Uwe Laubender

( ACP )

brian_p_dts
Community Expert
Community Expert
May 3, 2022

You can just use GREP in your Find/Replace panel:

Find: (\d{4})-(\d{4})
Change: $2-$1

 

Known Participant
May 3, 2022

Thanks a million for your help.
Out of curiosity, I'm thinking of improving it., and adding a condition to it. This means that if the number XXXX is greater than YYYY, swap their positions, and if XXXX is less than YYYY, do nothing. I don't think that is practical with the find/replace feature, and can be implemented with scripts.