Copy link to clipboard
Copied
Greetings everyone!
I'm trying to create a user interface containing a listbox and some buttons (find, remove and process), I wanted to make this functional as follows:
1-: When I click on the "Find" button, it will search for a folder so that I can select some images. jpg, png, tiff ......
2-: Add to Listbox a list of all selected images.
3-: How to remove an item from the Listbox using the "Remove" button.
4-: Open and do anything on each image in the list when I click the "OK" button.
That was all I got so far. All help is valid;
dialog = new Window("dialog");
dialog.text = "Dialog";
lbx_array = "";
lbx = dialog.add("listbox", undefined, undefined, {name: "lbx", items: lbx_array});
lbx.preferredSize.width = 100;
lbx.preferredSize.height = 100;
b1 = dialog.add("button", undefined, undefined, {name: "b1"});
b1.text = "Localizar";
b1.onClick = function() {
var selectedFolder = Folder.selectDialog("Please select folder");
var fileList= selectedFolder.getFiles(/\.(?:png|gif|jpg|bmp|tif|psd)$/i);
lbx_array = fileList;
dialog.update();
}
b2 = dialog.add("button", undefined, undefined, {name: "b1"});
b2.text = "Remove";
b3 = dialog.add("button", undefined, undefined, {name: "b2"});
b3.text = "Ok";
dialog.show();
Thank you!
Try this:
var dlg = new Window('dialog','Dialog');
var lbx_array = [];
var lbx = dlg.add('listbox', undefined, undefined, {name: 'lbx', items: lbx_array});
lbx.preferredSize.width = 300;
lbx.preferredSize.height = 400;
var b1 = dlg.add('button', undefined, 'Localizar' );
b1.onClick = function() {
var selectedFolder = Folder.selectDialog('Please select folder');
var fileList= selectedFolder.getFiles(/\.(?:png|gif|jpg|bmp|tif|psd)$/i);
lbx.removeAll();
lbx_array = fil
...
Copy link to clipboard
Copied
Sound like you want to steal(reuse) the code in Adobe's script Load File into a Stack. With its dialog and code you can have the user build the file list then have the script process the file list. All you would need do is the change the script function the load files into a stack change the script name and perhaps generate a new UUID. I have not look at the script code. You may want to.
Copy link to clipboard
Copied
Did you say steal? What a nastiness, I confess that I never explored this Adobe script Load a file in a stack, maybe the answer is really in it. Grateful for the attention.
Copy link to clipboard
Copied
I think to remember that some of the scripts were intended as examples, so that their code could be reporposed, indeed, it might be the wrong term...
Copy link to clipboard
Copied
Try this:
var dlg = new Window('dialog','Dialog');
var lbx_array = [];
var lbx = dlg.add('listbox', undefined, undefined, {name: 'lbx', items: lbx_array});
lbx.preferredSize.width = 300;
lbx.preferredSize.height = 400;
var b1 = dlg.add('button', undefined, 'Localizar' );
b1.onClick = function() {
var selectedFolder = Folder.selectDialog('Please select folder');
var fileList= selectedFolder.getFiles(/\.(?:png|gif|jpg|bmp|tif|psd)$/i);
lbx.removeAll();
lbx_array = fileList;
for(i=0;i<lbx_array.length;i++){
lbx.add('item',lbx_array[i])
}
dlg.update();
}
var b2 = dlg.add('button', undefined, undefined, {name: 'b1'});
b2.text = 'Remove';
b2.onClick = function(){
var selNum = lbx.selection.index;
lbx.remove(selNum);
lbx_array.splice (selNum, 1);
}
var b3 = dlg.add('button', undefined, undefined, {name: 'b2'});
b3.text = 'Ok';
b3.onClick = function(){
var selNum = lbx.selection.index;
var doc = open( lbx_array[selNum])
dlg.close()
}
Copy link to clipboard
Copied
Hi Chuck Uebele. Man you are very kind, always showing up in the most difficult moments, your script worked almost 100% the way I imagined, this is a huge advance.
Since we already have all the files in the listbox, now we only need to open not only the selected item, but all of the list, each file is opened in sequence and so I can add my script with some functions in each one. Again very grateful for your attention.
Copy link to clipboard
Copied
Am I correctly understijg that only splice() is the correct method of removing and item from an array?
I tried JavaScript methods but that won't work. Also remove() and delete() don't work
Copy link to clipboard
Copied
There are various methods for removing an item from an array, but I think splice() works best. I'm not sure about the remove() method, but the delete() method does work, but it leaves a hole in your array.
Copy link to clipboard
Copied
I'll try that method. Was doing some reading about splice. Look quite handy
Copy link to clipboard
Copied
Well i still see an issue, when the array is build from a string using this method
var filesPathLst = new Array();//[]; //
// Generate list from exportInfo
filesPathLstStr = exportInfo.filesPathLst.split(',');
for (idx in filesPathLstStr){
if (filesPathLstStr[idx]!="")
filesPathLst.push(filesPathLstStr[idx]);
}
splice() seems to work
But when the array is filled with files from a dialog and they are an object, splice will not work. Do you know how to remove an item than.?
EDIT
its actually the otherway arround. When its an object it works. But when the items are made from a string it doesnt. How can i remove/delete items when they are made of a string?
Copy link to clipboard
Copied
I didn't have any issues with strings, in the array, but I used the array position rather than the contents of the item.
Copy link to clipboard
Copied
I found the issue. The issue is than when the array is made of strings, File.decode(fileName[i].name) returns undefined
But when i do it like this, it does seem to work
if(typeof(filesPathLst[i])=="string"){
fileName = new File(filesPathLst[i]);
if (File.decode(fileName.name) == selList[s].text){
filesPathLst.splice(i,1);
break;
}
}
if(typeof(filesPathLst[i])=="object"){
if (File.decode(filesPathLst[i].name) == selList[s].text){
filesPathLst.splice(i,1);
break;
}
}
The reason why the array changes, is because i also use ObjectToDescriptor to store all the settings. Since that doesnt take an array, i convert it to a string, than on opening i convert it back to a list. But than the filepaths are string and not object.
Copy link to clipboard
Copied
Does anyone know why the files list is not changed after using splice(). Ive tried to catch the file type using typeOff, so im sure im using the correct method. After the splice(), the files list doesnt seem to be changed.
Edit
i guess the issue for me was, initially i had copy/paste the function from CreateStackImages.jsx where it declared i as a variable. But this was used throught out the script a couple times more. I now gave it a unique name and this seems to help. Though i wonder why that is. Because i is declared inside other function, its not a global var name. But if i used i, it would return the wrong file name.
Ive now updated it with different var name and it seems to be working correct now
function removeOnClick(){
var file, s;
var selList = sourcePathLst.selection;
for (s in selList){
for (file in filesPathLst) {
if(typeof(filesPathLst[file])=="string"){
fileName = File(filesPathLst[file]);
if (File.decode(fileName.name) == selList[s].text){
filesPathLst.splice(file,1);
break;
}
}
if(typeof(filesPathLst[file])=="object"){
if (File.decode(filesPathLst[file].name) == selList[s].text){
filesPathLst.splice(file,1);
break;
}
}
}
sourcePathLst.remove(selList[s]);
}
}
Copy link to clipboard
Copied
Yea, I wasn't sure what exactly you wanted to do with opening files. If you want to open all of them, then create a loop using the lbx_array, as the length of the loop, and place the open code in the loop, with the counter to open each instance in the array.
Copy link to clipboard
Copied
Perfect!
Chuck Uebele you are a genius, very grateful for your help.
Perhaps someone else can study this subject:
Final script: Updated script
var dlg = new Window('dialog','Dialog');
var lbx_array = [];
var lbx = dlg.add('listbox', undefined, undefined, {name: 'lbx', items: lbx_array});
lbx.preferredSize.width = 300;
lbx.preferredSize.height = 400;
var b1 = dlg.add('button', undefined, 'Localizar' );
b1.onClick = function() {
/// added feature to be able to select more than one file
var selectedFolder = File.openDialog ("Select the file", Multiselect = true)
var fileList = selectedFolder;
//var selectedFolder = Folder.selectDialog('Please select folder');
//var fileList= selectedFolder.getFiles(/\.(?:png|gif|jpg|bmp|tif|psd)$/i);
lbx.removeAll();
lbx_array = fileList;
for(i=0;i<lbx_array.length;i++){
lbx.add('item',decodeURI(lbx_array[i].name)); /// Added: Display only the list of file names and extensions
}
dlg.update();
}
var b2 = dlg.add('button', undefined, undefined, {name: 'b1'});
b2.text = 'Remove';
b2.onClick = function(){
var selNum = lbx.selection.index;
lbx.remove(selNum);
lbx_array.splice (selNum, 1);
}
var b3 = dlg.add('button', undefined, undefined, {name: 'b2'}); b3.text = 'Ok';
b3.onClick = function(){
for (var i=0;i<lbx_array.length; i++){
var doc = open(lbx_array[i])
alert ("My script");
}
dlg.close()
}
dlg.show()