2

I have this automator script that is to be a finder service to process images. The idea is:

  1. I select a bunch of images
  2. I right click and choose the script on Finder services
  3. the script asks me if I want to convert the image to 100x100, 200x200 or 300x300. I can choose multiple options.

Here is what I did: first, a module in applescript that asks me for the sizes I want:

on run
    choose from list {"100", "200", "300"} with prompt "What size(s) do you want? (multiple sizes allowed)" with multiple selections allowed
    set choice to the result as string
    return choice
end run

because this service receive image files, and the following bash script is receiving a list of arguments, this choice variable will be the first on "$@". I need to remove it as the bash script (the next part) starts.

Here is the bash script:

#!/bin/bash

# here is the parameters passed to the functions
# $1 path to the file to be processed
# $2 original image name
# $3 original image directory

function f100 {
   sips -z 100 100 "$1" --out "$3"/"$2"_file100.png
   sips -z 200 200 "$1" --out "$3"/"$2"[email protected]
}

function f200 {
   sips -z 200 200 "$1" --out "$3"/"$2"_file200.png
   sips -z 400 400 "$1" --out "$3"/"$2"[email protected]
}

function f300 {
   sips -z 300 300 "$1" --out "$3"/"$2"_file300.png
   sips -z 600 600 "$1" --out "$3"/"$2"[email protected]
}

choice=$1
shift

for f in "$@"; do

  DIRNAME="$(dirname "$f")"

  filename=$(basename "$f")
  name="${filename%.*}"
  extension="${filename##*.}"


  if [[ $choice == *"100"* ]]
  then 
    f100 $f $filename $DIRNAME 
  fi

  if [[ $choice == *"200"* ]]
  then
    f200 $f $filename $DIRNAME
   fi

  if [[ $choice == *"300"* ]]
  then  
    f300 $f $filename $DIRNAME 
  fi

done

The Applescript part passes the choice as a string to the script. So, if I choose 100 and 200 it will pass a string "100200". If I choose all 3 options, it will pass "100200300", this is why I am searching for the substring on the ifs and running the proper functions.

This script works perfectly from terminal but does nothing when installed as a service on finder. No error, nothing.

1 Answer 1

1

This is because the "Run Shell Script" action does not receive the files, the AppleScript must return the input, like this

on run {input, parameters}
    choose from list {"100", "200", "300"} with prompt "What size(s) do you want? (multiple sizes allowed)" with multiple selections allowed
    set beginning of input to the result as string
    return input
end run

Or

on run {input, parameters}
    choose from list {"100", "200", "300"} with prompt "What size(s) do you want? (multiple sizes allowed)" with multiple selections allowed
    set choice to the result as string
    return {choice} & input
end run

Updated

I tested only on files whose path does not contain spaces or special characters.

You must enclose each variable with a double quotes character.

function f100 {
   sips -z 100 100 "$1" --out "$3"/"$2"_file100.png
   sips -z 200 200 "$1" --out "$3"/"$2"[email protected]
}

function f200 {
   sips -z 200 200 "$1" --out "$3"/"$2"_file200.png
   sips -z 400 400 "$1" --out "$3"/"$2"[email protected]
}

function f300 {
   sips -z 300 300 "$1" --out "$3"/"$2"_file300.png
   sips -z 600 600 "$1" --out "$3"/"$2"[email protected]
}

choice=$1
shift
for f in "$@"; do
  DIRNAME="$(dirname "$f")"

  filename=$(basename "$f")
  name="${filename%.*}"
  extension="${filename##*.}"

  if [[ $choice == *"100"* ]]
  then 
    f100 "$f" "$filename" "$DIRNAME"
  fi
  if [[ $choice == *"200"* ]]
  then
    f200 "$f" "$filename" "$DIRNAME"
  fi
  if [[ $choice == *"300"* ]]
  then  
    f300 "$f" "$filename" "$DIRNAME" 
  fi
done

You can test a service in Automator when the service doesn't work.

Select one or two files in the Finder

Insert the "Get Selected Finder Items" action at the first position of the workflow.

Check the result of each action, like this image enter image description here

I adding some echo "$some variable" to debug the shell script

Sign up to request clarification or add additional context in comments.

3 Comments

I think you may be on the right path but I tried both options and there is no change. The script continues to do nothing.
I tested the workflow with your shell script, it works without issues. There remains a possibility, if you have the input being passed to stdin, just change the popup menu in the top right (next to the Pass input) to as arguments.
is it running for you????? I have my script part adjusted "as arguments" and it is not working for me. Is there any way to echo what the script is doing to terminal or something? Can you send me your automator test workflow so I can compare yours with mine? thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.