How could I adapt the following code so that it works from a toolbar button, and that it does not take into account any file, nor any renaming, simply that it asks for the two dates, performs the calculation and displays it on the screen?!?
function OnGetNewName(getNewNameData) {
var birthdate = new Date(2019, 2 - 1, 13);
var item = getNewNameData.item;
var metadata = item.metadata;
if (metadata && metadata.image && metadata.image.datetaken) {
var datetaken = new Date(metadata.image.datetaken);
var diff = datetaken - birthdate;
var years = datetaken.getFullYear() - birthdate.getFullYear();
var months = datetaken.getMonth() - birthdate.getMonth();
var days = datetaken.getDay() - birthdate.getDay();
var strYears = (years != 0) ? (years + "a ") : "";
var strMonths = (months != 0) ? (months + "m ") : "";
var strDays = (days != 0) ? (days + "d ") : "";
var strTotal = (strYears + strMonths + strDays).slice(0,-1);
var age = "(" + (strTotal || "0 tiempo") + ")";
var result = item.name_stem + " " + age + item.ext;
return result;
} else {
return item.name;
}
}
´´´
@Leo That is more or less what I am needing, to be able to create an Input to be able to induce the dates each time the button is pressed, and see the result on the screen.
Function OnClick(ByRef ClickData) {
var date1 = new Date(1978, 01 -1 , 27);
var date2 = new Date(2023, 08 - 1, 10);
var diff = date2 - date1;
var years = date2.getFullYear() - date1.getFullYear();
var months = date2.Month() - date1.getMonth();
var days = date2.getDay() - date1.getDay();
var age = "(" + years + "a " + months + "m " + years + "d)";
return "The age is" + age;
}
The best way would be for you to create your own Script Dialog.
There is also an easier way using the Dialog object but this is more clunky since there will be 3 windows opening in series (start date input, end date input, and a result message box):
To ask the user to enter the dates you can use the Dialog.GetString function.
To display the result in a messagebox I think there is an easier way I'm missing, but you can use the Dialog.Show function.
Here is an example button code:
function OnClick(clickData)
{
var dlg = clickData.func.Dlg;
// Ask start date
var dateStart = dlg.getString("Start date");
if (!dateStart) {
return; // User canceled or didn't enter anything, so exit.
}
// Ask end date
var DOpusFactory = DOpus.Create();
var dateEnd = dlg.getString("End date", DOpusFactory.Date()); // Extra example where the default today's date is pre-filled (can be formatted, see Opus' scripting Date docs).
if (!dateStart) {
return; // User canceled or didn't enter anything, so exit.
}
// Display result
dlg.title = "Age result";
dlg.message = "Do some magic and calculate the age between:\n• Start date: " + dateStart + "\n• End date: " + dateEnd;
dlg.icon = "info";
dlg.buttons = "OK";
dlg.Show();
}
You'll have to figure out how to process the user input (convert whatever the user enters into a usable date object for calculation purposes).
Hello @bytespiller , I have adapted your code, but I am getting an error on line 21 "Object does not accept this property or method". I tried to create new Date with the fechaInicial and fechaFinal variables but the error continues. What will be the error?
function OnClick(clickData)
{
var dlg = clickData.func.Dlg;
// Preguntar fecha inicial
var fechaInicio = dlg.getString("Fecha inicio (dd-MM-yyyy):");
if (!fechaInicio) {
return; // Si el usuario cancela o no se introduce nada, salir
}
// Preguntar edad final
var DOpusFactory = DOpus.Create();
var fechaFinal = dlg.getString("Fecha final:", DOpusFactory.Date()); // Se sugiere la fecha actual (el formato de esta puede ser modificado)
if (!fechaFinal) {
return; // Si el usuario cancela o no se introduce nada, salir
}
var diff = fechaFinal - fechaInicio
var totalDias = Math.floor(diff / 8.64e+7);
var anos = fechaFinal.getFullYear() - fechaInicio.getFullYear();
var meses = fechaFinal.Month() - fechaInicio.getMonth();
var dias = fechaFinal.getDay() - fechaInicio.getDay();
var edad = "(" + anos + "años, " + meses + "meses y " + dias + "dias)";
// Mostrar el resultado
dlg.title = "Cálculo de edad";
dlg.message = "La edad entre:\n Fecha inicial: " + fechaInicial + "\n Fecha final: " + fechaFinal + " es " + edad;
dlg.icon = "info";
dlg.buttons = "Aceptar";
dlg.Show();
}
And here is the example of converting ISO string to date using Opus' Date objects which support parsing that:
function OnClick(clickData)
{
var dlg = clickData.func.Dlg;
var DOpusFactory = DOpus.Create();
// Preguntar fecha inicial
var fechaInicio = dlg.getString("Fecha inicio (yyyy-MM-dd):");
if (!fechaInicio) {
return; // Si el usuario cancela o no se introduce nada, salir
}
fechaInicio = DOpusFactory.Date(fechaInicio);
// Preguntar edad final
var fechaFinal = dlg.getString("Fecha final:", DOpusFactory.Date().Format("D#yyyy-MM-dd")); // Se sugiere la fecha actual (el formato de esta puede ser modificado)
if (!fechaFinal) {
return; // Si el usuario cancela o no se introduce nada, salir
}
fechaFinal = DOpusFactory.Date(fechaFinal);
var diff = fechaFinal - fechaInicio
//var totalDias = Math.floor(diff / 8.64e+7);
var anos = fechaFinal.year - fechaInicio.year;
var meses = fechaFinal.month - fechaInicio.month;
var dias = fechaFinal.day - fechaInicio.day;
var edad = "(" + anos + " años, " + meses + " meses y " + dias + " dias)";
// Mostrar el resultado
dlg.title = "Cálculo de edad";
dlg.message = "La edad entre:\nFecha inicial: " + fechaInicio + "\nFecha final: " + fechaFinal + " es " + edad;
dlg.icon = "info";
dlg.buttons = "Aceptar";
dlg.Show();
}