Copy Photoshop Layer Text to Tag with Regular Expression

Started by Stephen Marsh, May 17, 2017, 04:34:59 AM

Previous topic - Next topic

Stephen Marsh

I'm not having much luck copying a Photoshop layer text content to another tag.

I can see that the source tag is correct, namely:

exiftool -XMP-photoshop:TextLayerText 'FILE.psd'


or

exiftool -Photoshop:LayerUnicodeNames 'FILE.psd'


Output for example could be:

Layer 0, Layer 1, Layer 2


And I only need the bottom, first named layer content in the stack – such as: Layer 0 (removing the first comma and everything afterwards)... so a regular expression similar in part to:

,.*
or
(?!^),.*

Should be part of the answer.

However, even without tring to add the RegEx, I am having no luck:

exiftool '-title<TextLayerText' 'FILE.psd'
or
exiftool '-title<${Photoshop:LayerUnicodeNames}' 'FILE.psd'

Errors with:

Error: Bad Photoshop IRB resource "MeSa"
0 image files updated
1 files weren't updated due to errors

Phil Harvey

Hi Stephen,

The Bad IRB resource is unrelated to what you are trying to write.  Could you send me the sample PDF so I can take a look?  My email is philharvey66 at gmail.com

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Stephen Marsh

#2
Thanks Phil, I have recreated the test files and yes, the command now works... So now all I need to do is to work out the regex to keep the all characters before the first comma and remove all characters after the first comma. I believe that I know the regex, however trying to work out the syntax to include this into the command is a little trickier.

Yes, I'll email the two test files to you, they were created from Photoshop CC17 using variables/data sets (data driven graphics).

Phil Harvey

I got the files, thanks.

I've found a reference for the new MeSa IRB, and I'll add support for this in ExifTool 10.53 so you will be able to write these files with this version of ExifTool.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Phil Harvey

...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Stephen Marsh

Thank you Phil.

I have searched the forums and although I can find references to achieving specific tasks using regular expressions, I can't seem to achieve the following:

Layer Zero, Layer 1, Next Layer, And another one

Simply removing everything including the first comma and all remaining characters.

I know my regex works, however I am getting caught up in the ExifTool syntax.

,.*
or
(?!^),.*

Can somebody please point me to posts explaining the syntax or show me where I am going wrong?

StarGeek

There are a variety of ways to do that.  You could try something like
exiftool '-title<${TextLayerText;s/(^[^,]*).*/$1/}' 'FILE.psd'
Here's how it works on Regex101.  You can see the step by step break down in the upper right corner.

Another way is
exiftool '-title<${TextLayerText;s/(^.*?),.*/$1/}' 'FILE.psd'
Regex101 again

Edit: Fixing your expression would be this
exiftool '-title<${TextLayerText;s/,.*//}' 'FILE.psd'
Regex101
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Stephen Marsh

Thank you StarGeek!

Your last example is pretty much what I was expecting:

exiftool '-title<${TextLayerText;s/,.*//}' 'FILE.psd'

At least for the pure RegEx part, however I am just trying to work out the ExifTool/Perl syntax around it...

So, we need to add:

${

Then at the end of the source tag we add:

;s

Then an opening:

/

Now we plug in our regex:

,.*

Followed by two closing (escaped?):

//

Finally a closing brace/curly bracket:

}

Although it would be helpful to understand why, I don't really need to know if all I have to do is plug-in my regex into such a structure. Not understanding the required syntax, I just need to work out the "scaffolding" around the regex that I know works.

I'll need to study your other examples a little deeper, this is the great thing about regular expressions, so many ways to the same end...

StarGeek

Quote from: Stephen Marsh on May 17, 2017, 07:34:46 PM
Then at the end of the source tag we add:

;s

Then an opening:

/

Followed by two closing (escaped?):

//

Not quite.  The semicolon is there to mark the end of the tag name and start of the advanced formatting.  Everything following that should be perl statements, which have a semicolon as a statement delimiter.

The actual perl statement is regex substitution, which is s/Search/Replace/options;.   The slashes are standard, but Perl doesn't require you to use slashes.  You can use other characters.  For example, you'll often Phil use parenthesis () (example) instead of slashes.  So your command could just as easily be exiftool '-title<${TextLayerText;s(,.*)()}' 'FILE.psd'.  The reason you might use something else is because if you have too many slashes you have to escape in your regex, it becomes hard to read (see Leaning toothpick syndrome).

Ah, here's a good post from Phil. 

QuoteI'll need to study your other examples a little deeper, this is the great thing about regular expressions, so many ways to the same end...

I often recommend Regular-Expressions.info as a site to learn about regular expressions.  And Regex101.com is a great site where you can test out your regex. 
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Stephen Marsh

Thank you StarGeek, that is really helpful and a great place to start in working this all out, cheers!