Final Boss Mega Script
Hi I am trying to build a script that would be able to standardize all my files according to the best possible date and time the file was built eg the date the photo was taken, the date the video was taken, the date the Word document was saved, the date the Zip file was compressed, the date the wav file was recorded, the date the MP3 file was encoded, etc...
The problem is the date is not always in the same field depending on the media, the extension, etc...
I am struggling with jpg file, in the script I build I have placed EXIF DateTimeOriginal as my priority field but it fails to catch it...
I have checked and made sur the EXIFTOOL is the last version.
Any ideas what I could be missing? (Disclaimer I have used Gemini to build this code, I and I am sharing the prompt below the code)
var name = item.name;
// 1. PREVENT DOUBLE RENAMING
if (name.match(/^\d{8} \d{6} /)) {
return name;
}
var ext = item.extension.toLowerCase();
var tagsToCheck = [];
var finalDate = "";
// 2. EXTRACTION ENGINE
function pullDate(tagStr) {
var val = app.parseTags(tagStr);
if (!val || val.indexOf("<") > -1 || val.trim() === "") return "";
var digits = val.replace(/[^0-9]/g, "");
if (digits.length >= 8) {
return (digits + "000000").substring(0, 14);
}
return "";
}
// 3. APPLY RULES
switch(ext) {
case "avi":
tagsToCheck = ["<ExifTool:DateTimeOriginal>", "<ExifTool:CreateDate>", "<Video Date:yyyymmddhhnnss>", "<ExifTool:FileModifyDate>"];
break;
case "mov":
tagsToCheck = ["<ExifTool:ContentCreateDate>", "<ExifTool:CreateDate>", "<ExifTool:MediaCreateDate>", "<ExifTool:MediaModifyDate>"];
break;
case "m4v":
tagsToCheck = ["<ExifTool:MediaCreateDate>", "<ExifTool:CreateDate>", "<ExifTool:MediaModifyDate>", "<ExifTool:ModifyDate>"];
break;
case "mpg": case "mpeg":
tagsToCheck = ["<ExifTool:FileModifyDate>"];
break;
case "wmv":
tagsToCheck = ["<ExifTool:CreationDate>", "<ExifTool:FileModifyDate>"];
break;
case "png":
tagsToCheck = ["<ExifTool:ModifyDate>", "<ExifTool:FileModifyDate>"];
break;
case "jpg": case "jpeg": case "heic":
tagsToCheck = [
"<ExifTool:DateTimeOriginal>",
"<ExifTool:CreateDate>",
"<ExifTool:CreationDate>",
"<ExifTool:ModifyDate>",
"<ExifTool:FileModifyDate>"];
break;
case "bmp":
tagsToCheck = ["<ExifTool:FileModifyDate>"];
break;
case "tif":
tagsToCheck = ["<Date Taken:yyyymmddhhnnss>", "<ExifTool:DateTimeOriginal>", "<ExifTool:CreateDate>", "<ExifTool:ModifyDate>"];
break;
case "psd":
tagsToCheck = ["<ExifTool:ModifyDate>", "<ExifTool:MetadataDate>"];
break;
case "mp3": case "wav":
tagsToCheck = ["<ExifTool:FileModifyDate>"];
break;
case "pdf":
tagsToCheck = ["<ExifTool:MetadataDate>", "<ExifTool:ModifyDate>", "<ExifTool:FileModifyDate>"];
break;
case "xls": case "xlsx": case "vsd": case "txt": case "ppt": case "pptx": case "doc": case "docx":
tagsToCheck = ["<ExifTool:FileModifyDate>", "<ExifTool:ModifyDate>"];
break;
case "rar":
tagsToCheck = ["<ExifTool:ModifyDate>", "<ExifTool:FileModifyDate>"];
break;
case "zip":
tagsToCheck = ["<ExifTool:ZipModifyDate>", "<ExifTool:FileModifyDate>"];
break;
case "ps":
tagsToCheck = ["<ExifTool:FileModifyDate>"];
break;
}
// 4. EXECUTE EXIF SEARCH
for (var i = 0; i < tagsToCheck.length; i++) {
finalDate = pullDate(tagsToCheck[i]);
if (finalDate !== "") break;
}
// 5. NEW FALLBACK: EXTRACT DATE FROM FOLDER NAME
// If EXIF is dead and we are about to use the 2010 recovery date, check the folder path first!
// Looks for patterns like "2005_04_12" or "2005 04 12" or "2005-04-12"
if (finalDate === "") {
var folderPath = item.directory;
var pathMatch = folderPath.match(/(19|20\d{2})[-_ ](0[1-9]|1[0-2])[-_ ](0[1-9]|[12]\d|3[01])/);
if (pathMatch) {
var y = pathMatch[1];
var m = pathMatch[2];
var d = pathMatch[3];
// We set the time to 000000 since the folder only gives us the day
finalDate = y + m + d + "000000";
}
}
// 6. FINAL FALLBACK: SYSTEM MODIFIED
if (finalDate === "") {
if (item.modifiedDate) {
var mod = app.formatDate(item.modifiedDate, "yyyymmddhhnnss");
if (mod && mod.length === 14) finalDate = mod;
} else {
finalDate = pullDate("<Date Mod:yyyymmddhhnnss>");
}
}
// 7. FINAL OUTPUT
if (finalDate !== "" && finalDate.length === 14) {
var datePart = finalDate.substring(0, 8);
var timePart = finalDate.substring(8, 14);
return datePart + " " + timePart + " " + name;
}
return name;
Prompt:
Video files:
If the file extension is avi
look at EXITOOL
If DateTimeOriginal is present take it, if not
If FileModifyDate is present take it, if not
If the file extension is MOV
look at EXITOOL
If ContentCreateDate is present take it, if not
If CreateDate is present take it, if not
If MediaCreateDate is present take it, if not
If MediaModifyDate is present take it, if not
If the file extension is m4v
look at EXITOOL
If MediaCreateDate is present take it, if not
If CreateDate is present take it, if not
If MediaModifyDate is present take it, if not
If ModifyDate is present take it, if not
If the file extension is mpg and mpeg
Look at EXIFTOOL
If FileModifyDate is present take it, if not
If the file extension is wmv
Look at EXIFTOOL
If CreationDate is present take it (drop the Z at the end), if not
If FileModifyDate is present take it, if not
Photos files:
If the file extension is png
Look at EXIFTOOL
If ModifyDate is present take it, if not
If FileModifyDate is present take it, if not
If the file extension is jpg or jpeg
Look at EXIFTOOL
If DateTimeOriginal is present take it, if not
If CreateDate is present take it, if not
If CreationDate is present take it, if not
If FileModifyDate is present take it, if not
If ModifyDate is present take it, if not
If the file extension is heic
Look at EXIFTOOL
If DateTimeOriginal is present take it, if not
If CreateDate is present take it, if not
If CreationDate is present take it, if not
If ModifyDate is present take it, if not
If FileModifyDate is present take it, if not
If the file extension is bmp
Look at EXIFTOOL
If FileModifyDate is present take it, if not
If the file extension is tif
Look at EXIFTOOL
If DateTaken is present take it, if not
If CreateDate is present take it, if not
If ModifyDate is present take it, if not
If the file extension is psd
Look at EXIFTOOL
If ModifyDate is present take it if not
If MetadataDate is present take it if not
If ModifyDate is present take it if not
Audio files:
If the file extension is mp3
Look at EXIFTOOL
If FileModifyDate is present take it, if not
If the file extension is wav
Look at EXIFTOOL
If FileModifyDate is present take it, if not
Documents:
If the file extension is pdf
Look at EXIFTOOL
If MetadataDate is present take it, if not
If ModifyDate is present take it, if not
If FileModifyDate is present take it, if not
If the file extension is xls, xlsx, vsd, txt, ppt, pptx, doc, docx
Look at EXIFTOOL
If FileModifyDate is present take it, if not
If ModifyDate is present take it, if not
If the file extension is rar
Look at EXIFTOOL
If ModifyDate is present take it, if not
Take FileModifyDate
If the file extension zip
Look at EXIFTOOL
If ZipModifyDate is present take it, if not
Take FileModifyDate
If the file extension is ps
Look at EXIFTOOL
If FileModifyDate is present take it, if not
For any of the files above if you cannot get a proper date and time look at File and take the DateModified and TimeModified
For other files with unknow extensions or does not have extension look at File and take the DateModified and TimeModified
For all dates and files you must return YYYYMMDD HHMMSS filename.ext
where YYYYMMDD is the year, month, day
follow by a space where HHMMSS is the time
follow by a space and the orginal filename
For example "IMG_4399 Ski top corrupted.MOV" would become "20110306 125300 IMG_4399 Ski top corrupted.MOV"
If despite all your best efforts you can't get a legit DATE AND TIME, then ignore the file an do not do anything, the file should remain unchanged
The problem is the date is not always in the same field depending on the media, the extension, etc...
I am struggling with jpg file, in the script I build I have placed EXIF DateTimeOriginal as my priority field but it fails to catch it...
I have checked and made sur the EXIFTOOL is the last version.
Any ideas what I could be missing? (Disclaimer I have used Gemini to build this code, I and I am sharing the prompt below the code)
var name = item.name;
// 1. PREVENT DOUBLE RENAMING
if (name.match(/^\d{8} \d{6} /)) {
return name;
}
var ext = item.extension.toLowerCase();
var tagsToCheck = [];
var finalDate = "";
// 2. EXTRACTION ENGINE
function pullDate(tagStr) {
var val = app.parseTags(tagStr);
if (!val || val.indexOf("<") > -1 || val.trim() === "") return "";
var digits = val.replace(/[^0-9]/g, "");
if (digits.length >= 8) {
return (digits + "000000").substring(0, 14);
}
return "";
}
// 3. APPLY RULES
switch(ext) {
case "avi":
tagsToCheck = ["<ExifTool:DateTimeOriginal>", "<ExifTool:CreateDate>", "<Video Date:yyyymmddhhnnss>", "<ExifTool:FileModifyDate>"];
break;
case "mov":
tagsToCheck = ["<ExifTool:ContentCreateDate>", "<ExifTool:CreateDate>", "<ExifTool:MediaCreateDate>", "<ExifTool:MediaModifyDate>"];
break;
case "m4v":
tagsToCheck = ["<ExifTool:MediaCreateDate>", "<ExifTool:CreateDate>", "<ExifTool:MediaModifyDate>", "<ExifTool:ModifyDate>"];
break;
case "mpg": case "mpeg":
tagsToCheck = ["<ExifTool:FileModifyDate>"];
break;
case "wmv":
tagsToCheck = ["<ExifTool:CreationDate>", "<ExifTool:FileModifyDate>"];
break;
case "png":
tagsToCheck = ["<ExifTool:ModifyDate>", "<ExifTool:FileModifyDate>"];
break;
case "jpg": case "jpeg": case "heic":
tagsToCheck = [
"<ExifTool:DateTimeOriginal>",
"<ExifTool:CreateDate>",
"<ExifTool:CreationDate>",
"<ExifTool:ModifyDate>",
"<ExifTool:FileModifyDate>"];
break;
case "bmp":
tagsToCheck = ["<ExifTool:FileModifyDate>"];
break;
case "tif":
tagsToCheck = ["<Date Taken:yyyymmddhhnnss>", "<ExifTool:DateTimeOriginal>", "<ExifTool:CreateDate>", "<ExifTool:ModifyDate>"];
break;
case "psd":
tagsToCheck = ["<ExifTool:ModifyDate>", "<ExifTool:MetadataDate>"];
break;
case "mp3": case "wav":
tagsToCheck = ["<ExifTool:FileModifyDate>"];
break;
case "pdf":
tagsToCheck = ["<ExifTool:MetadataDate>", "<ExifTool:ModifyDate>", "<ExifTool:FileModifyDate>"];
break;
case "xls": case "xlsx": case "vsd": case "txt": case "ppt": case "pptx": case "doc": case "docx":
tagsToCheck = ["<ExifTool:FileModifyDate>", "<ExifTool:ModifyDate>"];
break;
case "rar":
tagsToCheck = ["<ExifTool:ModifyDate>", "<ExifTool:FileModifyDate>"];
break;
case "zip":
tagsToCheck = ["<ExifTool:ZipModifyDate>", "<ExifTool:FileModifyDate>"];
break;
case "ps":
tagsToCheck = ["<ExifTool:FileModifyDate>"];
break;
}
// 4. EXECUTE EXIF SEARCH
for (var i = 0; i < tagsToCheck.length; i++) {
finalDate = pullDate(tagsToCheck[i]);
if (finalDate !== "") break;
}
// 5. NEW FALLBACK: EXTRACT DATE FROM FOLDER NAME
// If EXIF is dead and we are about to use the 2010 recovery date, check the folder path first!
// Looks for patterns like "2005_04_12" or "2005 04 12" or "2005-04-12"
if (finalDate === "") {
var folderPath = item.directory;
var pathMatch = folderPath.match(/(19|20\d{2})[-_ ](0[1-9]|1[0-2])[-_ ](0[1-9]|[12]\d|3[01])/);
if (pathMatch) {
var y = pathMatch[1];
var m = pathMatch[2];
var d = pathMatch[3];
// We set the time to 000000 since the folder only gives us the day
finalDate = y + m + d + "000000";
}
}
// 6. FINAL FALLBACK: SYSTEM MODIFIED
if (finalDate === "") {
if (item.modifiedDate) {
var mod = app.formatDate(item.modifiedDate, "yyyymmddhhnnss");
if (mod && mod.length === 14) finalDate = mod;
} else {
finalDate = pullDate("<Date Mod:yyyymmddhhnnss>");
}
}
// 7. FINAL OUTPUT
if (finalDate !== "" && finalDate.length === 14) {
var datePart = finalDate.substring(0, 8);
var timePart = finalDate.substring(8, 14);
return datePart + " " + timePart + " " + name;
}
return name;
Prompt:
Video files:
If the file extension is avi
look at EXITOOL
If DateTimeOriginal is present take it, if not
If FileModifyDate is present take it, if not
If the file extension is MOV
look at EXITOOL
If ContentCreateDate is present take it, if not
If CreateDate is present take it, if not
If MediaCreateDate is present take it, if not
If MediaModifyDate is present take it, if not
If the file extension is m4v
look at EXITOOL
If MediaCreateDate is present take it, if not
If CreateDate is present take it, if not
If MediaModifyDate is present take it, if not
If ModifyDate is present take it, if not
If the file extension is mpg and mpeg
Look at EXIFTOOL
If FileModifyDate is present take it, if not
If the file extension is wmv
Look at EXIFTOOL
If CreationDate is present take it (drop the Z at the end), if not
If FileModifyDate is present take it, if not
Photos files:
If the file extension is png
Look at EXIFTOOL
If ModifyDate is present take it, if not
If FileModifyDate is present take it, if not
If the file extension is jpg or jpeg
Look at EXIFTOOL
If DateTimeOriginal is present take it, if not
If CreateDate is present take it, if not
If CreationDate is present take it, if not
If FileModifyDate is present take it, if not
If ModifyDate is present take it, if not
If the file extension is heic
Look at EXIFTOOL
If DateTimeOriginal is present take it, if not
If CreateDate is present take it, if not
If CreationDate is present take it, if not
If ModifyDate is present take it, if not
If FileModifyDate is present take it, if not
If the file extension is bmp
Look at EXIFTOOL
If FileModifyDate is present take it, if not
If the file extension is tif
Look at EXIFTOOL
If DateTaken is present take it, if not
If CreateDate is present take it, if not
If ModifyDate is present take it, if not
If the file extension is psd
Look at EXIFTOOL
If ModifyDate is present take it if not
If MetadataDate is present take it if not
If ModifyDate is present take it if not
Audio files:
If the file extension is mp3
Look at EXIFTOOL
If FileModifyDate is present take it, if not
If the file extension is wav
Look at EXIFTOOL
If FileModifyDate is present take it, if not
Documents:
If the file extension is pdf
Look at EXIFTOOL
If MetadataDate is present take it, if not
If ModifyDate is present take it, if not
If FileModifyDate is present take it, if not
If the file extension is xls, xlsx, vsd, txt, ppt, pptx, doc, docx
Look at EXIFTOOL
If FileModifyDate is present take it, if not
If ModifyDate is present take it, if not
If the file extension is rar
Look at EXIFTOOL
If ModifyDate is present take it, if not
Take FileModifyDate
If the file extension zip
Look at EXIFTOOL
If ZipModifyDate is present take it, if not
Take FileModifyDate
If the file extension is ps
Look at EXIFTOOL
If FileModifyDate is present take it, if not
For any of the files above if you cannot get a proper date and time look at File and take the DateModified and TimeModified
For other files with unknow extensions or does not have extension look at File and take the DateModified and TimeModified
For all dates and files you must return YYYYMMDD HHMMSS filename.ext
where YYYYMMDD is the year, month, day
follow by a space where HHMMSS is the time
follow by a space and the orginal filename
For example "IMG_4399 Ski top corrupted.MOV" would become "20110306 125300 IMG_4399 Ski top corrupted.MOV"
If despite all your best efforts you can't get a legit DATE AND TIME, then ignore the file an do not do anything, the file should remain unchanged