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

Use an AppleScript droplet for easier compression System
I came across some hints on using the ditto command to create .zip files and people were doing it with folder action scripts. I figured why not just make it a droplet? Copy and paste the following into Script Editor and save it as an application:
on open (theItems) --receive items dropped onto droplet as a list
  --incase you're trying to compress something really big on your Rev A iMac:
  with timeout of 1000000 seconds
    try
      tell application "Finder"
        --repeat the command to compress each item as an individual archive
        repeat with oneItem in theItems
          --used to extract the name and location of the file
          set itemProp to properties of oneItem
          --where the file is
          set itemPath to quoted form of POSIX path of oneItem
          --where the compressed file should end up
          set destFold to quoted form of POSIX path of 
            (container of itemProp as alias)
          --what the name of the file is
          set itemName to name of oneItem
          --do it, do it now
          do shell script 
            ("ditto -c -k -X --rsrc --keepParent " & itemPath & 
              " " & destFold & "'" & itemName & "'" & ".zip")
        end repeat
      end tell
    on error errmsg
      --should anything go wrong let the user know
      display dialog errmsg
    end try
  end timeout
end open
[robg adds: This works as described. It does not, however, create one archive from multiple dropped files. Instead, each dropped file will create its own zip file.]
    •    
  • Currently 2.20 / 5
  You rated: 4 / 5 (5 votes cast)
 
[12,477 views]  

Use an AppleScript droplet for easier compression | 4 comments | Create New Account
Click here to return to the 'Use an AppleScript droplet for easier compression' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use an AppleScript droplet for easier compression
Authored by: macshome on Sep 28, '04 10:53:20AM

Or just ctrl-click and select "Make Archive" to make a fork-preserving zip of anything selected in 10.3.+.



[ Reply to This | # ]
Use an AppleScript droplet for easier compression
Authored by: dweebert on Sep 28, '04 11:43:29AM

I wasn't actually aware that ditto could be used this way.

I'm not sure why you are putting single quotes around the itemPath. The quoted form of a POSIX path should do the necessary quoting for you. I think that the way it is, there might be problems if the filename actually includes a "'" character.



[ Reply to This | # ]
Use an AppleScript droplet for easier compression
Authored by: Graff on Sep 28, '04 11:45:46AM

Here's a script which will combine multiple files into one archive. It also will attempt to get a unique name for the final archive in case there is already a file with the chosen archive name.

on open these_items
  tell application "Finder"
    set tempFolderPath to path to "temp" as string
    set tempFolderName to my UniqueName(tempFolderPath, "Archive")
    
    if (tempFolderName is not anything) then
      try
        set theFolder to choose folder with prompt "Choose where the archive should be placed:"
        
        display dialog "Enter a name to use for the archive:" default answer "Archive.zip"
        set theName to text returned of the result
        set archiveName to my UniqueName(theFolder, theName)
        
        if (archiveName is not anything) then
          set tempFolder to make new folder at tempFolderPath with properties {name:tempFolderName}
          repeat with i from 1 to the count of these_items
            set this_item to item i of these_items
            duplicate this_item to tempFolder
          end repeat
          
          set theSource to quoted form of (POSIX path of (tempFolder as alias))
          set theDest to quoted form of ((POSIX path of theFolder) & archiveName)
          do shell script "/usr/bin/ditto -c -k --sequesterRsrc " & theSource & " " & theDest
        else
          display dialog "The archive can not be created: too many archive files with similar names."
        end if
        delete tempFolder
      end try
    else
      display dialog "The archive can not be created: too many temp files with similar names."
    end if
  end tell
end open

on UniqueName(baseFolder, itemName)
  tell application "Finder"
    set basePath to baseFolder as string
    set savedDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "."
    set theTextItems to text items of itemName
    if ((count of theTextItems) > 1) then
      set baseName to (items 1 thru -2 of theTextItems) as text
      set theExt to "." & (item -1 of theTextItems)
    else
      set baseName to itemName
      set theExt to ""
    end if
    set AppleScript's text item delimiters to savedDelims
    
    set tempName to itemName
    set i to 0
    repeat while ((folder (basePath & tempName) exists) and (i < 1000))
      set i to i + 1
      set tempName to baseName & i & theExt
    end repeat
    if (i < 1000) then
      return tempName
    else
      return anything
    end if
  end tell
end UniqueName



[ Reply to This | # ]
How about encryption?
Authored by: ekc on Sep 28, '04 01:32:28PM

This script would be more powerful if it encrypted the files after compressing them. As it is, it only duplicates functionality that is readily available in the Finder already. (Incidentally, I would use --sequesterRsrc instead of --rsrc, even if it makes the script dependent on Panther.) The OS 9 Finder actually had an Encrypt command, though it only worked on individual files and not folders, if memory serves.

After ditto-ing the file/folder, you could run the zip archive through openssl to encrypt it. Then call srm on the original and zip files to clean things up.

The only snag I can foresee is password management. How do you get AppleScript to request a password without echoing it? How do you then send that password securely to openssl? (I understand the most obvious approach -- put it in the command line -- is prone to eavesdropping, though maybe this problem is overblown?) Could you put it in a keychain, maybe? (On first glance, I could not see a way of doing this, though you can look up keys that are in there already.)

-Ted



[ Reply to This | # ]