Skip to main content
Inspiring
November 26, 2018
Question

Search and replace by RegEx/GREP

  • November 26, 2018
  • 4 replies
  • 1838 views

Hi.

Can somebody tell me if Photoshop allows the use of RegEx/GREP in a script?

I need to change prizes in a document.

something like this:

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = "^(\\d{1,3})(,)(\\d{2})";

app.changeGrepPreferences.changeTo = myArtPrizeEuro +"$2" + myArtPrizeCent;

app.textItem.activeDocument.changeGrep();

       

app.findGrepPreferences = NothingEnum.nothing;

app.changeGrepPreferences = NothingEnum.nothing;

This topic has been closed for replies.

4 replies

cmoke73Author
Inspiring
November 28, 2018

Why does this not work? (the content of the TextLayer in PS is: "ab 0,00 €")

var d = prizeLayer.textItem.contents[3].replace("15");

If I check it with "alert(prizeLayer.textItem.contents[3]);" the result ist the 4th character in that string = "0".

But it won´t change the content....

Following snippet is working, but changing the whole content and so changing the character style:

var myArtPrizeEuro = prompt ("Euro", undefined, "EURO")

var myArtPrizeCent = prompt ("Cent", undefined, "CENT")

var re = /(ab\s)(0,)(00\s)(€)/;

var str = prizeLayer.textItem.contents.toString();

var newstr = str.replace(re, "$1" + myArtPrizeEuro + "," + myArtPrizeCent + " " + "$4");

prizeLayer.textItem.contents = newstr;

Tom Ruark
Inspiring
November 28, 2018

There is the RegExp object. But I'm not seeing anything in the docs about it. There is some info in the object model viewer from the help menu.

Legend
November 28, 2018

The RegExp Object is part of the JavaScript Core, and very often not described in specific variants of JavaScript.

You might look at a general JavaScript documentation which does include the Core.

Stephen Marsh
Community Expert
Community Expert
November 28, 2018

Does this help?

Re: [JS] rename files without opening in PS

Both .rename(); and .getfiles(); and .replace(); support regular expressions and I’m sure that other JavaScript objects do too (but I’m just a newb to scripting)...

How about string.replace();.– as in:

var string = 'abc456';

string.replace(/\D+/,123);

Find: abc

Replace: 123

Before: abc456

After: 123456

w3schools – JavaScript RegExp Object

But what are the actual “prizes”… Are they layer names, actual text layer content or something else?

cmoke73Author
Inspiring
November 28, 2018

The prizes are text-layers and its contents in a Photoshop document:

So I have to change the first zero to the Euro-amount from the dialog box and the both smaller zeros to the Cent-value without changing the style.

pixxxelschubser
Community Expert
Community Expert
November 28, 2018

Hallo

Zuerst eine ernst gemeinte Frage:

Warum macht ihr das nicht in InDesign?

Wenn schon in Photoshop, dann:

Gib bitte deine Version an!

Die praktikabelste Funktion (ohne Skript) ist Variablen und Datensätze. Lies dich da bitte einmal ein.

Und wenn es dann doch ein Skript sein muß:

Stelle uns bitte ein Beispieldokument zur Verfügung (vereinfacht und ohne private Inhalte). Hauptsache die Ebenenstruktur stimmt und die Anordnung plus Aufbau der Textfelder ist gut erkennbar. Und eine Datei aus der man die Änderungen der Preise auslesen kann.

Letzte Frage:

Verwendest du eigentlich Zeichen- und Absatzformate?

JJMack
Community Expert
Community Expert
November 26, 2018

Grep is a Unix command line command for searching text files.  The only global text command I know of in Photoshop is for a single document text layers its Photoshop spell checker.    You may be able to programs a function to find and replace text strings.  However,  I would think supporting regular expressions in find and replace would be very hard to do.  Not possible for a hacker like me that does not know JavaScript or regulate expressions syntax.   I have never seen any documentation that had a Grep like function for JavaScript or Adobe DOM scripting.   For Web sites there is jquery to help you use with html in web browsers.

JJMack
cmoke73Author
Inspiring
November 26, 2018

that´s a pity.

All I wanted to do is to change a prize in a PS-document from default to the content of a dialog-input. The dialog I created has one input field (edittext) for Euro and another one for Cent.The problem is, that in the doc I have a text with 3 different character styles. So I cannot change the whole text in one because afterwards it gets only one style. I have to change the part with "Euro"s and, separated from it, the part with "Cent"s. I wanted to get them through GREP.

Hmm.... checkmate(?)