# shellcheck shell=dash

___x_cmd_mac_wallpaper(){
    [ $# -gt 0 ]   ||   set --  --help
    local op="$1";      shift
    case "$op" in
        -h|--help)      ___x_cmd help -m mac wallpaper       ;;
        info|set)
                        ___x_cmd_mac_wallpaper_"$op" "$@"    ;;
        *)              N=mac M="Unknown subcmd -> $op" log:ret:64 ;;
    esac
}

___x_cmd_mac_wallpaper_info(){
    [ "$#" -gt 0 ] || set -- 1
    ___x_cmd_mac___cmd osascript -e "tell application \"System Events\" to get picture of desktop $1"
}

___x_cmd_mac_wallpaper_set(){
    [ "$#" -gt 0 ] || N=mac M="Please specify the image path" log:ret:64
    local image="$1"
    [ -f "$image" ] || N=mac M="File $image does not exist" log:ret:64
    if [ -n "$2" ]; then
        ___x_cmd_mac___cmd osascript -e "tell application \"System Events\" to set picture of desktop $2 to (POSIX file \"$image\")"
        mac:info "The desktop $2 wallpaper has been changed successfully."
    else
        ___x_cmd_mac___cmd osascript -e "tell application \"System Events\" to set picture of every desktop to (POSIX file \"$image\")"
        mac:info "The front desktop wallpaper is changed successfully."
    fi
}

