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

A script that substitutes place of numbers

Explorer ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

404

Translate

Translate

Report

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 3 Correct answers

Community Expert , May 02, 2022 May 02, 2022

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

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

 

Votes

Translate

Translate
Community Expert , May 02, 2022 May 02, 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 )

Votes

Translate

Translate
Community Expert , May 03, 2022 May 03, 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 )

Votes

Translate

Translate
Community Expert ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

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

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

 

Votes

Translate

Translate

Report

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
Explorer ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
May 02, 2022 May 02, 2022

Copy link to clipboard

Copied

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 )

Votes

Translate

Translate

Report

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 ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

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

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

 

 

Votes

Translate

Translate

Report

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
Explorer ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

LATEST

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 )

Votes

Translate

Translate

Report

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