Advertisement
Guest User

AR Script: Insert Datetimeoriginal with counter & more

a guest
Oct 6th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   // v1.0 | Add formatted DateTimeOriginal exif tag and file group counter. Author: glambany@gmail.com
  2.   const GROUP_CNT =       true;  // Enable the incrementing number by file group. Each group is of all files with same filename but different extention.
  3.   const CNT_START =             1;     // Value from where the group counter start counting
  4.   const CNT_STEP =        1;     // Counter incrementing steps.
  5.   const CNT_DIGIT_NB =      3;     // Number of digits, padded with zeros if necessary, that the counter number has
  6.   const DTO =             true;  // Enable adding Datetimeoriginal to the filename
  7.   const DTO_LOOKAROUND =  true;  // Enable the datetimeoriginal LOOKAROUND feature. It looks for a datetimeoriginal in other files around (1 up or down) if the current file hasn't any
  8.   const KEEP_FILEN =        true;  // Enable keeping original filename in addition to the new stuff
  9.   // Filename sep.: Date separator, Date prefix separator, Group counter prefix separator
  10.   const DATE_SEP = "-", DATE_PREFIX = "_", CNT_PREFIX = "_";
  11.   const NEWNAME_FOR_CNT = false;    // Check the new name the files have (modified by previous methods) to detect group of files for the counter. If disabled, use the original filename regardless of what was modified of it
  12.  
  13.   // Start of the stuff you shouldn't touch, unless you know what you are doing of course
  14.   var final_filename = "";
  15.  
  16.   // first, try to rename if the file has it's own datetimeoriginal
  17.   if ( get_datetimeoriginal(index) != null )  {
  18.        final_filename = en(item.newBasename + DATE_PREFIX, KEEP_FILEN) + en(get_datetimeoriginal(index, DATE_SEP), DTO) + en(en(CNT_PREFIX, DTO) + zpad(String(counter + CNT_START), CNT_DIGIT_NB), GROUP_CNT);}
  19.   // then, try to find a file around that has a datetimeoriginal, and use it
  20.   else if ( DTO_LOOKAROUND ){
  21.     if        ( index != 0 &&  get_datetimeoriginal(index - 1, DATE_SEP) != null && app.getItem(index - 1).name == item.name ) {
  22.       final_filename = en(item.newBasename + DATE_PREFIX, KEEP_FILEN) + en(get_datetimeoriginal(index - 1, DATE_SEP), DTO) + en(en(CNT_PREFIX, DTO) + zpad(String(counter + CNT_START), CNT_DIGIT_NB), GROUP_CNT);}
  23.     else if ( index != (app.itemcount - 1) &&  get_datetimeoriginal(index + 1, DATE_SEP) != null && app.getItem(index + 1).name == item.name ) {
  24.       final_filename = en(item.newBasename + DATE_PREFIX, KEEP_FILEN) + en(get_datetimeoriginal(index + 1, DATE_SEP), DTO) + en(en(CNT_PREFIX, DTO) + zpad(String(counter + CNT_START), CNT_DIGIT_NB), GROUP_CNT);}
  25.     // can't find a datetimeoriginal around, keep the original filename
  26.     else { final_filename = item.newBasename + en(CNT_PREFIX + zpad(String(counter + CNT_START), CNT_DIGIT_NB), GROUP_CNT); }
  27.   }
  28.   // file doesn't have a datetimeoriginal, and the LOOKAROUND is off, so, keep the original filename
  29.   else { final_filename = item.newBasename + en(CNT_PREFIX + zpad(String(counter + CNT_START), CNT_DIGIT_NB), GROUP_CNT); }
  30.  
  31.   // Make sure we aren't at the last file
  32.   if ( index != (app.itemcount - 1) ){
  33.     // Increase the counter if the next file has a different name
  34.     if (NEWNAME_FOR_CNT) {
  35.             if( app.getItem(index + 1).newBasename != item.newBasename ){
  36.                 counter+= CNT_STEP;}
  37.         }
  38.         else {
  39.             if( app.getItem(index + 1).name != item.name ){
  40.                 counter+= CNT_STEP;}
  41.         }
  42.   }
  43.  
  44.   return final_filename;
  45.  
  46. // function to get datetimeoriginal from the file in the list with the given index.
  47. function get_datetimeoriginal(idx, sep) {
  48.     var DTO = "";
  49.     var aDate = [];
  50.     //regular expression to extract date from datetimeoriginal (which is a date and a time)
  51.     var reDateTimeSplit = /(\d{4}):(\d{2}):(\d{2})\s(\d{2}):(\d{2}):(\d{2})/;
  52.     //extract datetimeoriginal from file
  53.     DTO = app.getItem(idx).exifToolValue("DateTimeOriginal");
  54.  
  55.     if ( DTO.length != 0 ) {
  56.         aDate = reDateTimeSplit.exec(DTO);
  57.         return aDate[1] + sep + aDate[2] + sep + aDate[3]
  58.         }
  59.     else{ return null   }
  60. }
  61.  
  62. // Function that add leading zeros to any string
  63. function zpad(str, nb_digits)
  64. {
  65.     if ( str.length < nb_digits )
  66.         { return (Array(nb_digits - str.length + 1).join('0') + str); }
  67.     else
  68.         { return str; }
  69. }
  70.  
  71. // return the string if enabled
  72. function en(str, enabled){
  73.   if (enabled) {return str}
  74.   else { return "" }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement