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

Script pallete

Enthusiast ,
Jul 30, 2017 Jul 30, 2017

Copy link to clipboard

Copied

Hello, there.

I'm trying to populate a little pallete window with script files in a folder.

I'm able to create the window, automaticaly create the buttons for each script file in the folder. But I can't set a function to each button. What am I doing wrong?

#target indesign

#targetengine "Menu de Scripts"

var myFolder = Folder("~/Downloads/Nova pasta");

if (myFolder.exists) {

    var myFiles = new Array;

    GetSubFolders(myFolder);

    if (myFiles.length > 0) {

        myFiles.sort();

        var myScriptList = myFiles.join(";");

        var scriptFolder = RegExp((String(Folder.decode(myFolder))+"/"),"gi");

        var scriptFile = String(myScriptList).replace(scriptFolder,"");

        var scriptFiles = String(scriptFile).replace(/\.jsx/g,"");

        var myScriptsList = scriptFiles.split(";");

       

        var myMenu = new Window ("palette","Script by LFCorullón", undefined, {resizeable: false});

        var myG = myMenu.add ("group");

        myG.orientation = "column";

       

        for (var ) {

        for (var s=0; s<myScriptsList.length; s++) {

            var myFile = File(myFolder+"/"+myScriptsList+".jsx");

            var myBtn = myG.add ("button", [0,0,150,25], myScriptsList);

            myBtn.onClick = function() { app.doScript(myFile); }

            alert(myFile);

            }

        }

        myMenu.show();

        }

    }

// FUNÇÕES ======================================================================================

function GetSubFolders(theFolder) {

    var myFileList = theFolder.getFiles();

    for (var q = 0; q < myFileList.length; q++) {

        var myFile = myFileList;

        if (myFile instanceof Folder){

            GetSubFolders(myFile);

            }

        else if (myFile instanceof File && myFile.name.match(/\.js.+$/i)) {

            myFiles.push(File.decode(myFile));

            }

        }

    }

TOPICS
Scripting

Views

987

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

correct answers 1 Correct answer

People's Champ , Jul 31, 2017 Jul 31, 2017

You have a reference issue as you redeclare the same variable over and over. A solution is to have separate references while storing buttons in an array:

for (var s=0; s<myScriptsList.length; s++) { 

            var myFile = File(myFolder+"/"+myScriptsList+".jsx"); 

btns.push ( myG.add ("button", [0,0,150,25], myScriptsList) );

              btns[btns.length-1].file = myFile;

            btns[btns.length-1].onClick = function() {

app.doScript(this.file);

            } 

        } 

Votes

Translate

Translate
People's Champ ,
Jul 31, 2017 Jul 31, 2017

Copy link to clipboard

Copied

You have a reference issue as you redeclare the same variable over and over. A solution is to have separate references while storing buttons in an array:

for (var s=0; s<myScriptsList.length; s++) { 

            var myFile = File(myFolder+"/"+myScriptsList+".jsx"); 

btns.push ( myG.add ("button", [0,0,150,25], myScriptsList) );

              btns[btns.length-1].file = myFile;

            btns[btns.length-1].onClick = function() {

app.doScript(this.file);

            } 

        } 

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
Enthusiast ,
Jul 31, 2017 Jul 31, 2017

Copy link to clipboard

Copied

Thank you so much!!! Great!

I didn't understand your fragment yet. But it works.

You created a new array (btns) and, using the for loop, "push" each button from the length to the zero. Right?

Then, set the property file for each button to its jsx file.

Then, set the onClick function pointing to the property you declare in the past step? (this.file)


Did I understand well?

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 ,
Aug 01, 2017 Aug 01, 2017

Copy link to clipboard

Copied

Yes the idea was to establish a clear separation between buttons reference. The array helps in that goal. Adding a file property to the button instance is convenient because the on click listener is generic and will retrieve the File instance for a specific button.

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
Enthusiast ,
Sep 14, 2017 Sep 14, 2017

Copy link to clipboard

Copied

Just to share...

The idea is provide ever updated scripts to the users in a newsroom, avoiding having to copy the jsx files to the user desktop.

So, I create this palette to call the jsx files from the server.

#target indesign

#targetengine "Menu de Scripts"

//DESCRIPTION: Descrição do script (tooltip, helptip)

//=============================================================

//  Script by Luis Felipe Corullón

//  Contato: lf@corullon.com.br

//  Site: http://lf.corullon.com.br

//=============================================================

var myFolder = Folder("server folder where the scripts are");

if (myFolder.exists) {

    var myFiles = new Array;

    GetSubFolders(myFolder);

    if (myFiles.length > 0) {

        myFiles.sort();

        var myScriptList = myFiles.join(";");

        var scriptFolder = RegExp((String(Folder.decode(myFolder))+"/"),"gi");

        var scriptFile = String(myScriptList).replace(scriptFolder,"");

        var scriptFiles = String(scriptFile).replace(/\.jsx/g,"");

        var myScriptsList = scriptFiles.split(";");

       

        var myMenu = new Window ("palette","Script by LFCorullón", undefined, {resizeable: false});

        var myG = myMenu.add ("group");

        myG.orientation = "column";

        myMenu.margins = [5,5,5,5];

        myG.spacing = 5;

        var minimizar = myG.add ("button", [0,0,200,25], "*** Minimize ***");

        minimizar.onClick = function() { if (myMenu.size.height == size) { myMenu.size.height = 35 } else { myMenu.size.height = size } }

       

        var btns = [];

        for (var s=0; s<myScriptsList.length; s++) {

            var myFile = File(myFolder+"/"+myScriptsList+".jsx");

            btns.push ( myG.add ("button", [0,0,200,25], myScriptsList) );

            btns[btns.length-1].file = myFile;

            btns[btns.length-1].onClick = function() { app.doScript(this.file); }

            }

        myMenu.show();

        var size = myMenu.size.height;

        }

    else {

        alert("There is no script files in the specified folder.", "Script by LFCorullón");

        }

    }

else {

    alert("The specified folder doesn't exists.", "Script by LFCorullón");

    }

// FUNÇÕES ======================================================================================

function GetSubFolders(theFolder) {

    var myFileList = theFolder.getFiles();

    for (var q = 0; q < myFileList.length; q++) {

        var myFile = myFileList;

        if (myFile instanceof Folder){

            GetSubFolders(myFile);

            }

        else if (myFile instanceof File && myFile.name.match(/\.js.+$/i)) {

            myFiles.push(File.decode(myFile));

            }

        }

    }

Thanks for Loic.Aigon​ for help me in the buttons.

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 ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

For what it's worth, you can use a shortcut to the script inside the script panel. So you could have one unique location and have all your users use the file through the shortcut. In some occasions, that may not work if the script needs dependencies that you will try to reach through $.fileName for example.

Apart from that, I had one client on mac who was able to dispatch a script update to several dozens of colleagues through an app. He could monitor everyone's computer and put a file to some locations. I can't remember the app name right now but just in case.

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
Enthusiast ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

LATEST

Yes. Are you referring to the shortcut in the palette, right? If I just set

a shortcut to the script file (using the InDesign shortcut dialog window),

this didn't work. Everytime I start InDesign, I don't know why, the

shortcuts are gone. As I understand, InDesign sets different ID for each

script file everytime it starts.

Using the shortcut key parameter in the palette I wrote, I just be able to

use the combinations with Alt key, right? I don't find any javascript

reference to use other key combinations.

I have an app called SyncBack (that have free and paid versions) that could

be used to sync those files. But, the IT department on my clients doesn't

depends on me so, it's easier to use this palette calling the files from

the server (where I can keep all of them updated) instead of involving my

boss to ask the IT coordinator of each client of us, etc...

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