(Photoshop) Save action scripting

sublime tool
Offline / Send Message
thomasp sublime tool

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"
    set myDocument to current document
    set myFile to "System:Users:thomaspecht:2D:bitmaps:tmp.bmp" as string
    set myOptions to ¬
        {class:BMP save options, bits per sample:twenty four, save alpha channels:false, target operating system:Windows}
    save myDocument in file myFile as BMP with options ¬
        myOptions appending no extension without copying
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.


  • poopipe
    Offline / Send Message
    poopipe ngon master
    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 sublime tool
    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:
    #target photoshop

    var myBMP = File("/Users/thomaspecht/2D/bitmaps/tmp.bmp");

    function bmpExport(saveFile) {
        var bmpOptions = new BMPSaveOptions();
        bmpOptions.alphaChannels = false;
        bmpOptions.flipRowOrder = false;
        bmpOptions.rleCompression = false;
        bmpOptions.osType = OperatingSystem.WINDOWS;
        activeDocument.saveAs(saveFile, bmpOptions, true, Extension.LOWERCASE);

    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):
    function tiffExport(saveFile) {
        var tiffOptions = new TIFFSaveOptions();
        tiffOptions.alphaChannels = false;
        tiffOptions.byteOrder = ByteOrder.IBM;
        tiffOptions.imageCompression = TIFFEncoding.NONE;
        tiffOptions.layers = false;
        tiffOptions.transparency = false;
        app.activeDocument.saveAs(saveFile, tiffOptions, true, Extension.LOWERCASE);

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

  • kio
    Offline / Send Message
    kio polycounter lvl 11
    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 sublime tool
    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.