Exporting for the web, recreating folder structure

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: Tom, Kukurykus

karm

Exporting for the web, recreating folder structure

Post by karm »

Hello everybody !

I've a scripty question to ask you.

I retouch lot of images coming from different subfolders with special references. Usually i rename every photo with a renamer but sometimes i forgot.

I'd like to find a way in my script to export for the web in another folder with the folder structure automaticly recreated. Is there a possibility to do that inside a script?

Thanks a lot !

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Exporting for the web, recreating folder structure

Post by Mike Hale »

It's possible to script save for web. And with ExtendScript you can create files/folders.

I would need more details about the folders and save format before I could post any code snippets.
karm

Exporting for the web, recreating folder structure

Post by karm »

Thanks Mike for your reply !

Example of what i need.

I've a folder containing subfolders containing source images:
S:\to do\folderA
example of subfolders: S:\to do\folderA\subfolder36 , S:\to do\folderA\subfolder75 ...

no problem to charge this in the script, the problem is to export and to recreate the folders. I'd like the script to save the post processed images in:

S:\done\folderA with the images in the subfolders like this:
S:\done\folderA\subfolder36 , S:\done\folderA\subfolder75 ...

I export the image for the web in jpeg quality 60%.

My problem is that for the moment, i know only how to save all these images in S:\done\folderA\ , no subfolders

Thanks for your time and help
Mike Hale

Exporting for the web, recreating folder structure

Post by Mike Hale »

There are several ways to do this. Here is one example.
Code: Select allfunction createFolder( folderObj ){
    if( !folderObj.parent.exists ) createFolder( folderObj.parent );
    if( !folderObj.exists ) folderObj.create();
 };
function exportSFW( doc, saveFile, qty ) {
   var exportOpts = new ExportOptionsSaveForWeb( );
   exportOpts.format = SaveDocumentType.JPEG
   exportOpts.includeProfile = true;//default false
   exportOpts.quality = qty;
   if ( saveFile.exists ) saveFile.remove( );
   doc.exportDocument( saveFile, ExportType.SAVEFORWEB, exportOpts );
};
// make a new folder object from the doc path and change the structure from to-do to done. Note the folder being replace should not the doc path.parent
// S:\to do\folderA\subfolder36 to S:\done\folderA\subfolder36 not S:\to do\folderA\subfolder36 to S:\to do\folderA\subfolder37
var saveFolder = new Folder( decodeURI( app.activeDocument.path ).replace( '/to-do/', '/done/' ) );
createFolder( saveFolder );
var saveFile = new File( saveFolder + '/' + app.activeDocument.name );// assumes doc was a jpeg when opened if not change extension
// do your save for web step here using saveFile
exportSFW( app.activeDocument, saveFile, 60 );
karm

Exporting for the web, recreating folder structure

Post by karm »

Wonderful ! It works perfectly !!!

Just 3 more things:

Is there a possibility to have the parent folder renamed? Like this: "S:\to do\folderA\" to "S:\done\folderA-retouched\" ?

Is it possible to get rid of a subfolder before the last subfolder? Example: input: "S:\to do\folderA\xxxx\subfolder36", output folder: "S:\done\folderA\subfolder36"

And if there is no subfolder like "S:\to do\folderA\" without any subfolder 36 etc, could it be possible to put the retouches directly in "S:\done\folderA"?

Thanks
Mike Hale

Exporting for the web, recreating folder structure

Post by Mike Hale »

Once you have created a string from the folder path with decodeURI( app.activeDocument.path ) you can use any of the string methods to change the path. The last folder in the path does not have the forward slash so to add '-retouched' you could do something like
Code: Select allstrPath = strPath+'-retouched'
To remove a middle folder you could do something like
Code: Select allstrPath = strPath.replace( '/xxxx/', '/' )
So you should get the idea, change the folder string to what ever is needed keeping in mind that it needs to be in the URI format. ie it starts with a forward slash and doesn't end with one. Then all you need to do after you have changed the string is turn it back into a folder object with New Folder( strPath ) and use a function like createFolder above to make sure the folder chain exists before saving.
karm

Exporting for the web, recreating folder structure

Post by karm »

Thanks for your explanations !

I tried to reproduce what you explained but i don't know anything about coding, and i got each time some errors
Here is what i did:

Code: Select all    function createFolder( folderObj ){
        if( !folderObj.parent.exists ) createFolder( folderObj.parent );
        if( !folderObj.exists ) folderObj.create();
    };
    function exportSFW( doc, saveFile, qty ) {
       var exportOpts = new ExportOptionsSaveForWeb( );
       exportOpts.format = SaveDocumentType.JPEG
       exportOpts.includeProfile = true;//default false
       exportOpts.quality = qty;
       if ( saveFile.exists ) saveFile.remove( );
       doc.exportDocument( saveFile, ExportType.SAVEFORWEB, exportOpts );
    };
    // make a new folder object from the doc path and change the structure from to-do to done. Note the folder being replace should not the doc path.parent
    // S:\to do\folderA\subfolder36 to S:\done\folderA\subfolder36 not S:\to do\folderA\subfolder36 to S:\to do\folderA\subfolder37
    var saveFolder = new Folder( decodeURI( app.activeDocument.path ).replace( '/to-do/', '/done/' ) );
    strPath = strPath+'-retouched';
    createFolder( saveFolder );
    var saveFile = new File( saveFolder + '/' + app.activeDocument.name );// assumes doc was a jpeg when opened if not change extension
    // do your save for web step here using saveFile
    exportSFW( app.activeDocument, saveFile, 60 );


The middle folder that i want to remove have not a predifined name like xxxx, it could be yyyy.

If there is no subfolder in "S:\to do\folderA\" but a lot of files in it (curently it doesn't work with the script, it says the folder doesn't exist), could it be possible to put the retouches directly in "S:\done\folderA"?
Mike Hale

Exporting for the web, recreating folder structure

Post by Mike Hale »

Sorry, I combined several steps into one line in my example. Here an expanded version.

Code: Select all var strPath = decodeURI( app.activeDocument.path );
strPath = strPath.replace( '/to-do/', '/done/' );
strPath = strPath+'-retouched';
var saveFolder = new Folder( strPath )

To remove a middle folder where the folder name can change you can use a RegExp to make the change as long as there is a pattern to match. For example to remove /xxxx/ in the path S:\to do\folderA\xxxx\subfolder36" where x could be any char do this
Code: Select allstrPath = strPath.replace(/\/.{4}\//, '/' );// will remove the first four char folder in the path

Take care when using reg exp to change strings. For example if 'to do' in the above example path was 'todo' that folder would have been removed instead of 'xxxx'
karm

Exporting for the web, recreating folder structure

Post by karm »

Thanks for your explanations it works best

But now the "-retouched" appears here when there is no subfolder: "S:\done\folderA-retouched\"
and here when they are some subfolders: "S:\done\folderA\subfolder36-retouched\"
Is it possible to make it only for the folderA name? (S:\done\folderA-retouched\) and not the subfolders?


The script is not working when the path name contain some characters like "é". Can we do something to permit these characters?

Thanks again for all your time and knowledge, this script will be such a gain of time and organization for me
karm

Exporting for the web, recreating folder structure

Post by karm »

Hello again,

I use the script almost every day since two years but now that i've got cs6 and that i try to look to work more with bridge, i realized that when i launch the script from bridge, it simply launch the window export for web instead of doing a silent export. I've only got this problem from bridge, from ps everything works perfectly.

Do you know if it's possible to change that?

Here is the final code i use:
Code: Select allfunction createFolder( folderObj ){
    if( !folderObj.parent.exists ) createFolder( folderObj.parent );
    if( !folderObj.exists ) folderObj.create();
};
function exportSFW( doc, saveFile, qty ) {
   var exportOpts = new ExportOptionsSaveForWeb( );
   exportOpts.format = SaveDocumentType.JPEG
   exportOpts.includeProfile = true;//default false
   exportOpts.quality = qty;
   if ( saveFile.exists ) saveFile.remove( );
   doc.exportDocument( saveFile, ExportType.SAVEFORWEB, exportOpts );
};
// make a new folder object from the doc path and change the structure from to-do to done. Note the folder being replace should not the doc path.parent
// S:\to do\folderA\subfolder36 to S:\done\folderA\subfolder36 not S:\to do\folderA\subfolder36 to S:\to do\folderA\subfolder37
var strPath = decodeURI( app.activeDocument.path );
strPath = strPath.replace( 'work', 'ret');
//strPath = strPath+'-retouched';
//strPath = strPath.replace(/\/.{3}\//, '/' );// will remove the first three char folder in the path
var saveFolder = new Folder( strPath )
createFolder( saveFolder );
var saveFile = new File( saveFolder + '/' + app.activeDocument.name );// assumes doc was a jpeg when opened if not change extension
// do your save for web step here using saveFile
exportSFW( app.activeDocument, saveFile, 80 );