Applescript / Javascript > Adobe Photoshop scripting
CS5
Emma:
Am dipping my toes into scripting Photoshop, and have recently bought CS5. I was dismayed by the scarcity of commands in the library, until I read a tip elsewhere on the net - always open CS5 BEFORE Applescript
I tried that and full functionality returned, but it had me scratching my head for a while!
Now to work out how to save a damned file!!
larsen67:
Photoshop scripting is pretty easy in comparison to some other apps… The scripting dictionary has a problem with CS5 some one did post a fix in Adobe's Photoshop forum. I think that Adobe has now put a patch out that corrects the issue. I did post here before about this when Glenn asked about CS5. Im mostly working with ExtendScript (JavaScript) so I've not been affected by the AppleScript issues…
Publi-Script:
Hi Emma,
With Pohotoshop, I tend to go the Javascript way... To help you with that, Adobe has developed a plugin that will "record" your actions. You can find this plugin under Photoshop CS5 folder/Scripting/Utilities. Once enabled, this plugin will generate a text file on your desktop (ScriptingListenerJS.log). You might find the results daunting ad cryptic at first but it will soon make sense.
For instance, this code create a new 2 in x 2 in RGB document at 300 dpi named "New Doc":
--- Quote ---// =======================================================
var idMk = charIDToTypeID( "Mk " );
var desc1 = new ActionDescriptor();
var idNw = charIDToTypeID( "Nw " );
var desc2 = new ActionDescriptor();
var idNm = charIDToTypeID( "Nm " );
desc2.putString( idNm, "New Doc" );
var idMd = charIDToTypeID( "Md " );
var idRGBM = charIDToTypeID( "RGBM" );
desc2.putClass( idMd, idRGBM );
var idWdth = charIDToTypeID( "Wdth" );
var idRlt = charIDToTypeID( "#Rlt" );
desc2.putUnitDouble( idWdth, idRlt, 144.000000 );
var idHght = charIDToTypeID( "Hght" );
var idRlt = charIDToTypeID( "#Rlt" );
desc2.putUnitDouble( idHght, idRlt, 144.000000 );
var idRslt = charIDToTypeID( "Rslt" );
var idRsl = charIDToTypeID( "#Rsl" );
desc2.putUnitDouble( idRslt, idRsl, 300.000000 );
var idpixelScaleFactor = stringIDToTypeID( "pixelScaleFactor" );
desc2.putDouble( idpixelScaleFactor, 1.000000 );
var idFl = charIDToTypeID( "Fl " );
var idFl = charIDToTypeID( "Fl " );
var idWht = charIDToTypeID( "Wht " );
desc2.putEnumerated( idFl, idFl, idWht );
var idDpth = charIDToTypeID( "Dpth" );
desc2.putInteger( idDpth, 8 );
var idprofile = stringIDToTypeID( "profile" );
desc2.putString( idprofile, "sRGB IEC61966-2.1" );
var idDcmn = charIDToTypeID( "Dcmn" );
desc1.putObject( idNw, idDcmn, desc2 );
executeAction( idMk, desc1, DialogModes.NO );
--- End quote ---
From this, I produce the following handler:
--- Quote ---001 tell application "Adobe Photoshop CS5"
002 activate
003 do javascript (my NewPhotoshopDocument("New Doc", 144, 144, 300, "RGB"))
004 end tell
005 on NewPhotoshopDocument(DocName, DocWidth, DocHeight, DocResolution, DocColorMode)
006 -- DocName --> (String) The name of the document
007 -- DocWidth --> (Real) The width of the document in POINTS
008 -- DocHeight --> (Real) The height of the document in POINTS
009 -- DocResolution --> (Real) The resolution of the document
010 -- DocColorMode --> (String) Either "RGB" or "CMYK"
011 if DocColorMode is "RGB" then
012 set ColorMode to "RGBM"
013 set ColorProfile to "sRGB IEC61966-2.1"
014 else if DocColorMode is "CMYK" then
015 set ColorMode to "CMYM"
016 set ColorProfile to "U.S. Web Coated (SWOP) v2"
017 end if
018 set JSscript to ""
019 set JSscript to JSscript & "var idMk = charIDToTypeID( \"Mk \" );" & return
020 set JSscript to JSscript & " var desc1 = new ActionDescriptor();" & return
021 set JSscript to JSscript & " var idNw = charIDToTypeID( \"Nw \" );" & return
022 set JSscript to JSscript & " var desc2 = new ActionDescriptor();" & return
023 set JSscript to JSscript & " var idNm = charIDToTypeID( \"Nm \" );" & return
024 set JSscript to JSscript & " desc2.putString( idNm, \"" & DocName & "\" );" & return
025 set JSscript to JSscript & " var idMd = charIDToTypeID( \"Md \" );" & return
026 set JSscript to JSscript & " var idRGBM = charIDToTypeID( \"" & ColorMode & "\" );" & return
027 set JSscript to JSscript & " desc2.putClass( idMd, idRGBM );" & return
028 set JSscript to JSscript & " var idWdth = charIDToTypeID( \"Wdth\" );" & return
029 set JSscript to JSscript & " var idRlt = charIDToTypeID( \"#Rlt\" );" & return
030 set JSscript to JSscript & " desc2.putUnitDouble( idWdth, idRlt, " & DocWidth & " );" & return
031 set JSscript to JSscript & " var idHght = charIDToTypeID( \"Hght\" );" & return
032 set JSscript to JSscript & " var idRlt = charIDToTypeID( \"#Rlt\" );" & return
033 set JSscript to JSscript & " desc2.putUnitDouble( idHght, idRlt, " & DocHeight & " );" & return
034 set JSscript to JSscript & " var idRslt = charIDToTypeID( \"Rslt\" );" & return
035 set JSscript to JSscript & " var idRsl = charIDToTypeID( \"#Rsl\" );" & return
036 set JSscript to JSscript & " desc2.putUnitDouble( idRslt, idRsl, " & DocResolution & " );" & return
037 set JSscript to JSscript & " var idpixelScaleFactor = stringIDToTypeID( \"pixelScaleFactor\" );" & return
038 set JSscript to JSscript & " desc2.putDouble( idpixelScaleFactor, 1.000000 );" & return
039 set JSscript to JSscript & " var idFl = charIDToTypeID( \"Fl \" );" & return
040 set JSscript to JSscript & " var idFl = charIDToTypeID( \"Fl \" );" & return
041 set JSscript to JSscript & " var idWht = charIDToTypeID( \"Wht \" );" & return
042 set JSscript to JSscript & " desc2.putEnumerated( idFl, idFl, idWht );" & return
043 set JSscript to JSscript & " var idDpth = charIDToTypeID( \"Dpth\" );" & return
044 set JSscript to JSscript & " desc2.putInteger( idDpth, 8 );" & return
045 set JSscript to JSscript & " var idprofile = stringIDToTypeID( \"profile\" );" & return
046 set JSscript to JSscript & " desc2.putString( idprofile, \"" & ColorProfile & "\" );" & return
047 set JSscript to JSscript & " var idDcmn = charIDToTypeID( \"Dcmn\" );" & return
048 set JSscript to JSscript & " desc1.putObject( idNw, idDcmn, desc2 );" & return
049 set JSscript to JSscript & "executeAction( idMk, desc1, DialogModes.NO );"
050 return JSscript
051 end NewPhotoshopDocument
--- End quote ---
Note:
• The line numbers included with this script are there to aid future discussions. In order to use this script, you will have to strip all of them.
HTH
cdms:
The basic command for saving is fairly simple. However the useful bit is in the various save options available for each file type.
The following two code lines are examples of saving TIFFs and JPEGs. I don't have access to CS5 yet but this code has worked since CS2, so fingers crossed.
TIFF
--- Code: ---
tell document 1
save in thePath as TIFF with options {class:TIFF save options, byte order:IBM PC, embed color profile:true, image compression:none, save alpha channels:false, save layers:false, save spot colors:false, transparency:true} appending lowercase extension without copying
close
end tell
--- End code ---
JPEG
--- Code: ---
tell document 1
save in thePath as JPEG with options {class:JPEG save options, embed color profile:true, quality:12} appending no extension
close saving no
end tell
--- End code ---
You only need to specify the options you want to change from defaults. A search for 'save options' in the dictionary should get you the info you need.
Can you let us know if these work in CS5?
larsen67:
You could always build your self a set of sub-routines for each of the save options classes… This is JS but should give you the general idea.
// Save file as TIFF function
function SaveFileasTIFF(saveFile) {
tiffSaveOptions = new TiffSaveOptions();
tiffSaveOptions.alphaChannels = false;
tiffSaveOptions.byteOrder = ByteOrder.MACOS;
tiffSaveOptions.embedColorProfile = true;
tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
tiffSaveOptions.layers = false;
tiffSaveOptions.spotColors = false;
tiffSaveOptions.transparency = false;
activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE);
}
// Save file as EPS function
function SaveFileasEPS(saveFile) {
epsSaveOptions = new EPSSaveOptions();
epsSaveOptions.embedColorProfile = true;
epsSaveOptions.encoding = SaveEncoding.BINARY;
epsSaveOptions.preview = Preview.EIGHTBITTIFF;
activeDocument.saveAs(saveFile,epsSaveOptions, true,Extension.LOWERCASE);
}
// Save file as JPEG function
function SaveFileasJPEG(saveFile) {
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 12;
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
}
// Save file as PSD function
function SaveFileasPSD(saveFile) {
psdSaveOptions = new PhotoshopSaveOptions();
psdSaveOptions.alphaChannels = true;
psdSaveOptions.annotations = true;
psdSaveOptions.embedColorProfile = true;
psdSaveOptions.layers = true;
psdSaveOptions.spotColors = true;
activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);
}
// Save file as PNG function
function SaveFileasPNG(saveFile) {
pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.embedColorProfile = true;
pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
pngSaveOptions.matte = MatteType.NONE;
pngSaveOptions.quality = 1;
pngSaveOptions.PNG8 = true;
pngSaveOptions.transparency = true;
activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
}
Most important is knowing what is supported in each of the file formats and checking the document state before calling the save…
Navigation
[0] Message Index
[#] Next page
Go to full version