0

I have a droplet app created in AppleScript (code below) that will accept various image formats (either dropped onto it or via a dialog box) and spit out PNGs resized to a user-defined max pixel dimension. It is lightning fast compared to Photoshop batch processing.

Looking for some help with the (3) limitations outlined below. (My preference is to do this via scripting/commands native to MacOS; in other words, without having to install ImageMagick.)


LIMITATIONS:

  1. Alpha Channels & Transparencies. Is there a way to discard all Alpha Channels?

    • If possible, would be nice if there was an option to keep transparency if present/desired. If it needs to be discarded to work, would like a WHITE background as opposed to BLACK.

    • The PHP code offered here by @MarkSetchell may be part of my solution, though am not advanced enough to know how to add to the code below.*

  2. Spot Channels. Is there a way to convert/merge the Spot Channels into RGB? (A Spot Channel is a special color, for example Pantone 072 Blue, that is added to extend the gamut of the image. For these purposes, would like it merged to get a general representation in RGB.)

  3. Crop to Edge Pixels. Would like to add a feature that could crop the image to the image edge pixels (as oppsoed to canvas), if desired, preferrably before resizing to the max dimension provied by the user.


CURRENT APPLESCRIPT CODE:

property extension_list : {"tif", "tiff", "png", "jpeg", "jpg", "pict", "psb", "pdf", "bmp", "eps", "psd", "heic"}

global current_folder
global pixel_size

--___________________________________________________________________________ drag and drop

on open these_items
    log "open"
    log these_items
    try
        tell application "Finder"
            set maxpixel_size to display dialog "Enter desired maximum pixel dimension below." default answer "600" buttons {"Cancel", "Continue"} default button "Continue" with icon note
            set pixel_size to (text returned of the result)
        end tell
        process(these_items, pixel_size)
        delay 2
        display notification with title "Image Conversions Complete"
    on error error_message
        tell me to "exit"
    end try
end open

--___________________________________________________________________________ files

on process(these_items, pixel_size)
    initialise()
    log "process " & these_items
    repeat with i from 1 to the count of these_items
        set this_item to (item i of these_items)
        set the item_info to info for this_item
        if (alias of the item_info is false) and ¬
            (the name extension of the item_info is in the extension_list) then
            process_item(this_item, pixel_size)
        end if
    end repeat
    finalise()
end process

--___________________________________________________________________________ folder

on process_folder(this_folder, pixel_size)
    initialise()
    set current_folder to this_folder
    log "process_folder " & this_folder
    set these_items to list folder this_folder without invisibles
    repeat with i from 1 to the count of these_items
        set this_item to alias ((this_folder as text) & (item i of these_items))
        set the item_info to info for this_item
        if (alias of the item_info is false) and ¬
            (the name extension of the item_info is in the extension_list) then
            process_item(this_item, pixel_size)
        end if
    end repeat
    finalise()
end process_folder

on initialise()
    log "initialise"
end initialise

on finalise()
    log "finalise"
end finalise

--___________________________________________________________________________ sips process

on process_item(item_alias, pixel_size)
    log "process_item: " & item_alias
    
    set this_path to (item_alias) as string
    set the newPNG_name to my add_extension(item_alias, "png")
    
    try
        set the_command to "sips --setProperty format png --resampleHeightWidthMax " & pixel_size & " " & quoted form of POSIX path of item_alias & " --out " & quoted form of newPNG_name
        log "the_command: " & the_command
        do shell script the_command
        
    on error error_message
        display dialog error_message
    end try
end process_item

--____________________________________________________________________________ default behavior

try
    tell application "Finder"
        set the source_folder to choose folder with prompt ¬
            "Pick a folder to process:"
        set maxpixel_size to display dialog "What is the maximum pixel size?" default answer "600" buttons {"Continue"} default button "Continue" with icon note
        set pixel_size to (text returned of the result)
    end tell
    process_folder(source_folder, pixel_size)
    delay 2
    display notification with title "Image Conversions Complete"
on error error_message
    tell me to "exit"
end try

--________________________________________________________________________________ file ext

on add_extension(item_alias, new_extension)
    log "add_extension"
    log item_alias
    set this_info to the info for item_alias
    --set this_name to the name of this_info
    --set this_name to (item_alias) as string
    set this_name to POSIX path of item_alias
    
    set this_extension to the name extension of this_info
    if this_extension is missing value then
        set the default_name to this_name
    else
        set the default_name to text 1 thru -((length of this_extension) + 2) of this_name
    end if
    return (the default_name & "." & the new_extension)
end add_extension

Any help with any of the (3) limitations above would be amazing!


Essentially what I'm looking for is a preview of the image as if it were opened in Photoshop, Illustrator, InDesign, etc. (MacOS Finder Preview doesn't preview properly either.)

Link below takes you to reference files... the original PSD with an Alpha Channel, Spot Channel, and transparency, along with 3 PNG examples that would be the desired output... one is at the same size, the other trimmed to the outermost pixels, and the other padded 10px around the image edges.

To do this in Photoshop, process is convert to RGB (especially if in CMYK for larger gamut), merge spot channels, discard Alpha Channels, trim all sides (optional), save as PNG. This "quick" method of merging the spots doesn't maintain transparencies. The script above takes care of the rest, aside from padding back a border, if desired.

  • Original_Spot_Alpha.tif
  • Out_flat_trim_pad.png
  • Out_flat_trim.png
  • Out_flat.png

Click here to view/download reference files

Chris
  • 35
  • 5
  • `sips` is pretty useless, Applescript is ridiculously verbose and of limited applicability outside macOS and likely to be phased out. The Perl and Python versions Apple ship are outdated and contain next to no support for image processing. So you are probably pretty much stuck with ancient PHP and `gd` unless you want to learn ObjectiveC or swift - in which any image processing is torturous. Or if you really really can't install anything, buy a $15 Raspberry Pi and send images to that or a web API for offloaded processing. – Mark Setchell Mar 19 '23 at 08:55
  • Another PHP example https://stackoverflow.com/a/35128743/2836621 – Mark Setchell Mar 19 '23 at 08:57
  • Not sure what the objection is to **ImageMagick**, but you could maybe try installing `gmic` as an alternative. The NetPBM suite is also pretty lightweight and quick to install, if a little dated and limited. There is CImg at https://cimg.eu which is C++ which you could try to compile with the Apple-supplied compiler.... – Mark Setchell Mar 19 '23 at 09:10
  • If you have `docker`, it is trivially easy and fast to use **ImageMagick** on the `alpine` linux base, see https://stackoverflow.com/a/74032739/2836621 – Mark Setchell Mar 19 '23 at 09:14
  • Thanks Mark. Installing outside software is not allowed by IT, and if sharing with others, must be easy to use, hence the preference for native code. To be honest, was hoping someone that's already solved would be able to help. I get obsessed to find a solution and can easily spend 100 hours or more on something that someone else could do in 5 minutes... programming isn't my core competency. Anything that could be done through Automator or perhaps even Photoshop/JavaScript at the Finder level. Will have to investigate the options you presented above further; all over my head at the moment. – Chris Mar 20 '23 at 04:09
  • Maybe you could add a representative input and corresponding output image to your question and clarify what you call a *"spot channel"*. – Mark Setchell Mar 20 '23 at 07:58
  • @MarkSetchell, a "Spot Channel" is an extra color added in Photoshop, for example a Pantone color, to extend the CMYK gamut for print. I've added sample files in the original post for reference, with additional notes added to the bottom of the post about each. – Chris Mar 21 '23 at 16:37

1 Answers1

0

Here's an updated version of your script that should address the first (background will be white) and third limitation:

on process_item(item_alias, pixel_size)

log "process_item: " & item_alias

set this_path to (item_alias) as string
set the newPNG_name to my add_extension(item_alias, "png")

try
    set the_command to "sips --setProperty format png --resampleHeightWidthMax " & pixel_size & " " & quoted form of POSIX path of item_alias & " --out " & quoted form of newPNG_name
    log "the_command: " & the_command
    do shell script the_command
    
    -- Remove alpha channel (transparency) and set the background color to white
    set the_command to "sips --flatten --setProperty backgroundColor '255 255 255 1' " & quoted form of newPNG_name & " --out " & quoted form of newPNG_name
    log "the_command: " & the_command
    do shell script the_command
    
    -- Crop image to edge pixels
    tell application "Image Events"
        set the_image to open newPNG_name
        crop the_image to (crop box the_image)
        save the_image in newPNG_name as "PNG"
        close the_image
    end tell
    
on error error_message
    display dialog error_message
end try
end process_item

To handle spot channels, you would need to install Ghostscript since AppleScript does not support handling spot channels.

  • Hi @Noel, thank you! ScriptEditor doesn't like the line `crop the_image to (crop box the_image)`. I get a Syntax Error: Expected end of line, etc. but found “to”. Thoughts? – Chris Apr 06 '23 at 15:28