Skip to content

Instantly share code, notes, and snippets.

@ffeu
Last active October 12, 2020 12:34
Show Gist options
  • Save ffeu/ebbade2e2e91a9a8576bd7ff06b9367a to your computer and use it in GitHub Desktop.
Save ffeu/ebbade2e2e91a9a8576bd7ff06b9367a to your computer and use it in GitHub Desktop.
Gist to document a way to add a MacOS Automator Quick Action which automatically extracts the images inside a ZIP file. This is useful when you pick and download a few images taken from iPhone from Google Photos, and instead of an Image you get a ZIP file. This will extract the images inside the ZIP file and delete the original file.
for fullpath in "$@"
do
dir="$(/usr/bin/dirname $fullpath)"
filename="${fullpath##*/}" # Strip longest match of */ from start
base="${filename%.[^.]*}" # Strip shortest match of . plus at least one non-dot char from end
ext="${filename:${#base} + 1}" # Substring from len of base thru end
if [[ -z "$base" && -n "$ext" ]]; then # If we have an extension and no base, it's really the base
base=".$ext"
ext=""
fi
# echo -e "$fullpath:\n\tdir = \"$dir\"\n\tbase = \"$base\"\n\text = \"$ext\""
/usr/bin/unzip -o -qq "$fullpath" -d "$dir/$base"
for temp_file in $dir/$base/*; do
# echo "temp_file = $temp_file"
temp_filename="${temp_file##*/}"
temp_dir="$(/usr/bin/dirname $temp_file)"
temp_base="${temp_filename%.[^.]*}"
temp_ext="${temp_filename:${#temp_base} + 1}"
if [[ -z "$temp_base" && -n "$temp_ext" ]]; then
temp_base=".$temp_ext"
temp_ext=""
fi
# echo "ext = $temp_ext"
if [[ "$temp_ext" == 'heic' ]]; then
/bin/mv "$temp_file" "$dir/$base.heic"
fi
if [[ "$temp_ext" == 'HEIC' ]]; then
/bin/mv "$temp_file" "$dir/$base.heic"
fi
if [[ "$temp_ext" == 'jpeg' ]]; then
/bin/mv "$temp_file" "$dir/$base.jpg"
fi
if [[ "$temp_ext" == 'JPEG' ]]; then
/bin/mv "$temp_file" "$dir/$base.jpg"
fi
if [[ "$temp_ext" == 'jpg' ]]; then
/bin/mv "$temp_file" "$dir/$base.jpg"
fi
if [[ "$temp_ext" == 'JPG' ]]; then
/bin/mv "$temp_file" "$dir/$base.jpg"
fi
if [[ "$temp_ext" == 'png' ]]; then
/bin/mv "$temp_file" "$dir/$base.png"
fi
if [[ "$temp_ext" == 'PNG' ]]; then
/bin/mv "$temp_file" "$dir/$base.png"
fi
if [[ "$temp_ext" == 'gif' ]]; then
/bin/mv "$temp_file" "$dir/$base.gif"
fi
if [[ "$temp_ext" == 'GIF' ]]; then
/bin/mv "$temp_file" "$dir/$base.gif"
fi
done
/bin/rm -rf "$dir/$base"
done
@ffeu
Copy link
Author

ffeu commented Aug 7, 2020

Create the Quick Action in Automator.

Change Workflow receives current to files or folders
Change Shell to/bin/zsh
Change Pass Input to as arguments

Use the script in the Gist to populate the Run Shell Script action.

image

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