Help with EXIFTOOL and JavaScript
Hi there,
I'm new to Advanced Renamer and I'm looking for a method to rename photo files based on some exif tags.
Eg. the fujifilm Tag "AutoBracketing" and "SequenceNumber".
I'm used to exiftool, but new to JavaScript and the combination with Advanced Renamer.
Can somebody help me with a simple example for a script to substitute parts of the filename regarding a combination of both tags? That would help me to get started easier.
Perhaps: If the Tag "AutoBracketing" is "on" and SequenceNumber is "2", than replace a part of the filename with "-BKT" & SequenceNumber.
Thank's for help
Ulrich
I'm new to Advanced Renamer and I'm looking for a method to rename photo files based on some exif tags.
Eg. the fujifilm Tag "AutoBracketing" and "SequenceNumber".
I'm used to exiftool, but new to JavaScript and the combination with Advanced Renamer.
Can somebody help me with a simple example for a script to substitute parts of the filename regarding a combination of both tags? That would help me to get started easier.
Perhaps: If the Tag "AutoBracketing" is "on" and SequenceNumber is "2", than replace a part of the filename with "-BKT" & SequenceNumber.
Thank's for help
Ulrich
Reply to #1:
Welcome Ulrich!
This should get you started:
1) Create a new script method ( https://www.advancedrenamer.com/user_guide/v4/method_script ).
2) Paste this next code in the "function(index, item)" block and apply it:
//-------------------------------------------------------
// "function( index, item)" script
//-------------------------------------------------------
let bracketing = app.parseTags("<ExifTool:AutoBracketing>");
if (/^on$/i.test (bracketing) ) { // Is the whole value "On"? case-insensitive
let seqNum = app.parseTags("<ExifTool:SequenceNumber>");
return item.newBasename + "-BKT " + seqNum; // append to the filename
}
//-------------------------------------------------------
For now it adds "-BKT {sequence Number}" to the end of the filename.
If AutoBracketing is not "On" -- or is missing -- the filename will be unchanged.
Regards,
Randy
Welcome Ulrich!
This should get you started:
1) Create a new script method ( https://www.advancedrenamer.com/user_guide/v4/method_script ).
2) Paste this next code in the "function(index, item)" block and apply it:
//-------------------------------------------------------
// "function( index, item)" script
//-------------------------------------------------------
let bracketing = app.parseTags("<ExifTool:AutoBracketing>");
if (/^on$/i.test (bracketing) ) { // Is the whole value "On"? case-insensitive
let seqNum = app.parseTags("<ExifTool:SequenceNumber>");
return item.newBasename + "-BKT " + seqNum; // append to the filename
}
//-------------------------------------------------------
For now it adds "-BKT {sequence Number}" to the end of the filename.
If AutoBracketing is not "On" -- or is missing -- the filename will be unchanged.
Regards,
Randy
Reply to #1:
Hi guys,
I'd already done this when I saw Randy had answered, so I'll throw it out there; it may give you a little more insight into javascript. If it just confuses you, please ignore! :)
I envisioned a file structure: IMG_1234 oldStuff keepThis.jpg
(In future try to include samples of existing filenames and examples of what the transformation should look like - that will make our job much easier and more precise). Make sure the "Apply to:" field is set to "Name":
if ( app.exifToolValue("AutoBracketing") == "On" && app.exifToolValue( "SequenceNumber" ) == "2" ) {
return item.newBasename.replace( /oldStuff/, "-BKT2" ) ;
}
This will yield "IMG_1234 -BKT2 keepThis.jpg"
You could replace the "/oldStuff/" with "/ .* /" (two spaces, no quotes) if there were only the two spaces in the filename.
NOTE: I don't have those exifTool fields, so I did this with other values. It "should" work fine, but no guarantees...
Best,
DF
EDIT: Feel free to give example filenames/results, we can be more specific. :)
Hi guys,
I'd already done this when I saw Randy had answered, so I'll throw it out there; it may give you a little more insight into javascript. If it just confuses you, please ignore! :)
I envisioned a file structure: IMG_1234 oldStuff keepThis.jpg
(In future try to include samples of existing filenames and examples of what the transformation should look like - that will make our job much easier and more precise). Make sure the "Apply to:" field is set to "Name":
if ( app.exifToolValue("AutoBracketing") == "On" && app.exifToolValue( "SequenceNumber" ) == "2" ) {
return item.newBasename.replace( /oldStuff/, "-BKT2" ) ;
}
This will yield "IMG_1234 -BKT2 keepThis.jpg"
You could replace the "/oldStuff/" with "/ .* /" (two spaces, no quotes) if there were only the two spaces in the filename.
NOTE: I don't have those exifTool fields, so I did this with other values. It "should" work fine, but no guarantees...
Best,
DF
EDIT: Feel free to give example filenames/results, we can be more specific. :)
Reply to #3:
Yeah, crossing posts is annoying; sorry.
Anywho, DF, a code review if you want it:
The op specified the value as "on".
You and I know that the exact value was *probably* "On". But such things are not always true with file tags and vary from vendor to vendor and even from one SW/FW version to the next.
So, case-sensitive tests like == "On" are not robust when used against "untamed" strings.
If you don't want any more feedback like that you can tell me to [censored] but I might ignore it, if it is not capitalized properly. ;)
Fondly,
Randy
Yeah, crossing posts is annoying; sorry.
Anywho, DF, a code review if you want it:
The op specified the value as "on".
You and I know that the exact value was *probably* "On". But such things are not always true with file tags and vary from vendor to vendor and even from one SW/FW version to the next.
So, case-sensitive tests like == "On" are not robust when used against "untamed" strings.
If you don't want any more feedback like that you can tell me to [censored] but I might ignore it, if it is not capitalized properly. ;)
Fondly,
Randy
Reply to #4:
Hi Randy,
Absolutely valid, my friend. And you should know me well enough by now to know I don't mind being critiqued (but I'll probably just ignore it anyway! :)).
Seriously, you're absolutely right. I missed it in the OP, and I don't have those values on any files here, so I just went ahead and did whatever I wanted. Of course a lot of exifTool data values are first-letter-caps, but then there's stuff like ColorSpace = sRGB and ResolutionUnit = inches. Anyway, I appreciate the feedback.
bEsT,
df
Hi Randy,
Absolutely valid, my friend. And you should know me well enough by now to know I don't mind being critiqued (but I'll probably just ignore it anyway! :)).
Seriously, you're absolutely right. I missed it in the OP, and I don't have those values on any files here, so I just went ahead and did whatever I wanted. Of course a lot of exifTool data values are first-letter-caps, but then there's stuff like ColorSpace = sRGB and ResolutionUnit = inches. Anyway, I appreciate the feedback.
bEsT,
df