Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
    
What have you tried so far? –  ckpepper02 May 10 '13 at 23:20
add comment

2 Answers 2

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"}
share|improve this answer
    
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 May 11 '13 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 May 11 '13 at 3:47
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.