HELP • Zip files with password

Hello, I looked on the web for an Applescript allowing you to compress a Finder selection into .zip by giving it a password. I found this:

set display_text to "Please enter your Password:"
repeat
	considering case
		set init_pass to text returned of (display dialog display_text default answer "" with hidden answer)
		set final_pass to text returned of (display dialog "Please check your Password below." buttons {"OK"} default button 1 default answer "" with hidden answer)
		if (final_pass = init_pass) then
			exit repeat
		else
			set display_text to "Incompatible passwords, please try again."
		end if
	end considering
end repeat
tell application "Finder"
	set theItems to selection
	set theItem to (item 1 of theItems) as alias
	set itemPath to quoted form of POSIX path of theItem
	set fileName to name of theItem
	set theFolder to POSIX path of (container of theItem as alias)
	set zipFile to quoted form of (fileName & ".zip")
	do shell script "cd '" & theFolder & "'; zip -x .DS_Store -r0 -P '" & final_pass & "' " & zipFile & " ./'" & fileName & "'"
end tell

Unfortunately, on a selection of files and folders, the script only processes the first item. Could you help me solve this problem?... Thank you...

zip with pw.scpt.zip (4.8 KB)

I’ve never attempted to do this, so I’m not certain this will work or not, but you need to loop through each item in your selection. Perhaps the following would work, or some variant of it. Notice the repeat with nextItem bit in the Finder tell block.

Also, I reformatted the AppleScript in your original post. If you type three backtick characters before pasting in code, and then close it with three more backticks, the forum retains it’s formatting. Click edit on your original post to see what I mean. :wink:

set display_text to "Please enter your Password:"
repeat
	considering case
		set init_pass to text returned of (display dialog display_text default answer "" with hidden answer)
		set final_pass to text returned of (display dialog "Please check your Password below." buttons {"OK"} default button 1 default answer "" with hidden answer)
		if (final_pass = init_pass) then
			exit repeat
		else
			set display_text to "Incompatible passwords, please try again."
		end if
	end considering
end repeat
tell application "Finder"
	set theItems to selection
	repeat with nextItem in theItems
		set theItem to (item 1 of nextItem) as alias
		set itemPath to quoted form of POSIX path of theItem
		set fileName to name of theItem
		set theFolder to POSIX path of (container of theItem as alias)
		set zipFile to quoted form of (fileName & ".zip")
		do shell script "cd '" & theFolder & "'; zip -x .DS_Store -r0 -P '" & final_pass & "' " & zipFile & " ./'" & fileName & "'"
	end repeat
end tell

Thanks Cdthomer, but unfortunately the script does not work.
I'll look, but I lack a lot of experience with AS...

But, I suspect that there must be a bug with the two variables “theItems” and “theItems”.

Thanks again... :wink:

You can skip all of the AppleScript by using Keyboard Maestro's For Each action on a Finder selection, which greatly simplifies things.

The attached macro seems to work for me. It uses a loop to go through every file in the selection, and then just runs the zip command on each selection. It then shows a list of all files that were zipped during the operation.

zip with passwords.kmmacros (10 KB)

Macro screenshot

Note that passwords on zip files don't make the archive truly secure. For that, you'd want to encrypt each archive (-e), but that requires a password be provided at time of encryption, which is pretty much impossible to do in a Keyboard Maestro macro.

-rob.

1 Like

Thanks Griffman, I'll take a look at your macro.

My need was mainly to compress in .zip a selection from the Finder with a series of files or folders with the entry of a password each time (I do not want to automatically enter the password) and place the compressed file in the same directory as the original files...

Are you saying you want each zip file to have a different unique password? If so, modify the macro to move the password request prompt inside the loop instead of outside the loop. As it is now, it will apply the one password to all zipped files. But that's the same way the AppleScript would have worked: Enter the password once, then apply it to the selection of files.

That's exactly what the macro will do.

-rob.

Thanks again for the macro which I dissected as best as I could understand: very interesting. Unfortunately, I'm reaching my limits...
I must not have been clear enough in my explanations. Sorry.
Here is a small diagram.

My need seems very basic to me. I took a look at BetterZip, but buying a license just for that seems silly to me. In any case, I understand that KM and/or AS can carry out this procedure perfectly... :wink:

I'm sorry; the way you described the issue, and the AppleScript you posted, indicated that you wanted to zip each and every item in the selection into its own archive. That's what the AppleScript would do, so that's what the first version of my macro did.

If you just want to zip a selection into one archive, it's possible, but it wasn't actually all that easy. In theory, the zip command can take a list of files and folders as arguments. My revised macro built such a list, and structured the command properly—it ran fine in Terminal. But in Keyboard Maestro, it would generate an error about a file not found.

In the end, I had to write the list of files to a temporary file, then process that file with zip, in order to get this to work. But this seems to work, at least in my testing.

zip with passwords.kmmacros (15 KB)

Screenshot of macro

NOTE: This will not create an expanded folder with the name you want. I don't think such a thing is possible with zip, perhaps it is but it would take a lot of research time to figure it out, even if it is. The archive will expand into a folder and subfolders representing the full path of the original items.

In addition, I had trouble expanding these zip archives in Finder—something about the password seems to mess them up. They expand fine using Terminal (unzip archive.zip), but not in Finder.

But even getting to this point took 90 minutes, and that's about all the time I have to give to this particular question. From here, others will have to chime in, and/or you'll have to do some digging for further help.

-rob.

Hi rob, your script works perfectly.

I would like to thank you very, very warmly and after studying the macro, I admit that I do not understand everything.

But, I think I understood that above all, it was necessary to establish a strategy, a plan, an algorithm in order to put the actors in place and then apply the actions. I also realize that what repels me the most is the perfect knowledge of the syntax imposed by the code!...

I'm embarrassed to see that it took you so much time to work: if you have graphic design work, I could hope to help you with this task, if necessary, in exchange... :wink:

Thanks again Rob.

No problem, I always learn something writing these things, so it's not time wasted (and I like to help when I can!). Glad it's working for you!

-rob.

1 Like

I would like to take this opportunity to wish you a very happy New Year’s Eve!... :wink: