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

Collect a list of files from a folder.

Engaged ,
Oct 18, 2020 Oct 18, 2020

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.

Captura de tela 2020-10-19 001856.png
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!

 
TOPICS
Actions and scripting

Views

926

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

Community Expert , Oct 19, 2020 Oct 19, 2020

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
...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 19, 2020 Oct 19, 2020

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.

JJMack

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
Engaged ,
Oct 19, 2020 Oct 19, 2020

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.

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 ,
Oct 19, 2020 Oct 19, 2020

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...

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 ,
Oct 19, 2020 Oct 19, 2020

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()
        }

 

 

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
Engaged ,
Oct 19, 2020 Oct 19, 2020

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.

 

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
Advocate ,
Jan 03, 2024 Jan 03, 2024

Copy link to clipboard

Copied

@Chuck Uebele 

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

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 03, 2024 Jan 03, 2024

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.

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
Advocate ,
Jan 04, 2024 Jan 04, 2024

Copy link to clipboard

Copied

I'll try that method. Was doing some reading about splice. Look quite handy

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
Advocate ,
Jan 05, 2024 Jan 05, 2024

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?

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 05, 2024 Jan 05, 2024

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.

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
Advocate ,
Jan 05, 2024 Jan 05, 2024

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.

 

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
Advocate ,
Jan 08, 2024 Jan 08, 2024

Copy link to clipboard

Copied

LATEST

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]);
    }
}

 

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 ,
Oct 19, 2020 Oct 19, 2020

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.

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
Engaged ,
Oct 19, 2020 Oct 19, 2020

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()

 

 

 

 

 

 

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