Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Extendscript and Date Formats

Engaged ,
May 17, 2023 May 17, 2023

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.

3.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 7 Correct answers

Community Expert , May 18, 2023 May 18, 2023

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

...
Translate
Mentor , May 18, 2023 May 18, 2023

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

...
Translate
Community Expert , May 18, 2023 May 18, 2023
#target framemaker

var doc, variable;

doc = app.ActiveDoc;
variable = doc.GetNamedVarFmt ("Current Date (Long)");
variable.Fmt = "<$year>";
Translate
Community Expert , May 22, 2023 May 22, 2023

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.

Translate
Community Expert , May 22, 2023 May 22, 2023

The datepicker won't work in ExtendScript because it uses a widget that is meant to be displayed in a browser.

Translate
Community Expert , May 24, 2023 May 24, 2023

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.

Translate
Engaged , Jun 01, 2023 Jun 01, 2023

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

...
Translate
Community Expert ,
May 18, 2023 May 18, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 22, 2023 May 22, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 22, 2023 May 22, 2023

I couldn't find a converter for YYYYMMDD to DD MMM YYYY - but it seems easy enough to parse and recompose.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2023 May 22, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2023 May 22, 2023

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];
    }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2023 May 22, 2023

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];
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 22, 2023 May 22, 2023

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2023 May 22, 2023

The datepicker won't work in ExtendScript because it uses a widget that is meant to be displayed in a browser.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
May 18, 2023 May 18, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2023 May 18, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
May 18, 2023 May 18, 2023

OK, thanks. I didn't test it. But I have to tell you, that idea about a custom editor is pure genious 🙂

 

Russ

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 18, 2023 May 18, 2023

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!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 18, 2023 May 18, 2023

Specifically - for another project, how would I set the system Current Date (Long) variable to <?year> via Script?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2023 May 18, 2023
#target framemaker

var doc, variable;

doc = app.ActiveDoc;
variable = doc.GetNamedVarFmt ("Current Date (Long)");
variable.Fmt = "<$year>";
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 18, 2023 May 18, 2023

Works awesomely!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 18, 2023 May 18, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 22, 2023 May 22, 2023

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 ...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 22, 2023 May 22, 2023

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!');

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 24, 2023 May 24, 2023

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.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2023 May 24, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 24, 2023 May 24, 2023

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 24, 2023 May 24, 2023

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 ...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2023 May 24, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 24, 2023 May 24, 2023

Will do - thank you again!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines