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

I need to add in a page size text variable to my already working .jsx script

New Here ,
Dec 23, 2015 Dec 23, 2015

Copy link to clipboard

Copied

Hi all,

I was wondering if any of you scripters would know how to add in a piece of code that would allow me to generate a page size variable,

I have already found a script that adds in a user name to my slug but i dont know how to add one for page size as im not fluent in JavaScript.

Any help would be much appreciated!! see current script below:

----------------------------------------------------------------------------------

#targetengine 'usernameVariable'

//This function adds static text variables in InDesign

//They won't be dynamically updated though.

//That's why we need the updating function below

function addVariables(openEvent){

   

    var doc = openEvent.parent;

   

    while ( doc.constructor.name != "Document" )

    {

        if ( doc.constructor.name == "Application" ){ return; }

      

       doc = doc.parent;

    }

   

    //Adding User name to text variables

    createTextVariable(doc, "User name", (Folder.myDocuments).parent.displayName );

   

    //Adding Computer name to text variables

    createTextVariable(doc, "HD Name", (Folder.system).parent.displayName );

   

    //Adding Computer name to text variables

    createTextVariable(doc, "Computer Name", getComputerName() );

   

    //Adding a link to my website

    createTextVariable(doc, "_About Loic Aigon", "Feel free to visit my website: http://www.loicaigon.com !" );

   

}

//Generic function to add static custom text variables

function createTextVariable(target, variableName, variableContents){

   

    var usernameVariable = target.textVariables.itemByName(variableName);

    if(!usernameVariable.isValid){

        usernameVariable = target.textVariables.add();

        usernameVariable.variableType = VariableTypes.CUSTOM_TEXT_TYPE;

        usernameVariable.name = variableName;

    }

    usernameVariable.variableOptions.contents = variableContents;

}

//Snippet for grabbing the  deep name of the computer

function getComputerName(){

   

    var APLScript = "get computer name of (system info)";

    var VBScript = "Dim wshShell\

    Set wshShell = CreateObject( \"WScript.Shell\" )\

    strComputerName = wshShell.ExpandEnvironmentStrings( \"%COMPUTERNAME%\" )\

    app.scriptArgs.SetValue \"computerName\", strComputerName";

    var scpt = (File.fs=="Windows")? VBScript : APLScript;

    var language = (File.fs=="Windows")? ScriptLanguage.visualBasic : ScriptLanguage.APPLESCRIPT_LANGUAGE;

    var scriptResult = app.doScript(scpt, language);

    var computerName = (File.fs=="Windows")? app.scriptArgs.getValue("computerName") : scriptResult;

    return computerName;

}

//Add listeners to update file when opened.

app.addEventListener('afterOpen', addVariables);

TOPICS
Scripting

Views

2.7K

Translate

Translate

Report

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
Guru ,
Dec 24, 2015 Dec 24, 2015

Copy link to clipboard

Copied

If all the pages are the same size then you can use the below, if not then I would not recommend using text variables

See The intro comments

// https://forums.adobe.com/thread/2049382

// Main script by Loic Aigon Add highly customized Text Variables to InDesign | Ozalto

// Additions (First Page Dimensions and variable update on script execution)

// and modifications (without permission, sorry Loic) by Trevor http://www.creative-scripts.com

#targetengine 'usernameVariable'

//This function adds static text variables in InDesign

//They won't be dynamically updated though.

//That's why we need the updating function below

function toTitleCase(s) { // By Trevor

    return s.toLowerCase().replace(/\b\w/g, function () {return arguments[0].toUpperCase()});

}

function myRound(thisNumber, digits) { // By Trevor

        digits = (digits) ? Math.pow(10, digits) : 1000;

        return Math.round(thisNumber * digits) / digits;

}

function addVariables(openEvent){

    if (!openEvent) return;

    var doc = (openEvent.constructor == Document) ? openEvent : openEvent.parent; // By Trevor

    while ( doc.constructor.name != "Document" )

    {

        if ( doc.constructor.name == "Application" ){ return; }

       doc = doc.parent;

    }

    //Adding User name to text variables

    createTextVariable(doc, "User name", $.getenv($.os[0] == "M" ? "USER" : "username") ); // changed by Trevor

    //Adding Computer name to text variables

    createTextVariable(doc, "HD Name", (Folder.system).parent.displayName );

    //Adding Computer name to text variables

    createTextVariable(doc, "Computer Name", getComputerName() );

    //Adding a link to my website

    createTextVariable(doc, "_About Loic Aigon", "Feel free to visit my website: http://www.loicaigon.com !" );

    //Adding a link to my website

    createTextVariable(doc, "_About Trevor", "Feel free to visit my website: http://www.creative-scripts.com !" ); // by Trevor

    //Adding First Page Dimensions

    createTextVariable(doc, "First Page Dimensions", getPageDimentions(doc.pages[0])); // by Trevor

}

addVariables(app.properties.activeDocument); // callback by Trevor

//Generic function to add static custom text variables

function createTextVariable(target, variableName, variableContents){

    var usernameVariable = target.textVariables.itemByName(variableName);

    if(!usernameVariable.isValid){

        usernameVariable = target.textVariables.add();

        usernameVariable.variableType = VariableTypes.CUSTOM_TEXT_TYPE;

        usernameVariable.name = variableName;

    }

    usernameVariable.variableOptions.contents = variableContents;

}

//Snippet for grabbing the  deep name of the computer

function getComputerName(){ // by Trevor

    return $.os[0] == "M" ? app.doScript("get computer name of (system info)", ScriptLanguage.APPLESCRIPT_LANGUAGE) : $.getenv("computername");

}

function getPageDimentions(page){ // by Trevor

    var width, height, bounds, units, decPlaces;

    // function presumes that normal pages

    // i.e. same vertical and horizontal and no page shape transformations

    decPlaces = 3;

    units = toTitleCase(page.parent.parent.viewPreferences.horizontalMeasurementUnits.toString()); // If you forgot to include the toTitleCase function then remove the method

    bounds = page.bounds;

    height = bounds[2] - bounds[0];

    height = myRound(height, decPlaces);

    width = bounds[3] - bounds[1];

    width = myRound(width, decPlaces);

    return width + " X " + height + " " + units

}

//Add listeners to update file when opened.

app.addEventListener('afterOpen', addVariables);

// You might like to add some or all of these to make the update better

app.addEventListener('beforeSave', addVariables); // by Trevor

app.addEventListener('beforeSaveAs', addVariables); // by Trevor

app.addEventListener('beforeSaveACopy', addVariables); // by Trevor

app.addEventListener('beforeExport', addVariables); // by Trevor

app.addEventListener('beforePrint', addVariables); // by Trevor

app.addEventListener('beforeDeactivate', addVariables); // by Trevor

app.addEventListener('afterActivate', addVariables); // by Trevor

Votes

Translate

Translate

Report

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
New Here ,
Dec 27, 2015 Dec 27, 2015

Copy link to clipboard

Copied

This is Fantastic, thank you so much, just a couple more questions,

is there anyway to get the unit to display as "mm, in, px etc...." instead of Millimeters, Inches, Pixels,

also is there anyway that this script could look at the pahe size tool sizes instead of document setup sizes incase of multipage docs?

and also can bleed size be added?

Thanks again!!!

Votes

Translate

Translate

Report

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
Guru ,
Dec 27, 2015 Dec 27, 2015

Copy link to clipboard

Copied

Hi Scott

mm, in etc. would be very easy as would bleed size.

If you want a page size variable that shows the size of each page that it appears on then you would need  a c++ custom plugin that would be quite expensive.

In my opinion what you need is a script that adds a textFrame in the bleed of each page of the document with the info that you want and not use variables.

This is a scripting forum which is for scripters to learn / share about scripting and not a script request forum.

For script requests I personally highly recommend Contact | Creative-Scripts.com some might say that as the site owner I'm biased.

Wouldn't cost a fortune.

I've got the flue and going to bed soon so don't expect any speedy response.

Regards

Trevor

Votes

Translate

Translate

Report

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
New Here ,
Jan 20, 2025 Jan 20, 2025

Copy link to clipboard

Copied

This script stopped working for some reason. It won't update the page size anymore. Is there something i'm missing. I'm on the latest Indesign. Any help would be greatly appreciated. Thanks.

Votes

Translate

Translate

Report

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 ,
Jan 20, 2025 Jan 20, 2025

Copy link to clipboard

Copied

quote

This script stopped working for some reason. It won't update the page size anymore. Is there something i'm missing. I'm on the latest Indesign. Any help would be greatly appreciated. Thanks.


By @attilioL

 

Do you get any errors? 

 

Votes

Translate

Translate

Report

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
New Here ,
Jan 20, 2025 Jan 20, 2025

Copy link to clipboard

Copied

LATEST
No errors. When I change page size and save it doesn’t reflect the new size

Get Outlook for iOS<>

Votes

Translate

Translate

Report

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
Explorer ,
Jul 11, 2018 Jul 11, 2018

Copy link to clipboard

Copied

Scott, is there a reason why this script works fine in Mac OS "High Sierra", but slows down Indesign to a crawl in "Sierra"? When i open the script in the editor i get this error:

Screen Shot 2018-07-11 at 2.42.07 PM.png

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 12, 2018 Jul 12, 2018

Copy link to clipboard

Copied

Trevorׅ​

(without permission, sorry Loic)

Waaaa

No problem, it's nice already that you took care of mentioning the source

Votes

Translate

Translate

Report

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
Guru ,
Jul 12, 2018 Jul 12, 2018

Copy link to clipboard

Copied

Loic.Aigon​ Anytime

attil​ I don't know why the script slows down InDesign on Sierra. Sounds strange

The linting bug is not connected and is not a bug in the script, the problem is that linters don't understand ExtendScripts # commands, you have to either set the linter to ignore them or just ignore the messages.

Votes

Translate

Translate

Report

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
Guru ,
Jul 12, 2018 Jul 12, 2018

Copy link to clipboard

Copied

Attil, you can try changing parts of the script to see if there's something in particular that's slowing it down.

Change units = toTitleCase(page.parent.parent.viewPreferences.horizontalMeasurementUnits.toString());

To units = page.parent.parent.viewPreferences.horizontalMeasurementUnits.toString();

Remove the applescript and just return a fixed string.

Restart the app each time before you try out the change then you should be able to identify a point where things start to run smoothly and can work on changing the problematic element.

Votes

Translate

Translate

Report

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
Explorer ,
Jul 12, 2018 Jul 12, 2018

Copy link to clipboard

Copied

Will do, i'll give it a shot when i get a sec. Thank you for your help. I'll let you know how it goes.

Votes

Translate

Translate

Report

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 ,
Jul 12, 2018 Jul 12, 2018

Copy link to clipboard

Copied

attil  wrote

Scott, is there a reason why this script works fine in Mac OS "High Sierra", but slows down Indesign to a crawl in "Sierra"? When i open the script in the editor i get this error:

Screen Shot 2018-07-11 at 2.42.07 PM.png

Ah. Then this is the original post and that one is a double post initially done in the InDesign forum:

Script works fine in "High Sierra" but not "Sierra"

Should we run both?

Or should the one I linked to be closed?

Regards,
Uwe

Votes

Translate

Translate

Report

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