Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Service to "Make Protected Zip" files Apps
Mac OS X has long supported password encrypted zip files, but you have to use command line to do it. So here is a simple Automator based Service to give you a GUI.

OpenAutomator and choose "Service" (the gear). Change "Service receives selected" to Files or folders in "Finder.app"

Add the "Run Applescript" step and then copy the code below and replace all the code in the "Run Applescript" command with this code.

Choose save, naming it something like "Make Protected Zip", then test it by going to the finder and selecting one or more files/folders. Scroll down to the "Services" Menu and select the service with the name you just saved as.


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
    •    
  • Currently 4.25 / 5
  You rated: 5 / 5 (8 votes cast)
 
[8,942 views]  

Service to "Make Protected Zip" files | 14 comments | Create New Account
Click here to return to the 'Service to "Make Protected Zip" files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Service to "Make Protected Zip" files
Authored by: markuswarren on Oct 17, '13 07:49:30AM

You may want to make a slight change to this line:

set passwd to text returned of (display dialog "password for zipped file" default answer "password" buttons {"OK", "Cancel"} default button "OK")

changing it to:

set passwd to text returned of (display dialog "password for zipped file" default answer "password" buttons {"OK", "Cancel"} default button "OK" with hidden answer)

The "with hidden answer" option will bullet out the password that is being entered.



[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: chocky on Oct 17, '13 09:10:45AM
Unfortunately Automator uses its own Dictionary which does not include "with hidden answer". To get around this enclose the section in a tell statement: (I also added a quick confirmation since you won't be able to see what you typed ;)

set passwd to "x"
set passwd_2 to "y"
repeat while passwd ≠ passwd_2
	tell application "System Events"
		activate
		set passwd to text returned of (display dialog "password for zipped file" default answer "" buttons {"OK", "Cancel"} default button "OK" with hidden answer)
		set passwd_2 to text returned of (display dialog "Please reenter your password" default answer "" buttons {"OK", "Cancel"} default button "OK" with hidden answer)
	end tell
	
end repeat


[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: barryjaylevine on Oct 19, '13 09:52:33AM

Thanks, chocky & ThomasMyers. Chocky's password modification script grafted into ThomasMyers' original code works perfectly. Excellent set of tips!.

---
-----
Two things in this world aren't overrated: MacOSX and Lemon Meringue Pie.



[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: David Hein on Oct 17, '13 02:52:15PM

i would like to use this but the main one input on 10.9 does not work. the comments left me confused since i don't do applescript. thanks, looking forward to using this.



[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: Fofer on Oct 18, '13 07:32:18AM

Same issue here - I'd love to see this AppleScript updated for 10.9 compatibility.



[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: chocky on Oct 21, '13 09:11:43PM
I just tested the modified version in Mavericks GM and it works just fine. Here's a complete modified version:

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 "x"
		set passwd_2 to "y"
		repeat while passwd ≠ passwd_2
			tell application "System Events"
				activate
				set passwd to text returned of (display dialog "password for zipped file" default answer "" buttons {"OK", "Cancel"} default button "OK" with hidden answer)
				set passwd_2 to text returned of (display dialog "Please reenter your password" default answer "" buttons {"OK", "Cancel"} default button "OK" with hidden answer)
			end tell
			
		end repeat
		
		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


[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: ThomasMyers on Oct 17, '13 08:15:04PM

This has been tested on both 10.8.5 and 10.9. You can use the line
set passwd to text returned of (display dialog "password for zipped file" default answer "password" buttons {"OK", "Cancel"} default button "OK" with hidden answer)

But if you do I recommend you add a 2nd verify password as you can't see what you are typing in. You do not need to use system events. It works just fine. I found that I didn't want to have to enter the password twice.



[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: chocky on Oct 22, '13 07:35:41AM
And, lest I forget, a big THANKS to ThomasMyers (the OP) for an excellent tip. Not only useful in and of itself but it served as a reminder to make more use of Automator and Services.

[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: shled on Nov 05, '13 04:38:41PM

This is a wonderful hint. Thanks to all who contributed to it.
To further fine tune it, how would I change the default name for the encrypted archive I'm creating to offer the name of the original folder, perhaps with -encrypted appended?



[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: shled on Nov 06, '13 07:59:02AM

This is a wonderful hint.
How would I change the default name for the encypted archive I making to show the name of the original folder, perhaps with -encrypted appended?



[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: darrylb on Oct 22, '13 12:48:39PM

This hint works very well, thanks. I'd like to add this AppleScript to Quickeys instead of as a service through Automater, and I can't get it to work.

I assume that the ApppleScript as configured doesn't see the Finder selection as an input. That is must be happening in Automater.

I'm not very well versed in AppleScript. Anyone know how to make the changes so that this could run as a standalone? I think it's a matter of taking the file or folder that's selected in the Finder and passing that along to the system as the input for the zip file.

Edited on Oct 22, '13 12:49:27PM by darrylb



[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: jelockwood on Nov 06, '13 08:26:53AM
I tried out the modified version posted by Chocky with the password hidden and it (mostly) works fine. I did however find a few issues.

1. It includes all the parent hierarchy in the zip file it produces, i.e. if your in /Users/youraccount/Downloads and select a file and make a zip it will contain not only your selection but the parent hierarchy of Users/youraccount/Downloads/ as well. This is because the version written by Chocky (and the original by ThomasMyers) include the -j flag in the command. (Probably related to the following two issues.)

Note: This is contrary to the behaviour of the built-in 'Compress' command provided by Apple in the Finder.

2. If you try to make a zip of a package type file the result when decompressed does not restore a working package.

3. If you try to make a zip of a normal folder it fails completely.

I have fixed all the above issues, the modified version is listed below. Tested only under Mountain Lion so far.


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 "x"
		set passwd_2 to "y"
		repeat while passwd ≠ passwd_2
			tell application "System Events"
				activate
				set passwd to text returned of (display dialog "password for zipped file" default answer "" buttons {"OK", "Cancel"} default button "OK" with hidden answer)
				set passwd_2 to text returned of (display dialog "Please reenter your password" default answer "" buttons {"OK", "Cancel"} default button "OK" with hidden answer)
			end tell
			
		end repeat
		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)
				set itemName to name of thisItem
				set itemStr to itemStr & quoted form of itemName & " "
			end tell
		end repeat
		if (the last character of itemStr is " ") then set itemStr to characters 1 thru ((length of itemStr) - 1) of itemStr as string
		set zipFile to POSIX path of (parentFolder & archiveFileName)
		set cmd to "cd " & parentFolder & " ; zip -P " & passwd & " -r " & zipFile & " " & itemStr & " -x *.DS_Store"
		do shell script cmd
	end if
	return
end run
Edited on Nov 06, '13 08:29:32AM by jelockwood


[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: prr on Nov 07, '13 05:57:32PM

I'm having some trouble with this Service as it seems to not like file paths with spaces in them. Is that possible? That's what it seems like, but the "The action “Run AppleScript” encountered an error." error message is kind of vague, so it's hard to tell.

That all said, this works great otherwise! Thanks!



[ Reply to This | # ]
Service to "Make Protected Zip" files
Authored by: jelockwood on Nov 20, '13 09:21:15AM
per you were right. If one of the parent folders of the selected items has a space in the name it failed. I have (hopefully) fixed this in the version below.

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 "x"
		set passwd_2 to "y"
		repeat while passwd ≠ passwd_2
			tell application "System Events"
				activate
				set passwd to text returned of (display dialog "password for zipped file" default answer "" buttons {"OK", "Cancel"} default button "OK" with hidden answer)
				set passwd_2 to text returned of (display dialog "Please reenter your password" default answer "" buttons {"OK", "Cancel"} default button "OK" with hidden answer)
			end tell
			
		end repeat
		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)
				set itemName to name of thisItem
				set itemStr to itemStr & quoted form of itemName & " "
			end tell
		end repeat
		if (the last character of itemStr is " ") then set itemStr to characters 1 thru ((length of itemStr) - 1) of itemStr as string
		set zipFile to quoted form of POSIX path of (parentFolder & archiveFileName)
		set cmd to "cd " & quoted form of parentFolder & " ; zip -P " & passwd & " -r " & zipFile & " " & itemStr & " -x *.DS_Store"
		do shell script cmd
	end if
	return
end run


[ Reply to This | # ]