Skip to content

Instantly share code, notes, and snippets.

@truongduyng
Created March 18, 2026 05:57
Show Gist options
  • Select an option

  • Save truongduyng/0c80dcab995dbc2a6255e506f9247899 to your computer and use it in GitHub Desktop.

Select an option

Save truongduyng/0c80dcab995dbc2a6255e506f9247899 to your computer and use it in GitHub Desktop.
macOS Quick Action that adds a random gradient background + drop shadow to images. Supports batch processing — just select multiple files in Finder, right-click, and go. Requires ImageMagick.
#!/bin/zsh
# Add Random Gradient Background + Drop Shadow - macOS Quick Action Script
# Requires: ImageMagick (brew install imagemagick)
export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"
for INPUT_FILE in "$@"; do
FILENAME=$(basename "$INPUT_FILE")
DIRNAME=$(dirname "$INPUT_FILE")
EXTENSION="${FILENAME##*.}"
BASENAME="${FILENAME%.*}"
OUTPUT_FILE="${DIRNAME}/${BASENAME}_gradient.${EXTENSION}"
# Get image dimensions
WIDTH=$(magick identify -format "%w" "$INPUT_FILE")
HEIGHT=$(magick identify -format "%h" "$INPUT_FILE")
# Generate random colors (hex), avoid near-white (brightness < 200)
MAX_BRIGHTNESS=200
while true; do
R1=$((RANDOM % 256)); G1=$((RANDOM % 256)); B1=$((RANDOM % 256))
BRIGHTNESS1=$(( (R1 + G1 + B1) / 3 ))
[[ $BRIGHTNESS1 -lt $MAX_BRIGHTNESS ]] && break
done
while true; do
R2=$((RANDOM % 256)); G2=$((RANDOM % 256)); B2=$((RANDOM % 256))
BRIGHTNESS2=$(( (R2 + G2 + B2) / 3 ))
[[ $BRIGHTNESS2 -lt $MAX_BRIGHTNESS ]] && break
done
COLOR1=$(printf '#%02X%02X%02X' $R1 $G1 $B1)
COLOR2=$(printf '#%02X%02X%02X' $R2 $G2 $B2)
# Random gradient direction
DIRECTIONS=("0" "45" "90" "135" "180" "225" "270" "315")
ANGLE=${DIRECTIONS[$((RANDOM % ${#DIRECTIONS[@]}))]}
# Settings
PADDING=80 # space around image
SHADOW_OFFSET=12 # shadow offset in px
SHADOW_BLUR=18 # shadow softness
SHADOW_OPACITY=70 # shadow darkness (0-100)
CANVAS_W=$((WIDTH + PADDING * 2))
CANVAS_H=$((HEIGHT + PADDING * 2))
# Create gradient → add shadow to image → composite on gradient
magick \
-size "${CANVAS_W}x${CANVAS_H}" \
"gradient:${COLOR1}-${COLOR2}" \
-define gradient:angle="${ANGLE}" \
\( "$INPUT_FILE" \
\( +clone \
-background "rgba(0,0,0,${SHADOW_OPACITY})" \
-shadow "${SHADOW_OPACITY}x${SHADOW_BLUR}+${SHADOW_OFFSET}+${SHADOW_OFFSET}" \
\) \
+swap \
-background none \
-layers merge \
\) \
-gravity center \
-composite \
"$OUTPUT_FILE"
echo "✅ Saved: $OUTPUT_FILE"
echo " Gradient: $COLOR1$COLOR2 | Angle: ${ANGLE}° | Shadow: offset=${SHADOW_OFFSET}px blur=${SHADOW_BLUR}px"
done
@truongduyng
Copy link
Author

A macOS Quick Action script that adds a random gradient background + drop shadow to any image (or batch of images) directly from Finder's right-click menu. Powered by ImageMagick.


What it does

  • Generates two random colors for the gradient (never near-white)
  • Picks a random gradient angle (0°, 45°, 90°… 315°)
  • Adds a soft drop shadow behind your image
  • Saves the output as a new file (yourfile_gradient.jpg) — original is untouched
  • Works on a single file or multiple files at once

Requirements


Step 1 — Install ImageMagick

Open Terminal and run:

brew install imagemagick

Verify it's working:

magick --version

Step 2 — Create the Quick Action in Automator

  1. Open Automator (use Spotlight: ⌘ Space → type Automator)
  2. Click New Document
  3. Choose Quick Action as the document type
  4. At the top of the workflow, set:
    • Workflow receives currentimage files
    • inFinder
  5. In the search bar on the left, search for Run Shell Script
  6. Drag it into the workflow area on the right
  7. In the action settings:
    • Shell/bin/zsh
    • Pass inputas arguments
  8. Paste the script above into the text area

Step 3 — Save the Quick Action

  1. Press ⌘ S to save
  2. Name it: Add Gradient Background
  3. Close Automator

Step 4 — Use it in Finder

  1. Select one or more image files in Finder
  2. Right-click → Quick ActionsAdd Gradient Background
  3. Done! A new file named yourimage_gradient.jpg will appear in the same folder

Batch processing works out of the box — select as many images as you want, they'll each get their own unique random gradient.


Customization

You can tweak these four variables inside the script:

Variable Default What it controls
PADDING 80 Border size around the image (px)
SHADOW_OFFSET 12 How far the shadow is offset (px)
SHADOW_BLUR 18 Shadow softness / spread
SHADOW_OPACITY 70 Shadow darkness (0 = none, 100 = solid black)
MAX_BRIGHTNESS 200 Max brightness to allow for gradient colors (lower = darker colors only)

Troubleshooting

Quick Action doesn't appear in Finder

  • Make sure you set "Workflow receives current → image files in Finder" in Automator
  • Try logging out and back in, or restart Finder: killall Finder

magick: command not found

  • The PATH line at the top of the script should fix this, but double-check ImageMagick is installed: brew install imagemagick
  • Also confirm the shell in Automator is set to /bin/zsh

Test it in Terminal first

zsh /path/to/add_gradient_background.sh /path/to/image.jpg

This shows the exact error if something goes wrong.


Output example

✅ Saved: /Users/you/Desktop/photo_gradient.jpg
   Gradient: #3A1F8C → #C04A22 | Angle: 135° | Shadow: offset=12px blur=18px
Screenshot 2026-03-18 at 11 27 44_gradient 12 56 06

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment