Copy link to clipboard
Copied
I think this will be a difficult question.
I have a document that must have a date as 20230517 in one location and as 5 May 2023 in another location.
This is a publication date, but not necessarily today's date, nor the creation date.
We use user variables - for example "DateYearFirst" and "DateD-M-Y" for the two dates and update them manually.
I know if VBA, there are format codes that can change the date format.
Does anyone know of a way to set up either Extendscript or the template so that when the YYYYMMDD variable is updated, the d Mmm YYYY date variable is also updated?
Something like an OnChange event for the variable.
7 Correct answers
I poked around at this and couldn't find an event that is triggered when a variable value is changed. So I would probably do it this way with a script:
Display a simple "Set Date" dialog box when either a book or document is active. You would enter the date in the YYYYMMDD format. When you click OK, the script would update (or create) both variables based on the value entered.
The script would have to parse the YYYYMMDD date in order to get the correct value for the DateD-M-Y variable, but E
...Hi Marshall, I don't think there is a direct notification. You might be able to trap it with Constants.FA_Note_PreFunction... you would have to experiment to see if iparm gives you something reliable. But since you are already writing code, why not just create your own variable editor? You could create a dialog box with 3 simple fields for the day, month, and year, then update both variables programmatically with the input. That seems the optimal approach. This would give you the added benefit o
...#target framemaker
var doc, variable;
doc = app.ActiveDoc;
variable = doc.GetNamedVarFmt ("Current Date (Long)");
variable.Fmt = "<$year>";
I would probably just code a simple parser and lookup.
20230522
year = 2023
month = 05
day = 22
Lookup for month:
...
04 = Apr
05 = May
06 = Jun
...
etc.
That might be less complicated than messing with the Javascript Date object.
The datepicker won't work in ExtendScript because it uses a widget that is meant to be displayed in a browser.
I recommend downloading this PDF:
https://creativepro.com/files/kahrel/indesign/scriptui.html
There used to be a dedicated ScriptUI Forum on Adobe Forums, but I am not sure it is still active. You might want to check and see if it is still there.
I figured it out, but the code could I'm sure be cleaner and more simplified, but it works.
I started with this code:
function addDays(myDate,days) {
return new Date(myDate.getTime() + days*24*60*60*1000);
}
var myDate = new Date('2013-02-11');
var newDate = addDays(myDate,5);
From https://stackoverflow.com/questions/14842526/add-days-to-date-format-yyyy-mm-dd Note that although the thread specifically asks for format YYYYMMDD, and the example shows YYYY-MM-DD, it doesn't actually work, it returns
...Copy link to clipboard
Copied
I poked around at this and couldn't find an event that is triggered when a variable value is changed. So I would probably do it this way with a script:
Display a simple "Set Date" dialog box when either a book or document is active. You would enter the date in the YYYYMMDD format. When you click OK, the script would update (or create) both variables based on the value entered.
The script would have to parse the YYYYMMDD date in order to get the correct value for the DateD-M-Y variable, but ExtendScript (Javascript) has date functions that make this pretty easy.
Copy link to clipboard
Copied
The script would have to parse the YYYYMMDD date in order to get the correct value for the DateD-M-Y variable, but ExtendScript (Javascript) has date functions that make this pretty easy.
Would you have
The script will be fairly complicated, but ...
There are actually three dates - the issue date in YYYYMMDD format, the issue date in d MMM YYYY format, and what I'll call an Expiration date of +1 or +2 years minus 1 day from the issue date (and NOT accounting for leap years). There are also other variables to write, which are simple enough to change.
I'm thinking I want the pop-up form to display the current variables - which would be 20XXMMDD for the issue and expiration dates. The user can change the Issue date and there should be a pop-up calendar for this. There will be a calculate function for the Expiration Date, and the DD MMM YYYY variable will be populated automatically without being displayed.
For a datepicker, I found: https://www.npmjs.com/package/js-datepicker - haven't tested it yet, but it looks like what I am wanting.
For date validation, I found https://www.scaler.com/topics/date-validation-in-javascript/ - but it lists three methods and I don't know which one is preferable. DateParse seems the simplest.
For the Expiration Date Calculation - https://www.tutorialrepublic.com/faq/how-to-add-days-to-current-date-in-javascript.php - SetDate + 365 or + 730 seems to give me what I am looking for.
I think I'm on the correct track, but I was wondering if anyone knew any better options.
Copy link to clipboard
Copied
I couldn't find a converter for YYYYMMDD to DD MMM YYYY - but it seems easy enough to parse and recompose.
Copy link to clipboard
Copied
I would probably just code a simple parser and lookup.
20230522
year = 2023
month = 05
day = 22
Lookup for month:
...
04 = Apr
05 = May
06 = Jun
...
etc.
That might be less complicated than messing with the Javascript Date object.
Copy link to clipboard
Copied
Here is a lookup function that you can use. I add the months to an array like this so that it would be easy to change the months' spelling if you needed full month spellings, etc.
// Test it for the fifth month.
var month = "05";
// Strip any leading zeros and cast to a number.
month = Number (month.replace (/^0+/, ""));
alert (getMonth (month));
function getMonth (num) {
var months;
months = [];
months.push ("");
months.push ("Jan");
months.push ("Feb");
months.push ("Mar");
months.push ("Apr");
months.push ("May");
months.push ("Jun");
months.push ("Jul");
months.push ("Aug");
months.push ("Sep");
months.push ("Oct");
months.push ("Nov");
months.push ("Dec");
if ((num > 0) && (num <= 12)) {
return months[num];
}
}
Copy link to clipboard
Copied
Here is a parser with the lookup:
var date, dateObj;
date = "20230522";
dateObj = getDateObject (date);
if (dateObj) {
alert (dateObj.day + " " + getMonth (dateObj.month) + " " + dateObj.year);
}
function getDateObject (date) {
var regex, matches, dateObj;
// Regular expression for parsing YYYYMMDD date.
regex = /^(\d{4})(\d{2})(\d{2})$/;
if (regex.test (date) === true) {
matches = date.match (regex);
dateObj = {};
dateObj.year = matches[1];
dateObj.monthPadded = matches[2];
dateObj.month = Number (dateObj.monthPadded.replace (/^0+/, ""));
dateObj.dayPadded = matches[3];
dateObj.day = Number (dateObj.dayPadded.replace (/^0+/, ""));
return dateObj;
}
}
function getMonth (num) {
var months;
months = [];
months.push ("");
months.push ("Jan");
months.push ("Feb");
months.push ("Mar");
months.push ("Apr");
months.push ("May");
months.push ("Jun");
months.push ("Jul");
months.push ("Aug");
months.push ("Sep");
months.push ("Oct");
months.push ("Nov");
months.push ("Dec");
return months[num];
}
Copy link to clipboard
Copied
For a datepicker, I found: https://www.npmjs.com/package/js-datepicker - haven't tested it yet, but it looks like what I am wanting.
Actually, looking more carefully at this, it says to add code to the <head> and <body> tags. I'm not sure I have these in a typical ESTK pop-up window. Is there a way to make these work with ESTK, or is there a datepicker that does work with ESTK?
Thanks again!
Copy link to clipboard
Copied
The datepicker won't work in ExtendScript because it uses a widget that is meant to be displayed in a browser.
Copy link to clipboard
Copied
Hi Marshall, I don't think there is a direct notification. You might be able to trap it with Constants.FA_Note_PreFunction... you would have to experiment to see if iparm gives you something reliable. But since you are already writing code, why not just create your own variable editor? You could create a dialog box with 3 simple fields for the day, month, and year, then update both variables programmatically with the input. That seems the optimal approach. This would give you the added benefit of making it foolproof... that is, add validation that makes it impossible to configure an incorrect format.
Russ
Copy link to clipboard
Copied
Hi Russ. I tried using the post function and note-back-to-user notifications, but neither showed anything when I updated a user variable value. -Rick
Copy link to clipboard
Copied
OK, thanks. I didn't test it. But I have to tell you, that idea about a custom editor is pure genious 🙂
Russ
Copy link to clipboard
Copied
Great suggestions - it isn't that simple.
For the discussion, I said it is one date at the top and a different format at the bottom. It is that, but it is 11 dates depending on conditional formatting and 5 of them use the dual format.
I think I could do it with a script that displays 11 radio buttons and based on the selection chooses which variables to update. I can probably figure that out - but it will take a while to do - but I'm done similar with two or three buttons before.
Question - was going to be a separate thread, but since you are both here and undoubtedly know - how do I change user or system variable values with a script?
Thanks in advance!!!
Copy link to clipboard
Copied
Specifically - for another project, how would I set the system Current Date (Long) variable to <?year> via Script?
Copy link to clipboard
Copied
#target framemaker
var doc, variable;
doc = app.ActiveDoc;
variable = doc.GetNamedVarFmt ("Current Date (Long)");
variable.Fmt = "<$year>";
Copy link to clipboard
Copied
Works awesomely!!!
Copy link to clipboard
Copied
How do I mark this resolved? Rick and Russ both had the suggestion for a custom script to udpate the variables. Rick had the answer for how to update the variables. I'm probably marking that the solution, but it was more a side answer to the thread. If I can select multiple correct answers I will.
Copy link to clipboard
Copied
Actually, it's the middle part of this that I am stuck on now. I know there is an SnpCreateCheckBoxRadioButtons.jsx in the C:\Program Files (x86)\Adobe\Adobe ExtendScript Toolkit CC\SDK\Samples\javascript folder, and I think I can figure out how to test if the date is valid and parse it to a new format in the script. Could you please point me to how to write a script to display a pop-up window with a text box that would show the current value of the variable and then update that variable when the text box is changed and the user clicks OK.
I'm pretty sure it's simple, but I didn't see any examples to follow ...
Copy link to clipboard
Copied
It looks like it can be done per SnpCreateUIAddMethod.jsx, like this:
win.pnl.txt = win.pnl.add('edittext', [15,15,200,35], 'Edit Me!');
Copy link to clipboard
Copied
Stupid question time ...
I have the script layout and most of the functionality working, although I haven't gotten into the meat of it yet.
How would I change the font size of the pop-up window?
See attached (attachment not loading - It says it is uploading, but never finishes) - Ideally, I'd like the window text to be slightly smaller than the window title text and the window title text to be smaller, but if the window title text can't be changed, I'd like them to be consistent.
Hard to tell sizes, but I'm thinking the title is around 12-point Arial and the window title is 10-point Arial. I'd like to change it to 12-point. (It would probably be fine on a large monitor, but I have my laptop set to 125% and I don't think it scales it - 12-point would probably work on either one.
Per the Adobe Creative Suite 6 Javascript Tools Guide, I tried:
ScriptUI.newFont ("Arial", "REGULAR", 24);
It didn't give me an error, but obviously I'm not dealing with 24-point type.
Copy link to clipboard
Copied
I recommend downloading this PDF:
https://creativepro.com/files/kahrel/indesign/scriptui.html
There used to be a dedicated ScriptUI Forum on Adobe Forums, but I am not sure it is still active. You might want to check and see if it is still there.
Copy link to clipboard
Copied
Excellent Guide - Page 79 had what I was looking for - basically, you can't apply a font to the entire window. You have use a function to apply it to all the elements, but he includes the function in the guide.
Off-topic, but didn't there used to be an ExtendScript forum also, or am I imagining that?
Copy link to clipboard
Copied
I didn't see a scriptUI forum, and I spoke a bit too soon. It changes everything except the font for panels (the heading text for the panel), but it does change the font for the elements of the panel.
I suspect that is just a bug in ScriptUI but in case I did something wrong (or if the guide gets taken down), here is what I did:
In the main function, I put:
set_font (win, "Tahoma:14");
win.show();
and added:
function set_font (control, font) {
for (var i = 0; i < control.children.length; i++) {
if ("GroupPanel".indexOf (control.children[i].constructor.name) > -1)
set_font (control.children[i], font);
else
control.children[i].graphics.font = font;
}
}
That didn't work for the panel, so I tried in the main function:
win.radioPanel = win.add("panel", [5, 10, 275, 90], "Select Customer:");
win.radioPanel.graphics.font = "Tahoma:14";
It didn't give me an error, but it didn't work ...
Copy link to clipboard
Copied
I haven't done anything with using specific fonts in ScriptUI so I am not sure what is going on. Try asking ScriptUI questions on the InDesign forum. There might even be an InDesign Scripting forum. There is a lot more traffic out there and you are likely to get an answer.
Copy link to clipboard
Copied
Will do - thank you again!!!


-
- 1
- 2