Copy link to clipboard
Copied
I am wanting to update the script below with some additional functionality.
Currently the script converts UK date formats dd/mm/yyyy into a more readable format.
Example:
01/02/2024 becomes Thursday 1st February 2024
I would like to update the script to also be able to handle date ranges.
With the current script it does the below which is ok except for the repeated month and year.
Example:
01/02/2024 ā 07/02/2024 becomes Thursday 1st February 2024 ā Wednesday 7th February 2024
I would like the result to be Thursday 1st ā Wednesday 7th February 2024
So whenever there is a repeat of month and/or year within a range the first instance it is removed or ignored?
01/02/2024 ā 07/05/2024 becomes Thursday 1st February ā Tuesday 7th May 2024
Not sure if the script can be updated to do this or weather a new script is needed.
Any help appreciated.
#target indesign;
/*
by Oleh Melnyk at 5 April 2017
requested at https://forums.adobe.com/message/9436296#9436296
Edited By :- Manan Joshi
Edited the date format to dd/mm/yyyy or d/m/yyyy
Jan 08 - 2023: Updated to include ordinal indicators st, th, rd
*/
try {
//> START OF doUndoWraper
if (parseFloat(app.version) < 6) // "app.version < 6" if it's running under an earlier version than CS4, as earlier versions don't support "Undo" in scripts
doUndoWrapper();
else
app.doScript(doUndoWrapper, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Change Date Format");
//< END OF doUndoWraper
function doUndoWrapper(){
function toUnique(a, b, c) { //array,placeholder,placeholder
b = a.length;
while (c = --b)
while (c--) a[b] !== a[c] || a.splice(c, 1);
return a // not needed ;)
}
function convertDateFormat(date) {
var t = date.split("/");
date = t[1] + "/" + t[0] + "/" + t[2];
var objDate = new Date(date);
var monthName = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var dayName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var day = objDate.getDate();
var suffix = "th";
if (day % 10 === 1 && day !== 11) {
suffix = "st";
} else if (day % 10 === 2 && day !== 12) {
suffix = "nd";
} else if (day % 10 === 3 && day !== 13) {
suffix = "rd";
}
return dayName[objDate.getDay()] + " " + day + suffix + " " + monthName[objDate.getMonth()] + " " + objDate.getFullYear();
}
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; // clear settings
app.findChangeGrepOptions.includeLockedLayersForFind = false; // search in Locked Layers
app.findChangeGrepOptions.includeLockedStoriesForFind = false; // search in Locked Stories
app.findChangeGrepOptions.includeHiddenLayers = false; // search in HiddenLayers
app.findChangeGrepOptions.includeMasterPages = false; // search in Master Pages
app.findChangeGrepOptions.includeFootnotes = true; // search in Footnotes
app.findGrepPreferences.findWhat = "\\d{1,2}\/\\d{1,2}\/\\d{4}";
var whereToSearch = app.activeDocument; // default - search in entire document
var foundPrep = whereToSearch.findGrep();
if (foundPrep.length === 0) {
alert("No matching date formats found in the document.\ndd/mm/yyyy or d/m/yyyy");
return; // No dates found, so exit the script.
}
var foundElements = [];
for(var x = 0; x < foundPrep.length; x++){
foundElements.push(foundPrep[x].contents);
}
var foundUnique = toUnique(foundElements);
for(var i = 0; i < foundUnique.length; i++){
var option = foundUnique[i];
app.findGrepPreferences.findWhat = option;
app.changeGrepPreferences.changeTo = convertDateFormat(option);
whereToSearch.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; // clear find what and change to field
}
}
} catch (error) {
alert(error.message);
}
Copy link to clipboard
Copied
First, change the scope of monthName by moving the line beginning with
var monthName = [...
right under the line
function doUndoWrapper(){
Next, append these lines to the end of the doUndoWrapper() function:
findDuplicates();
function findDuplicates() {
app.findGrepPreferences.findWhat = "(\\d{4}).*?(\\d{4})";
removeDuplicates(app.activeDocument.findGrep());
for(var i=0; i<monthName.length; i++) {
app.findGrepPreferences.findWhat = monthName[i] + ".+(?=\\x{2013}).+" + monthName[i];
removeDuplicates(app.activeDocument.findGrep());
}
}
function removeDuplicates(input) {
for(var i = 0; i<input.length; i++) {
if(input[i] != "") {
var sel = input[i].select();
var term = input[i].contents.substr(0, input[i].contents.indexOf(" "));
var selCon = app.selection[0].contents;
if(term == selCon.substr(selCon.length-term.length, selCon.length)){
app.selection[0].contents = selCon.substr(term.length+1, selCon.length);
}
}
}
}
Results:
Before
After
Copy link to clipboard
Copied
Hi @GNDGN, it works nicely. When I tested with '06/12/2023 until 15/12/2023', I get 'Wednesday 6th December until Friday 15th December 2023'. Would you be able to modify it to accommodate instances like that? Thanks.
Copy link to clipboard
Copied
Just add these lines inside the for loop of the findDuplicates() function:
app.findGrepPreferences.findWhat = monthName[i] + ".+(?=until).+" + monthName[i];
removeDuplicates(app.activeDocument.findGrep());
This covers the additional cases with the word until.
Copy link to clipboard
Copied
@Thunder-Lightning Are you able to post the full updated script? I am struggling to get it to work.
Copy link to clipboard
Copied
#target indesign;
/*
by Oleh Melnyk at 5 April 2017
requested at https://forums.adobe.com/message/9436296#9436296
Edited By :- Manan Joshi
Edited the date format to dd/mm/yyyy or d/m/yyyy
Jan 08 - 2023: Updated to include ordinal indicators st, th, rd
*/
try {
//> START OF doUndoWraper
if (parseFloat(app.version) < 6) // "app.version < 6" if it's running under an earlier version than CS4, as earlier versions don't support "Undo" in scripts
doUndoWrapper();
else
app.doScript(doUndoWrapper, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Change Date Format");
//< END OF doUndoWraper
function doUndoWrapper(){
var monthName = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function toUnique(a, b, c) { //array,placeholder,placeholder
b = a.length;
while (c = --b)
while (c--) a[b] !== a[c] || a.splice(c, 1);
return a // not needed ;)
}
function convertDateFormat(date) {
var t = date.split("/");
date = t[1] + "/" + t[0] + "/" + t[2];
var objDate = new Date(date);
var dayName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var day = objDate.getDate();
var suffix = "th";
if (day % 10 === 1 && day !== 11) {
suffix = "st";
} else if (day % 10 === 2 && day !== 12) {
suffix = "nd";
} else if (day % 10 === 3 && day !== 13) {
suffix = "rd";
}
return dayName[objDate.getDay()] + " " + day + suffix + " " + monthName[objDate.getMonth()] + " " + objDate.getFullYear();
}
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; // clear settings
app.findChangeGrepOptions.includeLockedLayersForFind = false; // search in Locked Layers
app.findChangeGrepOptions.includeLockedStoriesForFind = false; // search in Locked Stories
app.findChangeGrepOptions.includeHiddenLayers = false; // search in HiddenLayers
app.findChangeGrepOptions.includeMasterPages = false; // search in Master Pages
app.findChangeGrepOptions.includeFootnotes = true; // search in Footnotes
app.findGrepPreferences.findWhat = "\\d{1,2}\/\\d{1,2}\/\\d{4}";
var whereToSearch = app.activeDocument; // default - search in entire document
var foundPrep = whereToSearch.findGrep();
if (foundPrep.length === 0) {
alert("No matching date formats found in the document.\ndd/mm/yyyy or d/m/yyyy");
return; // No dates found, so exit the script.
}
var foundElements = [];
for(var x = 0; x < foundPrep.length; x++){
foundElements.push(foundPrep[x].contents);
}
var foundUnique = toUnique(foundElements);
for(var i = 0; i < foundUnique.length; i++){
var option = foundUnique[i];
app.findGrepPreferences.findWhat = option;
app.changeGrepPreferences.changeTo = convertDateFormat(option);
whereToSearch.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; // clear find what and change to field
}
findDuplicates();
function findDuplicates() {
app.findGrepPreferences.findWhat = "(\\d{4}).*?(\\d{4})";
removeDuplicates(app.activeDocument.findGrep());
for(var i=0; i<monthName.length; i++) {
app.findGrepPreferences.findWhat = monthName[i] + ".+(?=\\x{2013}).+" + monthName[i];
removeDuplicates(app.activeDocument.findGrep());
app.findGrepPreferences.findWhat = monthName[i] + ".+(?=until).+" + monthName[i];
removeDuplicates(app.activeDocument.findGrep());
}
}
function removeDuplicates(input) {
for(var i = 0; i<input.length; i++) {
if(input[i] != "") {
var sel = input[i].select();
var term = input[i].contents.substr(0, input[i].contents.indexOf(" "));
var selCon = app.selection[0].contents;
if(term == selCon.substr(selCon.length-term.length, selCon.length)){
app.selection[0].contents = selCon.substr(term.length+1, selCon.length);
}
}
}
}
}
} catch (error) {
alert(error.message);
}
Copy link to clipboard
Copied
Thank you @GNDGN .
Copy link to clipboard
Copied
Thanks @GNDGN @Thunder-Lightning
Working for me now although I get different results depending on the sepearter (-) vs (ā) ?
works as expected only with ā
Copy link to clipboard
Copied
Sorry also not working for date ranges across years like below.
07/10/2023 ā 14/10/2024 results in Saturday 7th 2023 ā Monday 14th October 2024
Should be Saturday 7th October 2023 ā Monday 14th October 2024
Copy link to clipboard
Copied
**EDITED**
sorry it's not quite working yet.
Copy link to clipboard
Copied
I think I have worked it out now.
#target indesign;
/*
by Oleh Melnyk at 5 April 2017
requested at https://forums.adobe.com/message/9436296#9436296
Edited By :- Manan Joshi
Edited the date format to dd/mm/yyyy or d/m/yyyy
Jan 08 - 2023: Updated to include ordinal indicators st, th, rd
*/
try {
//> START OF doUndoWraper
if (parseFloat(app.version) < 6) // "app.version < 6" if it's running under an earlier version than CS4, as earlier versions don't support "Undo" in scripts
doUndoWrapper();
else
app.doScript(doUndoWrapper, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Change Date Format");
//< END OF doUndoWraper
function doUndoWrapper(){
var monthName = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function toUnique(a, b, c) { //array,placeholder,placeholder
b = a.length;
while (c = --b)
while (c--) a[b] !== a[c] || a.splice(c, 1);
return a // not needed ;)
}
function convertDateFormat(date) {
var t = date.split("/");
date = t[1] + "/" + t[0] + "/" + t[2];
var objDate = new Date(date);
var dayName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var day = objDate.getDate();
var suffix = "th";
if (day % 10 === 1 && day !== 11) {
suffix = "st";
} else if (day % 10 === 2 && day !== 12) {
suffix = "nd";
} else if (day % 10 === 3 && day !== 13) {
suffix = "rd";
}
return dayName[objDate.getDay()] + " " + day + suffix + " " + monthName[objDate.getMonth()] + " " + objDate.getFullYear();
}
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; // clear settings
app.findChangeGrepOptions.includeLockedLayersForFind = false; // search in Locked Layers
app.findChangeGrepOptions.includeLockedStoriesForFind = false; // search in Locked Stories
app.findChangeGrepOptions.includeHiddenLayers = false; // search in HiddenLayers
app.findChangeGrepOptions.includeMasterPages = false; // search in Master Pages
app.findChangeGrepOptions.includeFootnotes = true; // search in Footnotes
app.findGrepPreferences.findWhat = "\\d{1,2}\/\\d{1,2}\/\\d{4}";
var whereToSearch = app.activeDocument; // default - search in entire document
var foundPrep = whereToSearch.findGrep();
if (foundPrep.length === 0) {
alert("No matching date formats found in the document.\ndd/mm/yyyy or d/m/yyyy");
return; // No dates found, so exit the script.
}
var foundElements = [];
for(var x = 0; x < foundPrep.length; x++){
foundElements.push(foundPrep[x].contents);
}
var foundUnique = toUnique(foundElements);
for(var i = 0; i < foundUnique.length; i++){
var option = foundUnique[i];
app.findGrepPreferences.findWhat = option;
app.changeGrepPreferences.changeTo = convertDateFormat(option);
whereToSearch.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; // clear find what and change to field
}
findDuplicates();
function findDuplicates() {
for(var i=0; i<monthName.length; i++) {
app.findGrepPreferences.findWhat = monthName[i] + " (\\d{4}) (?=\\x{2013}\|\\x{2010}).+?" + monthName[i] + " \\1";
removeDuplicatesMonth(app.activeDocument.findGrep());
app.findGrepPreferences.findWhat = monthName[i] + " (\\d{4}) (?=until).+?" + monthName[i] + " \\1";
removeDuplicatesMonth(app.activeDocument.findGrep());
}
app.findGrepPreferences.findWhat = "(\\d{4}).*?(\\1)";
removeDuplicatesYear(app.activeDocument.findGrep());
}
function removeDuplicatesYear(input) {
for(var i = 0; i<input.length; i++) {
if(input[i] != "") {
var sel = input[i].select();
var term = input[i].contents.substr(0, input[i].contents.indexOf(" "));
var selCon = app.selection[0].contents;
if(term == selCon.substr(selCon.length-term.length, selCon.length)){
app.selection[0].contents = selCon.substr(term.length+1, selCon.length);
}
}
}
}
function removeDuplicatesMonth(input) {
for(var i = 0; i<input.length; i++) {
if(input[i] != "") {
var sel = input[i].select();
var term = input[i].contents.substr(0, input[i].contents.indexOf(" "));
var selCon = app.selection[0].contents;
if(term == selCon.substr(selCon.length-term.length-5, term.length)){
app.selection[0].contents = selCon.substr(term.length+1, selCon.length);
}
}
}
}
}
} catch (error) {
alert(error.message);
}
It covers n-dash and hypen and only remove duplicated if the years are the same.