Author Topic: CS5  (Read 4286 times)

0 Members and 1 Guest are viewing this topic.

Offline Emma

  • Full Member
  • ***
  • Posts: 205
  • Karma: 9
CS5
« on: July 11, 2010, 04:06:12 AM »
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!!
------------
Quark 6.5
OS X 10.4.8
G4

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: CS5
« Reply #1 on: July 11, 2010, 05:24:54 AM »
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…

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: CS5
« Reply #2 on: July 11, 2010, 10:58:27 AM »
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 );

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
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
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline cdms

  • Full Member
  • ***
  • Posts: 111
  • Karma: 6
  • Gender: Male
Re: CS5
« Reply #3 on: July 12, 2010, 12:17:57 AM »
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: [Select]
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


JPEG
Code: [Select]
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

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?

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: CS5
« Reply #4 on: July 12, 2010, 04:01:05 AM »
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…

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: CS5
« Reply #5 on: July 12, 2010, 04:23:20 AM »
Similar to Michel's sub…

#target photoshop

function newDoc(docN, docM, docW, docH, docRes) {
   function cTID(s) { return app.charIDToTypeID(s); };
   function sTID(s) { return app.stringIDToTypeID(s); };
      var desc1 = new ActionDescriptor();
      var desc2 = new ActionDescriptor();
      desc2.putString( cTID('Nm  '), docN );
      desc2.putClass( cTID('Md  '), cTID( docM ) );
      desc2.putUnitDouble( cTID('Wdth'), cTID('#Rlt'), docW );
      desc2.putUnitDouble( cTID('Hght'), cTID('#Rlt'), docH );
      desc2.putUnitDouble( cTID('Rslt'), cTID('#Rsl'), docRes );
      desc2.putDouble( sTID('pixelScaleFactor'), 1.000000 );
      desc2.putEnumerated( cTID('Fl  '), cTID('Fl  '), cTID('BckC') );
      desc2.putInteger( cTID('Dpth'), 8 );
      desc2.putString( sTID('profile'), "Europe ISO Coated FOGRA27" );
      desc1.putObject( cTID('Nw  '), cTID('Dcmn'), desc2 );
   executeAction( cTID('Mk  '), desc1, DialogModes.NO );
}

newDoc('Testing', 'CMYM', 144, 144, 300);

Offline Emma

  • Full Member
  • ***
  • Posts: 205
  • Karma: 9
Re: CS5
« Reply #6 on: August 05, 2010, 12:49:49 AM »
Sorry, guys, I missed this helpful torrent of JS! Found a snippet next day at work that does the job fine.
Javascript is one of those things I guess I'll learn when I have to. So far I've managed without it. What are the advantages? Does Filemaker support it? Is it cross-platform?
------------
Quark 6.5
OS X 10.4.8
G4

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: CS5
« Reply #7 on: August 05, 2010, 02:14:42 AM »
Several of the Adobe Creative Suite apps can be scripted in 3 languages. 2 system dependent in Applescript for the Mac & Visual Basic for the PC. Adobe's ESTK is ExtendScript their own extension to JavaScript. This has the advantage of being platform independent. I doubt that you will find direct support for this from FMP but it could still be called using an AppleScript wrapper. Tell application "Photoshop CS5" to do javascript SomeFile as alias blah blah… or you could escape the syntax and place it as a variable in your AppleScript. I find that in a lot of cases JS performs a far bit quicker too.