shicloudautomatoricnsiconutil

Get path from both local and iCloud directories


I have an Automator Quick Action script that converts PNG to ICNS, keeping both the original file name and its original position. However, if used inside an iCloud folder or a folder with a space in its name, the script won't work.

#!/bin/sh

s=$1
NAME="${s%.*}"

ICONSET="$NAME.iconset"
mkdir $ICONSET

sips -z 1024 1024 $1 --out $ICONSET/icon_512x512@2x.png
sips -z 512  512  $ICONSET/icon_512x512@2x.png --out $ICONSET/icon_512x512.png
sips -z 512  512  $ICONSET/icon_512x512@2x.png --out $ICONSET/icon_256x256@2x.png
sips -z 256  256  $ICONSET/icon_512x512@2x.png --out $ICONSET/icon_256x256.png
sips -z 256  256  $ICONSET/icon_512x512@2x.png --out $ICONSET/icon_128x128@2x.png
sips -z 128  128  $ICONSET/icon_512x512@2x.png --out $ICONSET/icon_128x128.png
sips -z 64   64   $ICONSET/icon_512x512@2x.png --out $ICONSET/icon_32x32@2x.png
sips -z 32   32   $ICONSET/icon_512x512@2x.png --out $ICONSET/icon_32x32.png
sips -z 32   32   $ICONSET/icon_512x512@2x.png --out $ICONSET/icon_16x16@2x.png
sips -z 16   16   $ICONSET/icon_512x512@2x.png --out $ICONSET/icon_16x16.png

iconutil -c icns $ICONSET

rm -rf $ICONSET

How do I get the current path — no matter if local or iCloud — and also make the script work in folders that contain spaces in their names?


Solution

  • Be more consistent with quoting in order to handle special characters like spaces in the name.

    You can also write it as a for loop to prevent copy/paste errors for repetitive tasks.

    Assuming $1 is a regular file that can be read multiple times:

    #!/bin/bash
    
    s="$1"
    NAME="${s%.*}"
    
    ICONSET="$NAME.iconset"
    mkdir -p "$ICONSET"
    
    for res in 512 256 128 64 32 16; do
        x2=$((res * 2))
        sips -z $x2  $x2  "$1" --out "$ICONSET/icon_${res}x${res}@2x.png"
        sips -z $res $res "$1" --out "$ICONSET/icon_${res}x${res}.png"
    done
    
    iconutil -c icns "$ICONSET"
    
    rm -rf "$ICONSET"