Home Coding, Scripting, Shaders

(Photoshop) Save action scripting

hero character
Offline / Send Message
thomasp hero character
Hi,

I'm looking for a way to overwrite a specific file on my filesystem, saving as a copy when necessary even if the file is open and the active document - but without any user intervention. This is something that used to be easy but no longer seems possible with an action (something they changed in newer versions of CC?).

What I have right now is just taken from their reference (Apple script):

tell application "Adobe Photoshop CC 2019"<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; set myDocument to current document<br>&nbsp;&nbsp;&nbsp; set myFile to "System:Users:thomaspecht:2D:bitmaps:tmp.bmp" as string<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; set myOptions to ¬<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {class:BMP save options, bits per sample:twenty four, save alpha channels:false, target operating system:Windows}<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; save myDocument in file myFile as BMP with options ¬<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; myOptions appending no extension without copying<br>&nbsp;&nbsp;&nbsp; <br>end tell

When I run this it gives me a file dialog, tells me that I need to save with copy, then I have to click away the file overwrite confirmation and finally it presents me with the file options which I thought would be taken care of with the 'set myOptions to' paragraph. Worst of all worlds combined, yeah! :)

Is it possible to condense this into a simple no-questions-asked affair?

Edit: can't seem to find anything in the scripting reference for that 'without copying' part. No difference either way when run though.

Replies

  • poopipe
    Offline / Send Message
    poopipe grand marshal polycounter
    Could it be the same issue as  when you try to do this manually and it gets pissy if you have unmerged layers? 
  • thomasp
    Offline / Send Message
    thomasp hero character
    Well it's the same if you do it manually, correct - if the file isn't in a BMP compatible format then you have to deal with dialog boxes. My old action did force its way through without nagging me though.

    Anyway I found out that Apple Script isn't going to work for what I require, which is to trigger this function through a hotkey. To achieve that, I had to rewrite it in Javascript and it appears to work as intended now:
    <div>#target photoshop<br><br>var myBMP = File("/Users/thomaspecht/2D/bitmaps/tmp.bmp"); <br><br>function bmpExport(saveFile) {<br>&nbsp;&nbsp;&nbsp; var bmpOptions = new BMPSaveOptions();<br>&nbsp;&nbsp;&nbsp; bmpOptions.alphaChannels = false;<br>&nbsp;&nbsp;&nbsp; bmpOptions.flipRowOrder = false;<br>&nbsp;&nbsp;&nbsp; bmpOptions.rleCompression = false;<br>&nbsp;&nbsp;&nbsp; bmpOptions.osType = OperatingSystem.WINDOWS;<br>&nbsp;&nbsp;&nbsp; activeDocument.saveAs(saveFile, bmpOptions, true, Extension.LOWERCASE);<br>}</div><div><br>bmpExport(myBMP);</div>

    Now I'm looking at getting a check in there to retrieve the active document's bit depth and save it out as BMP if it's in 8 bits per channel. However if found to be in 16 bits I'd like to convert that to 8 bits and then overwrite my bmp file (without affecting the active document).

    Something like: if bit depth equals 16 then write out TIFF file, load it back in again, convert down to 8 bits and finally save out as BMP using the existing function. TIFF export might look like this (untested so far):
    <div>function tiffExport(saveFile) {<br>&nbsp;&nbsp;&nbsp; var tiffOptions = new TIFFSaveOptions();<br>&nbsp;&nbsp;&nbsp; tiffOptions.alphaChannels = false;<br>&nbsp;&nbsp;&nbsp; tiffOptions.byteOrder = ByteOrder.IBM;<br>&nbsp;&nbsp;&nbsp; tiffOptions.imageCompression = TIFFEncoding.NONE;<br>&nbsp;&nbsp;&nbsp; tiffOptions.layers = false;<br>&nbsp;&nbsp;&nbsp; tiffOptions.transparency = false;<br>&nbsp;&nbsp;&nbsp; app.activeDocument.saveAs(saveFile, tiffOptions, true, Extension.LOWERCASE);<br>}&nbsp;&nbsp;&nbsp; <br></div><div></div>

    Any ideas how to test for document bit depth and convert it?

  • kio
    Offline / Send Message
    kio polycounter lvl 16
    a photoshop document has a bitsPerChannel value which you could test.

    not sure what happens if you just set it with something like this:
    doc.bitsPerChannel = BitsPerChannelType.EIGHT;

    but you need to restore the history state after that so your document doenst get changed.


  • thomasp
    Offline / Send Message
    thomasp hero character
    With Easter out of the way I'm finally back at this. I think creating a flattened copy of the document and simply converting that to 8 bits no matter what will be the approach going forward. Wouldn't want to screw with the layered work-file and all.

Sign In or Register to comment.