11 Replies Latest reply: May 7, 2011 10:53 AM by twtwtw
Level 6 Level 6 (11,035 points)
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
  • Camelot Level 8 Level 8 (46,295 points)
    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.
  • Level 6 Level 6 (11,035 points)
    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.
  • Jacques Rioux Level 4 Level 4 (2,545 points)
    Hi,

    It's possible

    on open (theItems)
    repeat with tfile in theItems
    set f to POSIX path of tfile
    if f ends with "/" then set f to text 1 thru -2 of f
    do shell script "/usr/bin/zip -P 'mypassword' -e" & " " & (quoted form of (f & ".zip")) & " " & quoted form of f
    end repeat
    end open
  • Level 6 Level 6 (11,035 points)
    I tried the scrip below, but I got the error:
    zip error: Invalid command arguments (stderr is not a tty)

    Am I missing something?
  • Jacques Rioux Level 4 Level 4 (2,545 points)
    Ok,

    Replace the *do shell script* by this
    do shell script "/usr/bin/zip -P 'mypassword' " & (quoted form of (f & ".zip")) & " " & quoted form of f
  • Level 6 Level 6 (11,035 points)
    Works, but when I unzip, it's nested inside of too many folders:
    Volumes/MyServer/MyFolder/Myfile.zip inside of
    Volumes/MyServer/MyFolder/
    --so the full path "Volumes/MyServer/MyFolder/Volumes/MyServer/MyFolder/Myfile.zip"

    Message was edited by: Kermy812

    Message was edited by: Kermy812
  • Tony T1 Level 6 Level 6 (8,740 points)
    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
  • Jacques Rioux Level 4 Level 4 (2,545 points)
    Hi,

    Kermy812 wrote:
    Works, but when I unzip, it's nested inside of too many folders:


    The option -j is what you need.

    "/usr/bin/zip -P 'mypassword' -j "
  • Tony T1 Level 6 Level 6 (8,740 points)
    Ah! I missed the -j flag when I looked at the man page (making my suggestion almost worthless!).
  • James Meiss Level 1 Level 1 (5 points)

    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.

  • twtwtw Level 5 Level 5 (4,900 points)

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

     

    on open theItems

                        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 script cmd

                        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)