How to pass path of selected file from Finder to Automator Shell script

automatorfinderzip

I am creating Automator Service to unzip selected files in the same folder they are located.

How can I get selected file absolute path in Automator, so I can pass it into my shell script ?

As you can see I am only passing filename for now, but its not unzipping in same folder.

I would like to change folder before unzip, and then execute unzip. I tried cd ~ but it unzips to my homefolder. I would like to unzip in same folder as selected files exists.

enter image description here

Best Answer

Add cd "$(dirname "$f")" before the unzip line.

for f in "$@"; do
    cd "$(dirname "$f")"
    unzip -o "$f"
done 

or without changing to the directory, use the -d option

[-d exdir]
          An  optional  directory  to which to extract files.  By default, all files and subdirectories
          are recreated in the current directory; the -d  option  allows  extraction  in  an  arbitrary
          directory.....



for f in "$@"; do
    unzip "$f" -d "$(dirname "$f")"
done