How to open the files in subfolders"photo1,photo2,photo3"

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

bamboo
Posts: 10
Joined: Fri Jul 29, 2016 7:14 pm

How to open the files in subfolders"photo1,photo2,photo3"

Post by bamboo »

When I choose the folder "photo",it can open "photo-001.jpg",but can not open the files in "photo1,photo2,photo3"
(see the upload picture)
under is the js

/////////////////////////////////////////////
app.bringToFront();
res ="dialog { \
text:'Open photo',\
group: Group{orientation: 'column',alignChildren:'left',\
photoO:Group{ orientation: 'row', \
b: Button {text:'Photofolder', properties:{name:'open'} ,helpTip:'all photo'},\
s: EditText { text:'', preferredSize: [360, 20] },\
},\
gg: Group{orientation: 'column',alignChildren:'left' },\
timeline:Progressbar{bounds:[0,0,400,10] , minvalue:0,maxvalue:100}\
aa: Button { text:'Open'}, \
}\
}";


win = new Window (res);

var photoOpen=win.group.photoO

photoOpen.b.onClick = function() {
var defaultFolder = photoOpen.s.text;
var testFolder = new Folder(defaultFolder);
if (!testFolder.exists) {
defaultFolder = "~";
}
var selFolder = Folder.selectDialog("Photofolder", defaultFolder);
if ( selFolder != null ) {
photoOpen.s.text = selFolder.fsName;
photoOpen.s.helpTip = selFolder.fsName.toString();
}
}


win.group.aa.onClick=function(){

win.close ()
}

//////////////
win.center();
win.show();

// Use the path to the application and append the samples folder
var samplesFolder = Folder(win.group.photoO.s.text);
//var samplesFolder = Folder("‪C:/Users/cce/Desktop"+ "/BBB")

//Get all the files in the folder
var fileList = samplesFolder.getFiles()

// open each file
for (var i = 0; i < fileList.length; i++) {
// The fileList is folders and files so open only files
if (fileList instanceof File) {
open(fileList)


}
}
Attachments
photo1.jpg
photo1.jpg (68.85 KiB) Viewed 9930 times
Last edited by bamboo on Sun Jul 31, 2016 6:49 am, edited 1 time in total.

User avatar
Kukurykus
Posts: 377
Joined: Mon Jul 25, 2016 12:36 pm

Re: How to open the files in "photo1,photo2,photo3"

Post by Kukurykus »

Replace last linses of your code with the code part I modified.
PS.1 it won't work for subfolders in subfolders
PS.2 it'll stop if the file isn't openable in photoshop
(like Bridge sort or Windows thumbs or else error .tmp etc)

Code: Select all

//	Get all the files in the folder 
var fileList = samplesFolder.getFiles()

// open each file
for (var i = 0; i < fileList.length; i++) {
// The fileList is folders and files so open only files
if (fileList[i] instanceof File) {
open(fileList[i])
}
else if (fileList[i] instanceof Folder) {
var subFoldList = fileList[i].getFiles()
for (var j = 0; j < subFoldList.length; j++) {
if (subFoldList[j] instanceof File) {
open(subFoldList[j])
}
}
}
}

JavierAroche
Posts: 29
Joined: Sat Jul 30, 2016 3:52 am
Location: San Francisco

Re: How to open the files in "photo1,photo2,photo3"

Post by JavierAroche »

The getFiles() function only collects files from the specified directory. You will need to use a recursive function to iterate through all the subfolders and retrieve their files.

There's a really nice function in this thread where they do exactly what you need. You can even use a filter to retrieve only the files you need.
https://forums.adobe.com/thread/1246514

bamboo
Posts: 10
Joined: Fri Jul 29, 2016 7:14 pm

Re: How to open the files in "photo1,photo2,photo3"

Post by bamboo »

Thank you,JavierAroche !!

JavierAroche
Posts: 29
Joined: Sat Jul 30, 2016 3:52 am
Location: San Francisco

Re: How to open the files in "photo1,photo2,photo3"

Post by JavierAroche »

No problem!

bamboo
Posts: 10
Joined: Fri Jul 29, 2016 7:14 pm

Re: How to open the files in "photo1,photo2,photo3"

Post by bamboo »

I try run this,But cannot open the files in subfolder,why?,I read long time !!
///////////////////////////////////////////////////////////////////////////
var topFolder =Folder('~/desktop/photo');
var fileandfolderAr = scanSubFolders(topFolder,/\.(jpg|tif|psd|bmp|gif|png|)$/i);
var fileList = fileandfolderAr[0];
for(var a = 0 ;a < fileList.length; a++)
{
var docRef = open(fileList[a]);
//do things here
}
function scanSubFolders(tFolder, mask) { // folder object, RegExp or string
var sFolders = new Array();
var allFiles = new Array();
sFolders[0] = tFolder;
for (var j = 0; j < sFolders.length; j++){ // loop through folders
var procFiles = sFolders[j].getFiles();
for (var i=0;i<procFiles.length;i++){ // loop through this folder contents
if (procFiles instanceof File ){
if(mask==undefined) allFiles.push(procFiles);// if no search mask collect all files
if (procFiles.fullName.search(mask) != -1) allFiles.push(procFiles); // otherwise only those that match mask
}else if (procFiles instanceof Folder){
sFolders.push(procFiles);// store the subfolder
scanSubFolders(procFiles, mask);// search the subfolder
}
}
}
return [allFiles,sFolders];
};

JavierAroche
Posts: 29
Joined: Sat Jul 30, 2016 3:52 am
Location: San Francisco

Re: How to open the files in subfolders"photo1,photo2,photo3"

Post by JavierAroche »

I just tested this code and it works. Double check your path "~/desktop/photo". Does the ~ work as the user path on windows? Try a full absolute path to the folder.

bamboo
Posts: 10
Joined: Fri Jul 29, 2016 7:14 pm

Re: How to open the files in subfolders"photo1,photo2,photo3"

Post by bamboo »

Thank you JavierAroche!!
The code can open only one photo,other cannot be open
like the picture :2.jpg and 3.jpg and the files in subfolder photo1,photo2,photo3 cannot be open

the 1.jpg is opened

I cann't find the cause!!!
Attachments
photo2.jpg
photo2.jpg (78.6 KiB) Viewed 9883 times

User avatar
Kukurykus
Posts: 377
Joined: Mon Jul 25, 2016 12:36 pm

Re: How to open the files in subfolders"photo1,photo2,photo3"

Post by Kukurykus »

Try my solution, it's right under your first post and I tested it, so it works for sure...

bamboo
Posts: 10
Joined: Fri Jul 29, 2016 7:14 pm

Re: How to open the files in subfolders"photo1,photo2,photo3"

Post by bamboo »

Thank you Kukurykus,

All the files can be opened

thanks a lot!!