Photoshop Javascript
Jump to navigation
Jump to search
This is not updated frequently! I rarely use photoshop and even less script it.
If you want an old overview here's a tutorial I wrote a thousand years ago on scripting in Photoshop: https://www.kirupa.com/motiongraphics/ps_scripting.htm http://mlkdesign.online.fr/tutorials/scripting/scripting1.html
Run Actions according to folder names (wip)
Hadn't coded PS for a while goddam it's ugly. Some code stolen from the web, can't remember who to credit, sorry.
alert(getAllActions()); var folder = Folder.selectDialog("Folder To Process") loop = 1 if(folder){ while(loop == 1){ files = folder.getFiles(); for(item in files){ if (files[item] instanceof File ){ if(files[item].name.indexOf("kill") != -1){ loop = 0; } }else{ //is folder subfolder = new Folder(files[item]) folderName = files[item].name subfiles = subfolder.getFiles(); if(subfiles.length > 0){ for(item in subfiles){ if (files[item] instanceof File ){ } } alert(folderName+' '+subfiles) } //app.doAction( 'bernie' ,'action'); //app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); } } } } /* try { desc = executeActionGet(ref); } catch (e) { break; } finally { $.level = lvl; } */ ////////////////////// FUNCS ////////////// function getActionSets() { cTID = function(s) { return app.charIDToTypeID(s); }; sTID = function(s) { return app.stringIDToTypeID(s); }; var i = 1; var sets = []; while (true) { var ref = new ActionReference(); ref.putIndex(cTID("ASet"), i); var desc; var lvl = $.level; $.level = 0; try { desc = executeActionGet(ref); } catch (e) { break; } finally { $.level = lvl; } if (desc.hasKey(cTID("Nm "))) { var set = {}; set.index = i; set.name = desc.getString(cTID("Nm ")); set.toString = function() { return this.name; }; set.count = desc.getInteger(cTID("NmbC")); set.actions = []; for (var j = 1; j <= set.count; j++) { var ref = new ActionReference(); ref.putIndex(cTID('Actn'), j); ref.putIndex(cTID('ASet'), set.index); var adesc = executeActionGet(ref); var actName = adesc.getString(cTID('Nm ')); set.actions.push(actName); } sets.push(set); } i++; } return sets; }; function getActions(aset) { cTID = function(s) { return app.charIDToTypeID(s); }; sTID = function(s) { return app.stringIDToTypeID(s); }; var i = 1; var names = []; if (!aset) { throw "Action set must be specified"; } while (true) { var ref = new ActionReference(); ref.putIndex(cTID("ASet"), i); var desc; try { desc = executeActionGet(ref); } catch (e) { break; } if (desc.hasKey(cTID("Nm "))) { var name = desc.getString(cTID("Nm ")); if (name == aset) { var count = desc.getInteger(cTID("NmbC")); var names = []; for (var j = 1; j <= count; j++) { var ref = new ActionReference(); ref.putIndex(cTID('Actn'), j); ref.putIndex(cTID('ASet'), i); var adesc = executeActionGet(ref); var actName = adesc.getString(cTID('Nm ')); names.push(actName); } break; } } i++; } return names; }; function getAllActions() { var allActions = []; actionSets = getActionSets(); for(set in actionSets){ actions = getActions(actionSets[set]); for(action in actions){ allActions.push([actionSets[set],actions[action]]); } } return allActions; } function saveJPG(doc,filepath){ saveFile = new File("//PSTemp/"+app.activeDocument.name+".jpg"); saveOptions = new JPEGSaveOptions(); saveOptions.embedColorProfile = true; saveOptions.formatOptions = FormatOptions.STANDARDBASELINE; saveOptions.matte = MatteType.NONE; saveOptions.quality = 12; //ranges from 0 to 12 doc.saveAs(filepath, saveOptions, true,Extension.LOWERCASE); }
Launch an action using the name of the current document
Save as .jsx and launch from File>Script in photoshop
/* An example of how you can use the name of a document to launch an action that shares the same name try it on an image called 'Gradient Map.jpg' -- since 'Gradient Map is a default action in the 'Default Actions' set it should work on a brand new photoshop */ //this gets the name of the current open (foremost document) currentImage = app.activeDocument.name ; //this removes the extension currentImageName = currentImage.substring(0, currentImage.lastIndexOf('.')) || currentImage ; //this calls an action with the name of the current image from the 'Default Actions' set (which you can of course change) app.doAction( currentImageName ,'Default Actions');
All documents to current document
docs = app.documents; ad= app.activeDocument; for(i = 0;i<docs.length;i++){ if(docs[i]!=ad){ activeDocument = docs[i]; docs[i].selectAll; docs[i].layers[0].copy(); activeDocument = ad; ad.paste(); } } while(docs.length>1){ //hackish but works for(i = 0;i<docs.length;i++){ if(docs[i]!=ad){ docs[i].close(SaveOptions.DONOTSAVECHANGES); } } }
Incremental Save
// incrementalsave // // checks if there is a double underscore in filename, if not adds them starting at 0001 // else increments and saves as 24bit png (filename__####.png) in the doc's folder // // // feel free to use & modify - mbernadat@gmail.com // // // only tested on vista+cs3 function remEx(f) { extPeriod = f.lastIndexOf("."); if(extPeriod > 0){ return f.substr(0,extPeriod); }else{ return false; } } function getIncrPos(nameWithoutExt){ pos = nameWithoutExt.lastIndexOf("__"); return nameWithoutExt.substr(pos+2,nameWithoutExt.length); } function basenameKindOf(path,match) { a = path.lastIndexOf(match); if(a > 0){; return path.substr(a,path.length); }else{ return false; } } function addZeros(number){ if(number<10){ number = "000"+number; }else if(number<100){ number = "00"+number; }else if(number<1000){ number = "0"+number; } return number; } docP = app.activeDocument.path; docN = remEx(app.activeDocument.name); docFold = new Folder (app.activeDocument.path); files = docFold.getFiles(docN+"__*.png"); files = Folder.decode(files); if(files.length == 0){ numbering = 0; }else{ files = files.split(","); numbering = 0; for(var i in files){ curNumb = getIncrPos(remEx(basenameKindOf(files[i],docN))); numbering = Math.max(numbering,curNumb); //alert("'"+remEx(files[i])+"'"); } } numbering++; numbering = addZeros(numbering); var exportOptions = new PNGSaveOptions(); //var type = ExportType.PNG24; var fileSpec = new File(docP+"/"+docN+"__"+numbering+".png"); exportOptions.antiAliasing = true; exportOptions.transparency = true; exportOptions.saveAsHTML = false; app.activeDocument.saveAs(fileSpec,exportOptions,1,Extension.LOWERCASE);