0

I've been searching the internet for a solution to this. I would like to create an Automator workflow that will zip the selected files, name the zip the name of the Parent directory and save that zip in the current directory.

I can't seem to find a way to get the parent directory. I've kinda hit a wall here. Could anyone help me out?

0

2 Answers 2

1

In Automator:

  1. Add Get Selected Finder Items
  2. Add Run Applescript

Inssert this code for the Applescript:

on run {input, parameters}
 set pathList to {}
 repeat with itemNum from 1 to count of input
 tell application "System Events"
 copy POSIX path of (container of (item itemNum of input)) to end of pathList
 end tell
 end repeat
 return pathList
end run

Output/Results:

2013-05-11 00:06:02 +0000: Get Selected Finder Items completed
2013-05-11 00:06:02 +0000: Conversion from Files/Folders to Files/Folders completed
2013-05-11 00:06:02 +0000: Run AppleScript completed
2013-05-11 00:06:02 +0000: Workflow completed

{"/Users/Desktop", "/Users/Desktop", "/Users/Desktop"}
2
  • Ok cool, I tried returning the first item in the last with return first text item of pathList and I'm getting a list back. My path as well as "/Library/NetBoot/NetbootSP0". How can I return just the path I want? Also is there a way I can just return the last directory in that path?
    – tastytoast
    Commented May 11, 2013 at 2:04
  • @tastytoast - return item 1 of input to get the first item only instead of return pathList. It should be fairly obvious if you study the code sample I provided.
    – l'L'l
    Commented May 11, 2013 at 3:47
0

You could use a Create Archive action and rename the resulting archive:

d=${1%/*}
start="$d/${d##*/}"
name="$start.zip"
i=2
while [[ -e $name ]]; do
  name="$start $i.zip"
  let i++
done
mv "$1" "$name"
open -R "$name"

Or use a run shell shell script action like this:

for n in $(seq $(printf %s "$@" | tr -dc / | wc -c)); do
  uniq=$(printf %s\\n "$@" | cut -d / -f1-$n | uniq)
  [[ $uniq != *$'\n'* ]] && dir=$uniq || break
done

cd "${dir:-/}"
[[ $dir ]] && start=${dir##*/} || start=archive
name="$start.zip"

i=2
while [[ -e $name ]]; do
  name="$start $i.zip"
  let i++
done

zip -r "$name" -- "${@#$dir/}"
open -R "$name"

If some files have extended attributes or ACLs, zip removes them. Archive Utility and the Create Archive action store them in the AppleDouble format.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .