Use Hazel and Automator to convert image while retaining original

I’ve got a weekly talk that I do, and I need to update the title card each week. I export the Keynote slide to .tiff (since my understanding is that’s the best to use in terms of adding media to iMovie), but I need a .jpg to use as the thumbnail in places like YouTube or Facebook.

For weeks now I’ve been taking the file, adding it to Permute, and then letting it sit there until I need it.

What I’m wondering, though, is how to create a Hazel action that will monitor that folder and then run an Automator workflow on it that will retain the original while giving me a converted JPG for use as a thumbnail.

I understand this may be kind of “rube goldbergian”, but I can’t tell how to both duplicate a file, retain a copy, and convert the duplicate.

Thanks for any help anyone can offer!

1 Like

@monster94, maybe something like this…

Note that this script assumes that the folder containing the TIFF files includes a subfolder named Converted.

#!/bin/zsh
PATH="/usr/local/bin:$PATH"

scriptdir=${0:a:h}
filedir=$(dirname -- "$1")
basename=$(basename -- "$1")
filename="${basename%.*}"
fileext="${basename##*.}"

# Note: Hazel assumes paths are relative to the folder enclosing $1.
#       In contrast, with a Keyboard Maestro shell script, one must:
#         a. use full pathnames, or
#         b. cd to the folder of interest.

# To debug, remove leading # in the following:
#echo $scriptdir > Converted/0.txt
#echo $1 > Converted/1.txt
#echo $filedir > Converted/2.txt
#echo $filename > Converted/3.txt
#echo $fileext > Converted/4.txt

# Note: Will likey want to remove "-Z 400" or at least change 400. Refer to sips man page.
sips -s format jpeg -Z 400 "$1" --out "$filedir/Converted/$filename.jpg"
1 Like

Thanks, I’ll check that out. I don’t normally run scripts like that because even Automator sometimes seems a bit too advanced, but maybe I’ll give it a whirl!

@monster94, give it a try and if you have any questions, please don’t hesitate to ask. Here’s the basic outline:

  1. Open the folder that will contain the TIFF files. For this example, I’ll call it TIFF Files. Create a subfolder within named Converted. (Note: This subfolder name is case sensitive.)

  2. In Hazel, select the TIFF Files folder and add a rule named Convert TIFF to JPEG. The name can be any description meaningful to you.

  3. Within the rule, add If • all • of the following conditions are met: Extension • is • tiff (Note: In this case, tiff is not case sensitive.)

  4. Under the condition, add a Do the following to the match file or folder: Run shell script • embedded script.

  5. Copy the script supplied above. To embed the script, click i Edit Script and paste the script into the embedded script editor

  6. Save the rule.

  7. To test, add a TIFF file to the TIFF Files folder.

I regularly end up with a folder full of GIFs. My guess is this approach could get me them all converted to JPEG - with quite a lot of control over eg sizing.

@Martin_Packer, yes. I suspected there would no issues with GIF’s, but I did a quick test anyway. It worked as expected.

The shell script can be used as-is unless of course you want to adjust the size of the output JPEG. (The sips man page includes the transformation options.)

1 Like

This works perfectly, thanks! One question. When Hazel runs the script, it will convert a .tif file (for example a file that is 1.5 GB) into a .jpg file that’s 42 KB. What’s the command in the script that is converting it to that particular size of file? I’d like to have the .jpg file end up larger. For example, what would I change in the script to convert 1.5 GB file to a 1.5 MB .jpg file?

Hi @gonzalezja1, thanks for the comment.

It’s the sips command. You can access the man page from the Terminal app to get all of the possibilities:

% man sips

sips(1)                   BSD General Commands Manual                  sips(1)

NAME
     sips -- scriptable image processing system.

SYNOPSIS
     sips [image-functions] imagefile ...
     sips [profile-functions] profile ...

DESCRIPTION
     This tool is used to query or modify raster image files and ColorSync ICC profiles.  Its
     functionality can also be used through the "Image Events" AppleScript suite.  It also sup-
     ports executing JavaScript to either modify or generate images.

[ continued ]

For the -Z option, here’s the information:

-Z pixelsWH
     --resampleHeightWidthMax pixelsWH
           Resample image so height and width aren't greater than specified size.

Thanks!! Very helpful.

1 Like

You’re welcome. Hazel with shell scripts and/or AppleScript is a powerful combination. If you want to go far beyond the native macOS script/command capability check out the possibilities with Homebrew.

This is what I ended up cooking up. The “MP - Title Card” gets created weekly and deleted automatically by Hazel after a few days. This folder action works and keeps me from going into an infinite loop, which happened recently.

I guess what I need to do is set up a hazel action to look for this folder to be created, run the action on it, and then it’ll be set.

Thanks to everyone for their input and feedback! I guess it spurred some wondering about other things to do?

Coincidentally I was thinking of suggesting an Automator Folder Action for those that don’t have Hazel.

Anyone interested in using the sips command as I suggested above in combination with an Automator Folder Action, might find this video by MacMost helpful: Automatically Organize Files With an Automator Folder Action Shell Script - YouTube.

I like the sips command because of its many capabilities.

1 Like