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

10.3: Use a Folder Action script for easier zip compression Desktop
While Panther's Finder's contextual menu allows Zip compression of files with its "Create Archive of ..." this still requires bringing up the contextual menu, selecting the option .. and then 'cleaning up' the original file and newly created archive.zip. As Panther has the attached Folder Actions, I figured this could be used to shortcut the process and consolidate the file/archive within one place. Luckily "Sal from Cupertino" picked up on my query in this regard, and provided the following script to compile with the AppleScript Script Editor.

[robg adds: Read the rest of the hint for the script ... and I think "Sal from Cupertino" is probably Sal Soghoian, the AppleScript Product Manager at Apple (at least, that's what he was a few years back).]


on adding folder items to this_folder after receiving these_items
  tell application "Finder"
    if not (exists folder "Done" of this_folder) then
      make new folder at this_folder with properties 
        {name:"Done"}
    end if
    set the destination_folder to folder "Done" of 
      this_folder as alias
    set the destination_directory to POSIX path of 
      the destination_folder
  end tell
  repeat with i from 1 to number of items in these_items
    set this_item to item i of these_items
    set the item_info to info for this_item
    if this_item is not the destination_folder and the 
      name extension of the item_info is not in 
      {"zip", "sit"} then
      set the item_path to the quoted form of the 
        POSIX path of this_item
      set the destination_path to the quoted form of 
        (destination_directory & (name of the 
          item_info) & ".zip")
      do shell script 
        ("/usr/bin/ditto -c -k -rsrc --keepParent " & 
          item_path & " " & destination_path)
    end if
  end repeat
end adding folder items to
NOTES:
  • Create a "Done" folder in the attached folder and either move the file to process there or have the shell compress to that location. This will prevent the Folder Action from triggering again when the archive is created.

  • The "info for" command returns a record of an item's property. One of those properties is its "name extension". Use this property instead of checking the name to determine if a name extension exists or is the correct type.
Having placed the compiled script into /Library -> Scripts -> Folder Action Scripts and then using the contextual menu to attach the Folder Action to an empty folder, you then have the one place to store the original and zipped copy. Keeps the Desktop neat and saves time

Thanks Sal and Applescript!
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[20,952 views]  

10.3: Use a Folder Action script for easier zip compression | 8 comments | Create New Account
Click here to return to the '10.3: Use a Folder Action script for easier zip compression' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Undoing this, cpio, and tar archives
Authored by: SOX on Nov 24, '03 11:49:10AM
HFS files have meta-data and resource forks that are lost during common unix archive operations like cpio, tar, gzip. Panther's new archive maker is HFS aware but it's not compatible with Stuffit or tar-zip.

Fortunately you can use the ditto to make both PKZIP (windows) and compressed Unix archives that preserve the resource forks! There are three types of archives you can produce: pkzip, cpio-zipped, and tar-zipped. The first two are easy. The latter is tricky to create and to undo so I dont reccomend it but the method is instructuve so I'll describe it anyhow.

To create a pkzip archive of "some_folder" type


ditto -c -k -X --rsrc some_folder  some_folder.zip
Alternatively to create a compressed cpio archive type

ditto -c -X --rsrc some_folder some_folder.cpio
by the way, although most people are more familiar with tar-zip, a cpio archive is a univerally compatible unix archive, and if anything is more versatile. (see man pages for cpio and pax for info).

if you send this to a unix or to a windows person they can open these using cpio and pkzip, respectively. For example, on any unix machine (besides OS X) to open a cpio archive type:


cpio -i -I -z some_folder.cpio 
and it will unarchive it in the current folder. In this folder you may now observe that for each file, "some_file", that has meta-data there will be an associated file named ._somefile which contains all the meta-data and resource forks. This is called appledouble format. Note that not all files have meta-data so you may not see this appleDouble format file for every regular HFS file.

On an OS X machine, you can extract the archive using cpio also, and you will see these same appleDouble files. However when you do this on an HFS file system it fails to restore the resource forks to the right files. Therefore dont use cpio to unarchive on an HFS file system. (aside: it does restore the resource forks on mac OS if you have a UFS partition or a mounted NFS drive however).

On an HFS system you recover the resource forks from the cpio archive using ditto again:


ditto -x --rsrc some_folder.cpio  some_folder_destination
To recover the resource forks on a pkziped file you have to use stuffit expander. Strangely, ditto does not work in this case--I'm guessing this is a bug, since it should work.

ADVANCED STUFF
You dont really need to read further, but you might be interested is seeing how to use these tricks to make a tar gzip archive that has resource forks! Well its not pretty and its hard to undo. But in case you want to here is a script: save the following into file called tar_hfs.sh and make it executable (chmod a+x).


#!/bin/bash

 TEMPDIR=`mktemp -d`  || exit 1
 TEMPFIFO=`mktemp -p $TEMPDIR`  &&  mkdir ${TEMPDIR}/$1 && {

   ditto --rsrc -c -X $1 $TEMPFIFO  
   ditto -x $TEMPFIFO ${TEMPDIR}/$1

   (cd $TEMPDIR && tar -cz  $1)
}
exit 0
echo rm -fr $TEMPDIR
to use this on "some_folder" type

tar_hfs some_folder > some_folder.tgz
Again this saves the meta-data in appleDouble format. The method I use is a trick that uses ditto to first create a cpio archive then uses ditto to unpack it in a resource-unaware manner, leaving the ._somefile meta_data. Then I tar this. Note this trick winds up as an intermediate step, making a full copy of the folder you are trying to archive (it cleans up after itself), so dont do this on a nearly full disk! Also note that this script is for illustration purposes only and is not very general: it assumes that you are in the directory containing some_folder; it wont quite work if the you give it a full path to some_dir on the command line.

Okay now how do I un-tar-zip this and restore the resource forks. To do this you need to un-tar-zip it then create a cpio archive, then use ditto to unarchive this. Yuck, and this is left as an excersize to the reader. But better yet just stick with the cpio archives.

Lastly I'll mention one final mystery that maybe some reader can solve. I ran into in figuring this out. If you look at the script above, I called one of the intermediate files TEMPFIFO. This is because in my original script i used a unix fifo instead of a real file and backgrounded the first ditto command, figuring I could save some space and time. Oddly, about every ten or so tries the first "ditto" command issues an error message about not being able to complete the fifo. I have no idea why that error would happen. anyone know why?

Al

[ Reply to This | # ]

Undoing this, cpio, and tar archives
Authored by: SOX on Nov 24, '03 11:55:08AM

oops a typo

Alternatively to create a compressed cpio archive type

ditto -c -X -z --rsrc some_folder some_folder.cpio



[ Reply to This | # ]
10.3: Use a Folder Action script for easier zip compression
Authored by: davidcrickett on Nov 24, '03 12:31:40PM

You can just make a shortcut for 'Create Archive'. I use alt+z...

---
davidcrickett



[ Reply to This | # ]
10.3: Use a Folder Action script for easier zip compression
Authored by: mrHelf on Nov 25, '03 04:54:55PM

How do you do that? Went into System Prefs and Keyboard&Mouse and tried to add a shortcut for that, but I must be missing something here. Can't get this working! What am I doing wrong?

mrHelf



[ Reply to This | # ]
Err.. I works. My bad. Thanks for the tip!!
Authored by: mrHelf on Nov 25, '03 04:57:26PM

had to log out for it to take effect.
mrHelf



[ Reply to This | # ]
10.3: Use a Folder Action script for easier zip compression
Authored by: GusDoeMatik on Mar 31, '05 03:01:57PM
Thanx for the script. But is there anyway to modify this script? So that the file being zipped doesn't have two extensions? example: desert.jpg should look like this- desert.zip NOT desert.jpg.zip. The only reason I even care is because windows don't read double extensions. If it did I wouldn't even bother asking you if there was a way to modify this script.

[ Reply to This | # ]
10.3: Use a Folder Action script for easier zip compression
Authored by: photojojo on Jul 14, '05 07:41:00PM

Any idea on how to eliminate the '._' file that is created when zipping a file? I don't see the file in Mac os but the file is there when the zip is uncompressed on a windows box. My windows recipients are annoyed and I'm baffled!



[ Reply to This | # ]
10.3: Use a Folder Action script for easier zip compression
Authored by: photojojo on Jul 15, '05 01:15:26PM

Hi again,

Is there any way of preventing the apple double format (the '._' file) from appearing when windows users uncompress a file that I archived on a Mac?



[ Reply to This | # ]