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

Command: Compress Files in A Folder Individually

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

Posted on Sep 2, 2012 8:02 PM

Reply
19 replies

Sep 3, 2012 7:51 AM in response to Max NY

Hi,


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

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

settFoldertochoose folder


tellapplication "Finder" tosettheseFilesto (document filesoftFolderwhosename extensionisnot "zip") asalias list

repeatwithiintheseFiles

mycompress_file(POSIX pathofi)

endrepeat


oncompress_file(tFile)

set tFile to quoted form of tFile

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

endcompress_file

Sep 3, 2012 8:42 AM in response to Max NY

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 opentheFolders


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

Oct 2, 2012 8:58 AM in response to Max NY

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,

Oct 2, 2012 12:10 PM in response to twtwtw

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

Oct 2, 2012 12:41 PM in response to Max NY

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


on opentheFolders


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

Oct 2, 2012 2:01 PM in response to twtwtw

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

Nov 10, 2012 7:26 AM in response to Max NY

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

Nov 11, 2012 11:29 AM in response to twtwtw

Howdy? TwTwTw:


Thanks for your effort!


>>

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.

>>


Could you please elaborate this part a bit!


If I use the Script sparingly, it works fine. If I use it continuously for a few hours in a row, "`SystemEvents' TimeOut" happens.


Some Admins complained that there might be a bug in AppleEvents TimeOut Value in OSX.10.6.8?!


Happy ThanksGiving!


MacX NYC

Command: Compress Files in A Folder Individually

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