skip to Main Content

Here is the whole script.

    UI();

    function UI() {

    var window = new Window ('dialog', 'Settings', undefined, {closeButton: true});
    window.orientation = 'column';

    var iFolderC = loadSettings (['I','F',0]);
    for (i = 1; i <= iFolderC; i++)
    eval('var ' + "iFolder" + i + " = loadSettings (['I','F'," + i + "]);");

    var inputGroup = window.add ('group');
    inputGroup.add('panel', undefined, 'Input Folders', {borderStyle:'raised'});
    inputGroup.orientation = 'column';
    //inputGroup.alignment = 'left';

    for (i = 1; i <= iFolderC; i++) {
    eval('var ' + "inputFolder" + i + "= inputGroup.add ('group');");
    eval("inputFolder" + i + ".add('panel', [0, 0, 300, 20], iFolder"+ i +", {borderStyle:'raised'});");
    eval('var ' + "removeButton" + i + "= inputFolder" + i + ".add ('button', undefined, 'Remove');");}

    var newFolder = inputGroup.add ('group');
    //var folderButton = newFolder.add("iconbutton", undefined, "#FolderOpened");
    var newIFolder = newFolder.add('edittext',[0, 0, 300, 20]);
    var addButton =  newFolder.add ('button', undefined, 'Add');
    addButton.onClick = function () {window.close(); saveSettings (['N', 'F', 0], newIFolder.text);}

    for (i = 1; i <= iFolderC; i++) {
    eval("removeButton" + i + ".onClick = function() {window.close(); saveSettings (['R', 'F'," + i + "], '');}");}

    var closeButton = window.add ('button', undefined, 'Close');
    window.cancelElement = closeButton

    window.center();
    window.show();
    }

    function saveSettings (Identity, Fixture) {
    var file = '~/Documents/Read and write text102323210.txt';
    var str = read (file);
    var arr = str.split('n');
    var c = Math.floor(arr[0]);

    if (Identity[0] == 'N') {
        if (Identity[1] == 'F') {
            arr[c+1] = Fixture;
            arr[0] = c + 1;
            str = arr.toString().replace(/,/g, 'n');
            write (file, str);           
            UI();}}

        if (Identity[0] == 'R') {
        if (Identity[1] == 'F') {
            if (Identity[2] == c) {
                arr[c] = '';
                arr[0] = c - 1;
                str = arr.toString().replace(/,/g, 'n');
                write (file, str);           
                UI();}}}
                    if (Identity[2] < c) {
                        for ( i = 0; i <= c - Identity[2]; i++)
                            arr[Identity[2]] = arr[Identity[2]+1];
                        arr[c] = '';
                        arr[0] = c - 1;
                        str = arr.toString().replace(/,/g, 'n');
                        write (file, str);           
                        UI();}}

    function loadSettings (Identity) {
    var file = '~/Documents/Read and write text102323210.txt';
    var str = read (file);
    var arr = str.split('n');
    
    if (Identity[0] == 'I')
        if (Identity[1] == 'F')
            if (Identity[2] == 0)
                return Math.floor(arr[Identity[2]]);
                    else return arr[Identity[2]];}

    function read (filePath) {
    var file = new File (filePath);    
    file.open('r');
    file.encoding = 'UTF8';
    var text = file.read();
    file.close(); 
    return text;}

    function write (filePath, textContent) {  
    var file = new File( filePath );   
    file.open('w');
    file.encoding = 'UTF8';
    file.write( textContent );
    file.close();}`

Add/remove buttons work fine. When I click on close button instead of closing the window it starts to remove folders, sometimes one other times two at the time. When no more folders are left then it closes. Topright corner close button deletes all folders at once.

I’ve tried: window and palette, window acts the same, palette just goes bannans.
turning …{closeButton: true});, on and off.

closeButton.onClick = function () {window.close();}
closeButton.onClick = function () {window.close(); break;}
closeButton.onClick = function () {window.close(); return;}
closeButton.onClick = function () {window.close(); return 1;}
closeButton.onClick = function () {window.close(); exit();}

..also tried calling a funktion and returning window.close;

..it’s like it just continiues and keeps on clicking on remove buttons 🙁

Please help and thanks in advance

3

Answers


  1. Chosen as BEST ANSWER

    I've be reading a bit mostly ScriptUI 2.8 Tutorial and sotra found out that I need use addEventListerner for what I'm planning to achieve.

    var w = new Window ("dialog");
    var buttongroup = w.add ("group");
    var panel1 = buttongroup.add ("panel", undefined, "Buttons");
     for (var i = 0; i < 5; i++)
     panel1.add ("button", undefined, "Button " + i);
    var panel2 = buttongroup.add ("panel", undefined, "Knoppen");
     for (var i = 0; i < 5; i++)
     panel2.add ("button", undefined, "Button " + i);
    buttongroup.addEventListener('click', button_pressed);
    function button_pressed (e)
     {
     if (e.target.type == "button")
     $.writeln (e.target.text + " from panel " + e.target.parent.text);
     }
    w.show();
    

    Cuz my UI (once finished, haha) gonna have many buttons and several tabs. I've bitten more then I could chew for my very first javascript, ever :D. Also I've sorta given up on dynamic variables/statments "eval()" it's just waste of time! (For me at this stage, at least) However, it would be nice if I could make the buttons dynamic. This can be done in a loop as in the example above.

    The main script is partially done and it works :D, I’ll be adding to it for a while to come, tho. But the main function is done and it saves me hours of tedious work every week.

    Thanks for your replies guys and best regards 😊


  2. To find the values of x and y given a certain value equal to yx^2, you can use a brute force approach by iterating over possible integer values of x and y until you find a combination that satisfies the equation.

    // Define the close button
    var closeButton = window.add('button', undefined, 'Close');
    window.cancelElement = closeButton;
    
    // Define the onClick event handler for the close button
    closeButton.onClick = function() {
        // Close the window
        window.close();
    };
    

    the find_xy function iterates over possible values of x from 1 to the square root of the given value. This is because y will be at least 1, and the maximum possible value of x is the square root of the given value.

    For each value of x, it calculates y based on the equation yx^2 and checks if the equation is satisfied.
    If a solution is found, it returns the values of x and y. Otherwise, it returns None if no solution is found.
    
    Login or Signup to reply.
  3. It’s hard to tell what the script does without a sample of input data and some description. But it looks for me like you trying to keep the window on the screen after you click ‘Add’ button and you’re calling the new Widnow() for this. It can be a problem for ‘dialog window’. There are a ‘palette window’ that is staying on the screen all the time until you close it manually.

    Here is the example how it could be done as a ‘palette’:

    #targetengine 'session'
    
    var window = new Window ('palette', 'Settings', undefined, {closeButton: true});
    window.orientation = 'column';
    
    var iFolderC = loadSettings (['I','F',0]);
    for (var i = 1; i <= iFolderC; i++) {
        eval('var ' + "iFolder" + i + " = loadSettings (['I','F'," + i + "]);");
    }
    
    var inputGroup = window.add ('group');
    inputGroup.add('panel', undefined, 'Input Folders', {borderStyle:'raised'});
    inputGroup.orientation = 'column';
    
    for (i = 1; i <= iFolderC; i++) {
        eval('var ' + "inputFolder" + i + "= inputGroup.add ('group');");
        eval("inputFolder" + i + ".add('panel', [0, 0, 300, 20], iFolder"+ i +", {borderStyle:'raised'});");
        eval('var ' + "removeButton" + i + "= inputFolder" + i + ".add ('button', undefined, 'Remove');");
    }
    
    var newFolder = inputGroup.add ('group');
    
    var newIFolder = newFolder.add('edittext',[0, 0, 300, 20]);
    
    var addButton =  newFolder.add ('button', undefined, 'Add');
    addButton.onClick = function () {
        saveSettings (['N', 'F', 0], newIFolder.text);
        newIFolder.text = ''; // reset input field
    }
    
    for (i = 1; i <= iFolderC; i++) {
        eval("removeButton" + i + ".onClick = function() {saveSettings (['R', 'F'," + i + "], '');}");
    }
    
    var closeButton = window.add ('button', undefined, 'Close');
    closeButton.onClick = function () {
        window.close();
    }
    
    window.center();
    window.show();
    
    function saveSettings (Identity, Fixture) {
        var file = '~/Documents/Read and write text102323210.txt';
        var str = read (file);
        var arr = str.split('n');
        var c = Math.floor(arr[0]);
    
        if (Identity[0] == 'N') {
            if (Identity[1] == 'F') {
                arr[c+1] = Fixture;
                arr[0] = c + 1;
                str = arr.toString().replace(/,/g, 'n');
                write (file, str);
            }
        }
    
        if (Identity[0] == 'R') {
            if (Identity[1] == 'F') {
                if (Identity[2] == c) {
                    arr[c] = '';
                    arr[0] = c - 1;
                    str = arr.toString().replace(/,/g, 'n');
                    write (file, str);
                }
            }
        }
    
        if (Identity[2] < c) {
            for (i = 0; i <= c - Identity[2]; i++) arr[Identity[2]] = arr[Identity[2]+1];
            arr[c] = '';
            arr[0] = c - 1;
            str = arr.toString().replace(/,/g, 'n');
            write (file, str);
        }
    }
    
    function loadSettings (Identity) {
        var file = '~/Documents/Read and write text102323210.txt';
        var str = read (file);
        var arr = str.split('n');
    
        if (Identity[0] == 'I') {
            if (Identity[1] == 'F') {
                if (Identity[2] == 0) {
                    return Math.floor(arr[Identity[2]]);
                }
                else {
                    return arr[Identity[2]];
                }
            }
        }
    }
    
    function read (filePath) {
        var file = new File (filePath);
        file.open('r');
        file.encoding = 'UTF8';
        var text = file.read();
        file.close();
        return text;
    }
    
    function write (filePath, textContent) {
        var file = new File( filePath );
        file.open('w');
        file.encoding = 'UTF8';
        file.write( textContent );
        file.close();
    }
    

    I’m not sure if the code does exactly what you want, it’s just a half-working example of the ‘palette’ concept. The main point: you don’t have to recreate the window every time you click ‘Add’ button. And if you need to change the window after click the ‘Add’ button it can be done as well without closing and creating the window anew.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search