Previous 1 2 Next 19 Replies Latest reply: Nov 14, 2012 10:18 PM by Max NY
Max NY Level 1 Level 1 (0 points)

Dear MacUnix Gurus:

 

Need your help on AppleScript and Commandline.

 

How to create an AppleScript droplet which will trigger a BASH command which, preserving Resource Forks and the extended attributes, compresses the files within a folder individually.

 

Many Thanks!

 

MaX NYC

  • Jacques Rioux Level 4 Level 4 (2,695 points)

    Hi,

     

    If you want to archive in the same folder, you can use a script like this :

    ----------------------------------

    set tFolder to choose folder

     

    tell application "Finder" to set theseFiles to (document files of tFolder whose name extension is not "zip") as alias list

    repeat with i in theseFiles

          my compress_file(POSIX path of i)

    end repeat

     

    on compress_file(tFile)

          set tFile to quoted form of tFile

          do shell script "/usr/bin/ditto -c -k -rsrc  " & tFile & space & tFile & ".zip"

    end compress_file

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

    To do this properly you have to use ditto (the zip utility does not handle extended attributes or resource forks correctly).  from man ditto the correct command is:

     

    ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip

     

    ditto will preferentially lump all files into a single archive, so if you want separate archives you're going to have to loop through the items in the dropped folder.  That would look something like this (save it as an application to make the droplet):

     

    on open theFolders

      -- items are passed in as a list, so get the first item

              set theFolder to POSIX path of first item of theFolders

              tell application "System Events"

                        set pathList to POSIX path of (every disk item of folder theFolder whose visible is true)

              end tell

              repeat with thisItem in pathList

                        do shell script "ditto -c -k --sequesterRsrc --keepParent " & quoted form of thisItem & " " & quoted form of (thisItem & ".zip")

              end repeat

    end open

  • Mark Jalbert Level 5 Level 5 (4,595 points)

    Do you really need   --sequesterRsrc   if you are zipping a single file?

  • Max NY Level 1 Level 1 (0 points)

    Appreciare U All! Try!

     

    MaX NYC

  • Max NY Level 1 Level 1 (0 points)

    Hi! All!

     

    I tried the advices, both from Jacques and TwTwTw, to save the suggested Scripts into Applications into Droplets. Yet! Both failed! Error: Unknown, invalid tokens.

     

    In the old Stuffit Standard 8, there was an application called: DropStuff. It is rather simple: Drag a folder over DropStuff icon, a compressed “.zip” or “.sit” file would be conveniently generated!

     

    Is there any way? Create an AppleScript Droplet, drag a folder over it! Voila! All of the files inside the folder will be compressed individually; Resource Forks and Data Forks and all Extended Attributes would also all be preserved.

     

    Why compress files inside one folder individually, not into one single zipped file? After archiving closed job files, before removing them from online storage permanently, I want to use directory tree cataloger software to “record” the “snapshot” of the folder structure and file content, for future reference..

     

    To Mark: In order to preserve the flaky Resource Forks of old-style Adobe Type-one PostScript Fonts, “--sequesterRsrc” option is essential for one single file.

     

    Many Thanks to All Of You! Please give me further pointers.

     

    MacX NYC,

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

    What OS are you on?  I tested my code in Lion and it works flawlessly as a droplet (no errors of any sort). How did you copy the text into the script editor?  the 'invalid token' bit might be html gremlins getting mixed in during copy/paste.

  • Max NY Level 1 Level 1 (0 points)

    OS10.6.8

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

    There's nothing in my script that would break in 10.6.8, so I'm almost positive it's a copy/paste issue.  try copying into a plain-text text editor (like TextWrangler) and cleaning out any non-standard characters before putting it in the script editor.

  • Max NY Level 1 Level 1 (0 points)

    Thank U! Maestro TwTwTw!

     

    It worked!

     

    However! One kink! The individually compressed  files stay inside the same folder, alongside with the original uncompressed files. I have to separate the sets of files manually, which is time-consuming.

     

    Is there anyway to place the set of individually compssed files in a separate folder, named "FolderName.1"? Right next to the original folder?

     

    Many Thanks!

     

    MacX NYC

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

    easy enough (added a handler to modify the text path and changed the destination path in the shell script):

     

    on open theFolders

      -- items are passed in as a list, so get the first item

              set theFolder to POSIX path of first item of theFolders

              tell application "System Events"

                        set pathList to POSIX path of (every disk item of folder theFolder whose visible is true)

              end tell

              repeat with thisItem in pathList

                        set destingationPath to modifyPath(thisItem)

                        do shell script "ditto -c -k --sequesterRsrc --keepParent " & quoted form of thisItem & " " & quoted form of (destingationPath & ".zip")

              end repeat

    end open

     

    on modifyPath(oldPath)

              set {oldTID, my text item delimiters} to {my text item delimiters, "/"}

              set pathBits to text items of oldPath

              set item -2 of pathBits to item -2 of pathBits & ".1"

              set newPath to pathBits as text

              set my text item delimiters to oldTID

              return newPath

    end modifyPath

  • Max NY Level 1 Level 1 (0 points)

    Thanks! Maestro TwTwTw:

     

    It works! U rock!

     

    MacX NYC

  • Max NY Level 1 Level 1 (0 points)

    Dear TwTwTw:

     

    Thanks! It works as what is intended.

     

    What is happening: The original folder retains its name; the new folder containing each individually compressed files gets the name with ".1" suffix.

     

    Is that possible? What if, the original folder becomes renamed with ".o" suffix; the new folder gets to keep the name.

     

    Appreciate it! Cheers!

     

    MacX NYC

  • Max NY Level 1 Level 1 (0 points)

    Hi! TwTwTw:

     

    I have been running your Scipt fine! Suddenly! "AppleEvent Time Out" Error popped up! The whole thing halted!

     

    Any work around? Please!  Running on OSX.10.6.8, fully updated.

     

    Happy ThanksGiving! Cheers!

     

    MacX NYC

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

    can you tell me more about what you were doing, or at least what you were doing differently than the times it worked before?  different kinds of files, bigger folders, or anything that might point to what's giving the timeout.  My guess is it will be ditto timing out from some extra-large file or folder.  if that's the case, there are two workarounds.

     

    1. Release all the processes to work independently and concurrently.  You wouldn't have to worry about timeouts, (the script would ignore responses from the shell, and just fire everything off and quit), but if you have a lot of files in a folder you might hit a resource limit of some sort - I have no idea how many instances of ditto can be running at the same time.  That would look like this (only change is I tacked a string onto the end of the shell command):

     

    do shell script "ditto -c -k --sequesterRsrc --keepParent " & quoted form of thisItem & " " & quoted form of (destingationPath & ".zip") & " &> /dev/null &"

     

    Alternately, you could put the DSS inside a timeout block with with a large value.  That would give ditto more time to do its work before timing out.  That looks like this:

     

    with timeout of 3600 seconds -- set it arbitrarily for one hour.  Won't use all that time, but will have it available.

              do shell script "ditto -c -k --sequesterRsrc --keepParent " & quoted form of thisItem & " " & quoted form of (destingationPath & ".zip")

    end timeout

Previous 1 2 Next