Compress / Archive files and folders into a zip with the MacOS system tools. I know that I could use something like
Code: Select all
zip -er archive.zip /path/to/directory/
I love the built-in Compress (ZIP Archive) action in lauchbar and would like to have a similar one that does essentially the same but with a password prompt*.
At first I tryed the follwoing code for my Automator-Service:
Code: Select all
on run {input, parameters}
set dialogResults to display dialog "Name for zipped file (no extension)" default answer "Archive" buttons {"OK", "Cancel"} default button "OK"
if button returned of dialogResults is "OK" then
set passwd to text returned of (display dialog "password for zipped file" default answer "password" buttons {"OK", "Cancel"} default button "OK")
set archiveName to text returned of dialogResults
tell application "Finder"
set archiveFileName to archiveName & ".zip"
-- Append on a number if file exists.
set suffix to 1
set theFileExists to true
repeat while theFileExists
try
set archiveFile to ((container of (item 1 of input) as Unicode text) & archiveFileName)
if exists file archiveFile then
set archiveFileName to archiveName & suffix & ".zip"
set suffix to suffix + 1
else
set theFileExists to false
end if
end try
end repeat
end tell
set itemStr to ""
repeat with thisItem in input
set itemPath to quoted form of (POSIX path of thisItem)
tell application "Finder"
set parentFolder to POSIX path of (container of thisItem as alias)
end tell
set itemStr to itemStr & " " & itemPath
end repeat
set zipFile to quoted form of (parentFolder & archiveFileName)
set cmd to "zip -P " & passwd & " -rj " & zipFile & " " & itemStr & " -x *.DS_Store"
do shell script cmd
end if
return
end run
This almost works as expected but it’s completly removing any folder structures inside the archive. So after unzipping all files are on a top-level without any folder structure at all. I then just played around and removed the -rj in line 34. Now I have a folder structure but it’s the absolute path from User/… not the actual chosen folder.
Code: Select all
on run {input, parameters}
set dialogResults to display dialog "Name for zipped file (no extension)" default answer "Archive" buttons {"OK", "Cancel"} default button "OK"
if button returned of dialogResults is "OK" then
set passwd to text returned of (display dialog "password for zipped file" default answer "password" buttons {"OK", "Cancel"} default button "OK")
set archiveName to text returned of dialogResults
tell application "Finder"
set archiveFileName to archiveName & ".zip"
-- Append on a number if file exists.
set suffix to 1
set theFileExists to true
repeat while theFileExists
try
set archiveFile to ((container of (item 1 of input) as Unicode text) & archiveFileName)
if exists file archiveFile then
set archiveFileName to archiveName & suffix & ".zip"
set suffix to suffix + 1
else
set theFileExists to false
end if
end try
end repeat
end tell
set itemStr to ""
repeat with thisItem in input
set itemPath to quoted form of (POSIX path of thisItem)
tell application "Finder"
set parentFolder to POSIX path of (container of thisItem as alias)
end tell
set itemStr to itemStr & " " & itemPath
end repeat
set zipFile to quoted form of (parentFolder & archiveFileName)
set cmd to "zip -P " & passwd & " " & zipFile & " " & itemStr & " -x *.DS_Store"
do shell script cmd
end if
return
end run
Is there anyone around that could help me out with this? I don’t want to install a big chunk of software to do these tasks and I also would prefer to not switch zu terminal to do something simple like this.
Any help is appreciated!