Script aborts when accessing item.imgDate
I have a script that I have been using for quite some years now. It checks whether the item has an item.imgDate - if so use that for renaming, otherwise it uses the item.modifiedDate. So far so good.
Somehow since I have a new phone there are sometimes images, that make the script crash. It will simply abort processing without any further notice. The issue appears between the log messages "a" and "b" and the catch is not triggered.
This is really annoying, since I have to remove all the trouble files from the list for the script to work. Any ideas, what's going wrong.
Here is the script:
----------------------------------------
if(item == null)
{
console.log(" -> something went wrong: item is null");
return "fehler";
}
console.log(index + ": " + item.filename);
//item.ext = item.ext.toLowerCase();
// select date according depending on whether it is an image or not
var date;
try
{
console.log("a");
console.log(index + ": " + item.imgDate);
console.log("b");
}
catch(e)
{
console.log(" -> something went wrong: cannot access imgDate");
}
if((item.imgDate != null) && (item.imgDate.getTime() > 0)) // only images will have a valid date
{
console.log(" -> using imgDate = " + item.imgDate);
date = item.imgDate;
}
else
{
console.log(" -> using modifiedDate = " + item.modifiedDate);
date = item.modifiedDate;
}
if(date == null)
{
console.log(" -> something went wrong: date is null");
return item.filename;
}
// helper function
var fill = function(str, len) {
var s = "" + str;
while(s.length < len)
s = "0" + s;
return s;
};
if(offset)
{
date.setFullYear( date.getFullYear() + offset_year);
date.setMonth( date.getMonth() + offset_month);
date.setDate( date.getDate() + offset_date);
date.setHours( date.getHours() + offset_hour);
date.setMinutes( date.getMinutes() + offset_minute);
date.setSeconds( date.getSeconds() + offset_second);
}
// format the date
var dateStr = (date.getFullYear())
+ "." + fill(date.getMonth() + 1,2)
+ "." + fill(date.getDate(),2)
+ " " + fill(date.getHours(),2)
+ "." + fill(date.getMinutes(),2)
+ "." + fill(date.getSeconds(),2);
console.log(" -> adjusted date = " + dateStr);
return dateStr;
Somehow since I have a new phone there are sometimes images, that make the script crash. It will simply abort processing without any further notice. The issue appears between the log messages "a" and "b" and the catch is not triggered.
This is really annoying, since I have to remove all the trouble files from the list for the script to work. Any ideas, what's going wrong.
Here is the script:
----------------------------------------
if(item == null)
{
console.log(" -> something went wrong: item is null");
return "fehler";
}
console.log(index + ": " + item.filename);
//item.ext = item.ext.toLowerCase();
// select date according depending on whether it is an image or not
var date;
try
{
console.log("a");
console.log(index + ": " + item.imgDate);
console.log("b");
}
catch(e)
{
console.log(" -> something went wrong: cannot access imgDate");
}
if((item.imgDate != null) && (item.imgDate.getTime() > 0)) // only images will have a valid date
{
console.log(" -> using imgDate = " + item.imgDate);
date = item.imgDate;
}
else
{
console.log(" -> using modifiedDate = " + item.modifiedDate);
date = item.modifiedDate;
}
if(date == null)
{
console.log(" -> something went wrong: date is null");
return item.filename;
}
// helper function
var fill = function(str, len) {
var s = "" + str;
while(s.length < len)
s = "0" + s;
return s;
};
if(offset)
{
date.setFullYear( date.getFullYear() + offset_year);
date.setMonth( date.getMonth() + offset_month);
date.setDate( date.getDate() + offset_date);
date.setHours( date.getHours() + offset_hour);
date.setMinutes( date.getMinutes() + offset_minute);
date.setSeconds( date.getSeconds() + offset_second);
}
// format the date
var dateStr = (date.getFullYear())
+ "." + fill(date.getMonth() + 1,2)
+ "." + fill(date.getDate(),2)
+ " " + fill(date.getHours(),2)
+ "." + fill(date.getMinutes(),2)
+ "." + fill(date.getSeconds(),2);
console.log(" -> adjusted date = " + dateStr);
return dateStr;
Welcome Julian.
While it's true that item does not act like a proper JavaScript object (Object.keys() behavior, etc.), I cannot duplicate your issue.
And try/catch structures do work for me in Aren.
What version of Aren are you using? What OS?
Is it possible for you to share a file that triggers the problem?
Meanwhile, I recommend just switching to a more standard tag and see if that works better.
Use item.DateTaken rather than item.imgDate.
Regards,
Randy
PS: by clicking on the "i" icon in the left-hand column, you can see what data is available for the highlighted/selected file.
See https://www.advancedrenamer.com/user_guide/v4/exiftool
While it's true that item does not act like a proper JavaScript object (Object.keys() behavior, etc.), I cannot duplicate your issue.
And try/catch structures do work for me in Aren.
What version of Aren are you using? What OS?
Is it possible for you to share a file that triggers the problem?
Meanwhile, I recommend just switching to a more standard tag and see if that works better.
Use item.DateTaken rather than item.imgDate.
Regards,
Randy
PS: by clicking on the "i" icon in the left-hand column, you can see what data is available for the highlighted/selected file.
See https://www.advancedrenamer.com/user_guide/v4/exiftool
Reply to #2:
Hey Julian,
Your script didn't run (for me) in either v3 or v4.
You could save yourself some headaches, use tag fallback and date tag formatting & offsetting in the latest versions of ARen:
//--------------------------------------------------------------------
let newDate = app.parseTags( '<Img DateOriginal||Img DateCreate||DateTaken||DateTime||DateTimeDigitized||DateTimeOriginal||GPSDateStamp:yyyy-mm-dd_hh-nn-ss:-1h>' ) ;
// the "-1h" at the end sets the hour back by one, as an example.
// If no usable date/time, go to exifTool:
if ( newDate == null ) {
newDate = app.parseTags( '<ExifTool:FileCreateDate>' ) ;
}
// if above values yield "" or "0000-00-00 00-00-00" (I have a lot of those from previous fiddling)
if ( newDate == "" || /^0000/.test( newDate ) ) {
newDate = app.exifToolValue( 'FileCreateDate' ) ; // a different way to incorporate exifTool fields
}
return newDate ;
//------------------------------------------------------------------
DateTime tags and formatting: https://www.advancedrenamer.com/user_guide/v4/tags_datetime
Tag fallback: https://www.advancedrenamer.com/user_guide/v4/tag_fallback
Tag modifiers: https://www.advancedrenamer.com/user_guide/v4/tag_modifiers
Best,
DF
Hey Julian,
Your script didn't run (for me) in either v3 or v4.
You could save yourself some headaches, use tag fallback and date tag formatting & offsetting in the latest versions of ARen:
//--------------------------------------------------------------------
let newDate = app.parseTags( '<Img DateOriginal||Img DateCreate||DateTaken||DateTime||DateTimeDigitized||DateTimeOriginal||GPSDateStamp:yyyy-mm-dd_hh-nn-ss:-1h>' ) ;
// the "-1h" at the end sets the hour back by one, as an example.
// If no usable date/time, go to exifTool:
if ( newDate == null ) {
newDate = app.parseTags( '<ExifTool:FileCreateDate>' ) ;
}
// if above values yield "" or "0000-00-00 00-00-00" (I have a lot of those from previous fiddling)
if ( newDate == "" || /^0000/.test( newDate ) ) {
newDate = app.exifToolValue( 'FileCreateDate' ) ; // a different way to incorporate exifTool fields
}
return newDate ;
//------------------------------------------------------------------
DateTime tags and formatting: https://www.advancedrenamer.com/user_guide/v4/tags_datetime
Tag fallback: https://www.advancedrenamer.com/user_guide/v4/tag_fallback
Tag modifiers: https://www.advancedrenamer.com/user_guide/v4/tag_modifiers
Best,
DF
Reply to #3:
Huh, I didn't realize that app.parseTags() could be that useful.
Thanks, DF!
Huh, I didn't realize that app.parseTags() could be that useful.
Thanks, DF!
Reply to #4:
Randy,
Yeah, it's a regular Swiss Army Knife... :) I've found it useful with both tags ( especially ones like DateTime tags and <metadata:fieldname> ) and with combining tags with other strings, i.e.,
return app.parseTags( "This is a test <some tag> for Randy" ) ; kind of like `` for including both text and variables.
Glad I could help, my friend!
Best,
DF
Randy,
Yeah, it's a regular Swiss Army Knife... :) I've found it useful with both tags ( especially ones like DateTime tags and <metadata:fieldname> ) and with combining tags with other strings, i.e.,
return app.parseTags( "This is a test <some tag> for Randy" ) ; kind of like `` for including both text and variables.
Glad I could help, my friend!
Best,
DF
Hi everyone (Randy W, Delta Foxtrot) and thanks for your help and your comprehensive replies.
Reply to #2:
- I am using 3.88 on Windows and feel a little embarassed now that I haven't updated for so long. I'll try a newer version and get back to you. To my defense: the script was working well for some time and the issue was introduced with a new phone and only some specific files (only panorama pictures) so I didn't think of an issue with the actual version, but rather with the files...
Reply to #3:
- wow that parseTags looks awesome. It would simplify things a lot. Does this also allow deltas in years, months, days, minutes, seconds? I have an issue with my old GoPro sometimes completely forgetting the date so I have to correct large offsets.
Reply to #2:
- I am using 3.88 on Windows and feel a little embarassed now that I haven't updated for so long. I'll try a newer version and get back to you. To my defense: the script was working well for some time and the issue was introduced with a new phone and only some specific files (only panorama pictures) so I didn't think of an issue with the actual version, but rather with the files...
Reply to #3:
- wow that parseTags looks awesome. It would simplify things a lot. Does this also allow deltas in years, months, days, minutes, seconds? I have an issue with my old GoPro sometimes completely forgetting the date so I have to correct large offsets.
Reply to #3:
see above - I thought I could reply to both of your messages in one...
see above - I thought I could reply to both of your messages in one...
Reply to #6:
Hi Julian,
You don't have to give up v3 to get v4... just install the latest v4 in a different location. I keep 3.95 and 4.xx (latest) installed, plus a lot of portable versions available. Version 3's js engine has more useful error messages, and I can work with .lnk files in 3 as well, but I find I hardly ever use it anymore since v4 is so versatile.
As to date/time adjustment, see at the bottom of this page: https://www.advancedrenamer.com/user_guide/v4/tags_datetime
All values are adjustable. So much handier than rolling your own. And you can test all the tag stuff in various methods before coding... or just use the New Name or Replace and skip coding on a lot of problems.
Check out the tag section of the user guide in general. There are so many things you can do in tags now that were previously unavailable except through js.
Best,
DF
Hi Julian,
You don't have to give up v3 to get v4... just install the latest v4 in a different location. I keep 3.95 and 4.xx (latest) installed, plus a lot of portable versions available. Version 3's js engine has more useful error messages, and I can work with .lnk files in 3 as well, but I find I hardly ever use it anymore since v4 is so versatile.
As to date/time adjustment, see at the bottom of this page: https://www.advancedrenamer.com/user_guide/v4/tags_datetime
All values are adjustable. So much handier than rolling your own. And you can test all the tag stuff in various methods before coding... or just use the New Name or Replace and skip coding on a lot of problems.
Check out the tag section of the user guide in general. There are so many things you can do in tags now that were previously unavailable except through js.
Best,
DF