Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Zipping with terminal

Hi - new to applescript
I want to make an applescript droplet that will compress it into an encripted zip and always give it the same password. Here's what I have so far, but it's not working for me:

on open ( theItems )
tell application "Finder"
set fileAlias to the selection as alias
set fileName to name of fileAlias
end tell

tell application "Terminal"
set theDirectory to "cd" & theItems
set shellcommand to "zip -e" & " " & fileName & ".zip" & " " & fileName & "password" & return & "password" & return
set result to do shell script shellcommand
end tell
end open

3.33 GHz 6-Core Intel Xeon, Mac OS X (10.6.7), 32 GB 1333 MHz DDR3

Posted on Mar 31, 2011 8:27 AM

Reply
11 replies

Mar 31, 2011 10:04 AM in response to Merged Content 1

Well, there are several problems with your script. It would help if you posted what error message you were getting since it's not clear which problem is tripping you up.

For one:

set fileAlias to the selection as alias


This always returns a list. Even if there's just one item selected (you get a list of one item). You need to decide how you want to deal with that. Do you want to combine each file into one .zip archive? create one archive per file? something else?

Even more bizarrely you're asking the Finder for the selection, even though you're in an open handler that's passed a list of files. Which do you want? Do you want to compress the selected items? or the items dropped on the app icon?

Secondly:

set theDirectory to "cd" & theItems


Well, theItems is a list of items dropped on the app. I'm not sure what use there is in creating a new text object that combines the characters 'cd' with the paths of the dropped files, especially since you don't actually use this variable at an other point in your script.

Then:

set shellcommand to "zip -e" & " " & fileName & ".zip" & " " & fileName & "password" & return & "password" & return


This makes no sense. For one, according to man zip, -e tells zip to encrypt the archive using the password that it prompts for. There's nothing that tells it to take a password from the command parameters. Even if it did your string concatenation is off, particularly:

... & fileName & "password"


Assuming fileName was 'some.html" (which it won't be, but just go along with it for now), this will result in "some.htmlpassword". There's no way that's right. At the very least there should be a space preceding the 'password' string, so you'd end up with "some.html password", but that still doesn't make much sense given the fact that zip doesn't take a password from the command line parameters anyway.

Then there's:

& return & "password" & return


You cannot embed return characters in a shell command like this.

and finally, for now:

set result to do shell script shell command


You're using do shell script which executes the command directly. So why is this in a tell application "Terminal" block? Nothing here requires the use of Terminal.app, except, maybe, for prompting for the encryption password. If you want to use Terminal.app to run this then use do script rather than do shell script.

So all in all it's not surprising that your script doesn't work 🙂

There are some clarifications needed regarding which file(s) to action before anyone can provide a solution.

Apr 4, 2011 12:24 PM in response to Camelot

I guess I have no clue what I'm doing. What I'm trying to do is make a droplet that will compress my file or folder into a encrypted zip file. I don't want to be prompted to enter a password either. I want the password hardcoded into the droplet.

Basiscally, as if I typed this in Terminal:
cd /Volumes/MyServer/folder/email {return}
zip -e archive.zip MyFile.pdf {return}
mypassword {return}
mypassword {return}

Any assistance would be appreciated.

Apr 8, 2011 2:33 PM in response to Merged Content 1

Try:
<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #E6E6EE;
overflow: auto;">
do shell script "cd $(dirname " & quoted form of f & ") ; /usr/bin/zip -P 'mypassword' " & (quoted form of (f & ".zip")) & " $(basename " & quoted form of f & ")"</pre>

and the file will not be nested in any folders
Tony

May 7, 2011 10:04 AM in response to Tony T1

So it seems that a reasonable script to do this is:


tell application "Finder"

set theItem to selection 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 (theFolder & fileName & ".zip")



display dialog "Enter Password" default answer "" with hidden answer

set the passwd to the text returned of the result


do shell script "zip -P " & passwd & " -rj " & zipFile & " " & itemPath

end tell


I guess the main problems with this are:

1) It doesn't work with multiple files selected

2) It encrypts the .DS_Store file inside a folder, if a folder is selected

3) It uses zip, which is not a very secure encryption method.

May 7, 2011 10:53 AM in response to James Meiss

Following up on James, I think this is the droplet you want:


on opentheItems

repeat with thisItem in theItems

set itemPath to quoted form of (POSIX path of thisItem)

tell application "Finder"

set fileName to name of thisItem

set parentFolder to POSIX path of (container of thisItem as alias)

end tell

set zipFile to quoted form of (parentFolder & fileName & ".zip")



display dialog "Enter Password" default answer "" with hidden answer

set the passwd to text returned of the result


set cmd to "zip -P " & passwd & " -rj " & zipFile & " " & itemPath & " -x *.DS_Store"


do shell scriptcmd

end repeat

end open


this removes the DS-Store files and handles multiple dropped items. Note, though, that the password you use may be clearly visible to anyone who has access to your machine and wants to look (it's passed to zip as plain text, and may appear in command histories or system logs)

Zipping with terminal

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.