Batch convert BMP to both TIFF and JPEG, then have the converted images moved into respective folders

The example shell script code, shown below, can be used as an Automator workflow created as either an Application or Service/Quick Action, using a Run Shell Script action with setting Pass input: [as arguments] and replacing the default code with it, as shown in the images further below.

For each folder dropped on the Automator workflow created as an Application, the example shell script code does the following:

  • Changes directory into the dropped folder.
  • Deletes the .xx and .zz files within.
  • Makes the Job Number-JPEG and Job Number-TIFF folders,
    e.g., 2021_1234-JPEG and 2021_1234-TIFF.
  • Creates a .jpg type file and .tiff type file of each .bmp type file into their respective folders.
for dir in "$@"; do

    [ -d "${dir}" ] || continue
    [ -w "${dir}" ] || continue
    cd "${dir}" || exit
    rm *.xx *.zz
    dn="${dir##*/}"
    mkdir -p "${dn}-JPEG" "${dn}-TIFF"

    for file in *.bmp; do
        sips --setProperty format jpeg "${file}" -o "${dn}-JPEG/"
        sips --setProperty format tiff --setProperty formatOptions lzw "${file}" -o "${dn}-TIFF/"
    done

done

To use the example shell script code as an Automator workflow created as a Service/Quick Action, set Workflow receives current [folders] in [Finder], as shown in the second image below.

Then in Finder, select the target folder(s) and right-click selecting the Service/Quick Action from the Services context menu.


Understanding the shell script:

  • for dir in "$@"; do -- For each folder dropped or selected, do the following:
  • [ -d "${dir}" ] || continue -- Makes sure the Finder item dropped or selected is a directory, or continue with the next item.
  • [ -w "${dir}" ] || continue -- Makes sure the Finder item dropped or selected is writable, or continue with the next item.
  • cd "${dir}" || exit -- Changes directory to the dropped or selected folder, or exits the shell script to avoid erroneous processing of remaining code.
  • rm *.xx *.zz -- Deletes all .xx and .zz files within the "${dir}" directory.
  • dn="${dir##*/}" -- Uses shell parameter expansion to get the directory name portion of the fully qualified pathname of the folder dropped or selected. Not actually necessary and could have just used "${dir##*/}" through the rest of the code, however, I believe it makes the remaining code easier to read.
  • mkdir -p "${dn}-JPEG" "${dn}-TIFF" -- Makes the Job Number-JPEG and Job Number-TIFF directories, e.g., 2021_1234-JPEG and 2021_1234-TIFF.
  • for file in *.bmp; do -- For each .bmp type file do the following:
  • sips --setProperty format jpeg "${file}" -o "${dn}-JPEG/" -- Create a .jpg type file of each .bmp type file into the Job Number-JPEG, e.g., 2021_1234-JPEG directory.
  • sips --setProperty format tiff --setProperty formatOptions lzw "${file}" -o "${dn}-TIFF/" -- Create a .tiff type file of each .bmp type file, with the requested LZW compression, into the Job Number-TIFF, e.g., 2021_1234-TIFF directory.


Automator workflow as an Application

Automator workflow as a Service/Quick Action