Copy link to clipboard
Copied
I want to check if there are any jpg's in folder, before curFolders.push(curhit);
function recursivehit(sourceFolder) {
var mask = /\.(?:jpg)$/i;
var curFolders = new Array();
var curFiles = new Array();
var curhit = sourceFolder.getFiles();
var curPath = new Array();
for (var i = 0; i < curhit.length; i++)
{
if (curhit instanceof File) //if file
{
if (curhit.fullName.search(mask) != -1)
{
curFiles.push(curhit);
}
}
else if (curhit instanceof Folder)//if folder
{
curFolders.push(curhit);
}
}
Can anyone help? ^^
Copy link to clipboard
Copied
The getFiles method accepts a regular expression as an argument. If you use it all you need to check the the returned array's length to make sure there were jpg file in the folder.
var curhit = sourceFolder.getFiles(/.jpg$/i);
Copy link to clipboard
Copied
yes that works alone but I have realy bad written code because I don't know any better
when I write
var curhit = sourceFolder.getFiles(/.jpg$/i);
photoshop gives this error:
"-The command "Delete" is not currently available: on line 83" error.
on line 84 I have : newLayer.remove();
I have to have that line otherwise it will create psd with an empty layer left in it, also
var curhit = sourceFolder.getFiles(/.jpg$/i);
doesnt work because I use it to get folders too =/
#target photoshop
app.bringToFront();
//
function main() {
// user settings
var prefs = new Object();
prefs.sourceFolder = '~';
prefs.removeFileExtensions = true;
prefs.savePrompt = false;
prefs.closeAfterSave = false; )
var sourceFolder = Folder.selectDialog('Please select the folder to be imported:', Folder(prefs.sourceFolder));
prefs.sourceFolder = sourceFolder;
recursivehit(sourceFolder);
}
//
function recursivehit(sourceFolder) { // folder object, RegExp or string
var mask = /\.(?:jpg)$/i;
var curFolders = new Array();
var curFiles = new Array();
var curhit = sourceFolder.getFiles();
for (var i = 0; i < curhit.length; i++)
{
if (curhit instanceof File) //if file
{
if (curhit.fullName.search(mask) != -1)
{
curFiles.push(curhit);
}
}
else if (curhit instanceof Folder)//if folder
{
curFolders.push(curhit);
}
}
var nn = new Object();
nn.f = "'" + sourceFolder;
cursourcefoldername = nn.f.match(/([^\/]*)\/*$/)[1]
var newDoc = documents.add(300, 308, 72, cursourcefoldername, NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1);
var newLayer = newDoc.activeLayer;
for (var i = 0; i < curFiles.length; i++)
{
// open document
var doc = open(curFiles);
// get document name (and remove file extension)
var name = doc.name;
name = name.replace(/(?:\.[^.]*$|$)/, '');
doc.changeMode(ChangeMode.RGB);
doc.bitsPerChannel = BitsPerChannelType.EIGHT;
doc.artLayers.add();
doc.mergeVisibleLayers();
// rename layer; duplicate to new document
var layer = doc.activeLayer;
layer.name = name;
layer.duplicate(newDoc, ElementPlacement.PLACEATBEGINNING);
// close imported document
doc.close(SaveOptions.DONOTSAVECHANGES);
}
newLayer.remove();
newDoc.revealAll();
var saveOptions = new PhotoshopSaveOptions();
saveOptions.layers = true;
saveOptions.embedColorProfile = true;
var AD = app.activeDocument;
var parentFolder = decodeURI(sourceFolder.parent);
var savePath = new File( parentFolder );
newDoc.saveAs(savePath, saveOptions, false, Extension.LOWERCASE);
AD.close(SaveOptions.DONOTSAVECHANGES);
for (var i = 0; i < curFolders.length; i++)
{
recursivehit(curFolders); //call function recursively
}
};
Copy link to clipboard
Copied
I missed that you want to process subfolders. So you can either call getFiles without an argument or write a custom function that will only return folders and jpeg files. I don't know why you are getting that strange error message. In the second code you posted there is a mis-placed ) but that gives a dffferent erro
So now I am not sure what you mean by 'I want to check if there are any jpg's in folder'. Do you mean you only want to push folders that also contain jpeg files?
Copy link to clipboard
Copied
It gives that strange error because var doc = open(curFiles); doesnt realy work, it pushes folders and creates empty psd files of empty folders + sourceFolder psd eventhough it doesnt have images (only subfolders)
I don't know how I will call getFiles without an argument. That might do the trick ^^
Copy link to clipboard
Copied
I don't see how curFiles could have anything but jepg files. You are only adding to the array after checking that the object is a File and the name ends with .jpg.
Even if curFiles was a folder object you should get a different error message.
When I try to run this I get an error at newLayer.remove(); I think that is because the mergeVisibleLayers() call make newLayer an invalid object. i.e. that layer no longer exists in the document, it was merged.
Copy link to clipboard
Copied
I think I have that error when it tries to make psd files of empty folders
use this https://dl.dropboxusercontent.com/u/33437495/0upafoldertest.rar (you must change jpg to tif in the code ofc)as a way to try different types of subfolders..
it goes through some, than stops...
if you select the folder 50/ it just gives an error
if you select the folder 50/03 works fine for it and subfolders of it
if you select the folder 50/47 works fine for it
if you select the folder 50/665/ it just gives an error
if you select the folder 50/dd/ it just gives an error
I dont want an image to be necessary for it to run... when you disable newlayer.remove it works fine with empty psds created for folders without images in it..
Copy link to clipboard
Copied
I don't think this fixes everything but it seems to process all the files without error
if(curFiles.length>0){// only needs to run if there were image files to process
newLayer.remove();// can't remove this layer unless other layers were added in the loop above
newDoc.revealAll();// no need to revealAl if no layers were added
}
I think the problem was when there are no image files to process it throws the error because it is tries to remove the only layer in the doc.
Copy link to clipboard
Copied
Great!
I managed to do with:
if (activeDocument.activeLayer.parent.layers.length == 1)
{
AD.close(SaveOptions.DONOTSAVECHANGES);
}
I have to use "length" in a smart way.. still thinking if any other way.
Copy link to clipboard
Copied
This one works sorry about not copying the version check etc
#target photoshop
// bring application forward for double-click events
app.bringToFront();
///////////////////////////////////////////////////////////////////////////////
// main - main function
///////////////////////////////////////////////////////////////////////////////
function main() {
var prefs = new Object();
prefs.sourceFolder = '~';
prefs.removeFileExtensions = true;
prefs.savePrompt = false;
prefs.closeAfterSave = false;
var sourceFolder = Folder.selectDialog('Please select the folder to be imported:', Folder(prefs.sourceFolder));
prefs.sourceFolder = sourceFolder;
recursivehit(sourceFolder);
}
//////////////////////////////////////////////////////////////////////////
function recursivehit(sourceFolder) { // folder object, RegExp or string
var mask = /\.(?:jpg)$/i;
var curFolders = new Array();
var curFiles = new Array();
var curhit = sourceFolder.getFiles();
for (var i = 0; i < curhit.length; i++)
{
if (curhit instanceof File) //if file
{
if (curhit.fullName.search(mask) != -1)
{
curFiles.push(curhit);
}
}
else if (curhit instanceof Folder)//if folder
{
curFolders.push(curhit);
}
}
var nn = new Object();
nn.f = "'" + sourceFolder;
cursourcefoldername = nn.f.match(/([^\/]*)\/*$/)[1]
var newDoc = documents.add(300, 300, 72, cursourcefoldername, NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1);
var newLayer = newDoc.activeLayer;
for (var i = 0; i < curFiles.length; i++)
{
// open document
var doc = open(curFiles);
// get document name (and remove file extension)
var name = doc.name;
name = name.replace(/(?:\.[^.]*$|$)/, '');
doc.changeMode(ChangeMode.RGB);
doc.bitsPerChannel = BitsPerChannelType.EIGHT;
doc.artLayers.add();
doc.mergeVisibleLayers();
// rename layer; duplicate to new document
var layer = doc.activeLayer;
layer.name = name;
layer.duplicate(newDoc, ElementPlacement.PLACEATBEGINNING);
// close imported document
doc.close(SaveOptions.DONOTSAVECHANGES);
}
newLayer.remove();
newDoc.revealAll();
var saveOptions = new PhotoshopSaveOptions();
saveOptions.layers = true;
saveOptions.embedColorProfile = true;
var AD = app.activeDocument;
var parentFolder = decodeURI(sourceFolder.parent);
var savePath = new File( parentFolder );
newDoc.saveAs(savePath, saveOptions, false, Extension.LOWERCASE);
AD.close(SaveOptions.DONOTSAVECHANGES);
for (var i = 0; i < curFolders.length; i++)
{
recursivehit(curFolders); //call function recursively
}
};
///////////////////////////////////////////////////////////////////////////////
// isCorrectVersion - check for Adobe Photoshop CS2 (v9) or higher
///////////////////////////////////////////////////////////////////////////////
function isCorrectVersion() {
if (parseInt(version, 10) >= 9) {
return true;
}
else {
alert('This script requires Adobe Photoshop CS2 or higher.', 'Wrong Version', false);
return false;
}
}
///////////////////////////////////////////////////////////////////////////////
// showError - display error message if something goes wrong
///////////////////////////////////////////////////////////////////////////////
function showError(err) {
if (confirm('An unknown error has occurred.\n' +
'Would you like to see more information?', true, 'Unknown Error')) {
alert(err + ': on line ' + err.line, 'Script Error', true);
}
}
// test initial conditions prior to running main function
if (isCorrectVersion()) {
// remember ruler units; switch to pixels
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
try {
main();
}
catch(e) {
// don't report error on user cancel
if (e.number != 8007) {
showError(e);
}
}
// restore original ruler unit
preferences.rulerUnits = originalRulerUnits;
}