Photoshop "save for web" JavaScript
This is a JavaScript function for Photoshop which saves the active document
as a 24-bit PNG file. It is equivalent to manually selecting File > Save for
Web & Devices… which means that the file size of the resulting PNG will be
smaller than would be the case using PNGSaveOptions().
function saveForWebPNG(outputFolderStr, filename) { var opts, file; opts = new ExportOptionsSaveForWeb(); opts.format = SaveDocumentType.PNG; opts.PNG8 = false; opts.quality = 100; if (filename.length > 27) { file = new File(outputFolderStr + "/temp.png"); activeDocument.exportDocument(file, ExportType.SAVEFORWEB, opts); file.rename(filename + ".png"); } else { file = new File(outputFolderStr + "/" + filename + ".png"); activeDocument.exportDocument(file, ExportType.SAVEFORWEB, opts); } }
Photoshop on Mac limits the length of a File object's file name to
31 characters. Credit for the rename workaround should go to Mark Walsh
who posted the solution on the Adobe forums in a thread titled
Save for web filename problems.
Possibly related posts
- DigitalColor Meter
- AppleScript syntax highlighting
- Show full directory path in Finder window title bar
- Duplicating arrays in JavaScript
- Multisession CD burning in Snow Leopard
Comments
Awesome post. Followed through from the Adobe help forums. This was a life saver. Very frustrating that there is a relatively large lacking in the API for Photoshop Scripting. Thanks again.
I'm pleased that this post was helpful, Mike. I agree that Photoshop's JavaScript API has some holes in it.
Just thought I'd chime in and point out this isn't just a Javascript limitation. This post helped me solve this same problem I was having with Applescript and Photoshop CS4. The file name length restriction may be left in for compatibility with HFS or a holdover from pre-OSX days — an oversight, who knows?
export current document in file ((source_folder as string) & "temp.jpg") as save for web with options mySaveOptions
tell application "Finder" to set the name of file ((source_folder as string) & "temp.jpg") to (theFileName & ".jpg")
Thanks for this info - I ported some PhotoShop JS code from Windows to Mac and was going crazy trying to figure out why my filenames were being truncated.