Reputation: 69
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
Upvotes: 1
Views: 79
Reputation: 69
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 😊
Upvotes: 0
Reputation: 14537
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.
Upvotes: 1