AppleScript to Compress Files in Folder Individually

Hi! All!

AppleScript, CommandCompressFilesInFolderIndividually, in Question=>

>>>>>>>>>>

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 destinationPath to modifyPath(thisItem)



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


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 & "Zip"


set newPath to pathBits as text


set my text item delimiters to oldTID


return newPath

end modifyPath

<<<<<<<<<

Hi! Script Gurus:

I have been using the aforementioned AppleScript. One vexing issue: Double Compressions.

Please Help! How to make the Compression Script skip those files which have already been compressed, such as: .zip, .sit, .sitx, gzip, etc.

Thanks gZillion!

Max NY

Posted on Sep 27, 2016 12:51 PM

Reply
6 replies

Sep 27, 2016 1:03 PM in response to Max NY

The simplest solution would be to check if you've got a .zip file before invoking your ditto command:


repeat with thisItem in pathList

tell application "Finder" to set nx to name extension of thisItem

if nx is not in {"zip", "gz", "sitx"} then

set destinationPath to modifyPath(thisItem)

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

end if

end repeat

You can edit the list of extensions to exclude any file types you want.

Sep 28, 2016 3:26 PM in response to Camelot

Hi! I modified it :


>>>>>>

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



tell application "Finder" to set nx to name extension of thisItem



if nx is not in {"zip", "gz", "sit", "sitx"} then




set destinationPath to modifyPath(thisItem)




with timeout of 3600 seconds





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




end timeout



end if


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 & "Zip"


set newPath to pathBits as text


set my text item delimiters to oldTID


return newPath

end modifyPath


<<<<<<<

I tried to drag a folder over the droplet and got an error: "Can not get <<>class nmxt>"?


Help! Please!


Max NY

Sep 28, 2016 9:06 PM in response to Max NY

It fails due to differences between POSIX files and Finder file references. The Finder can't, for some reason, handle this.


There are a couple of approaches to dealing with this. One is to standardize on one format (either POSIX files or Finder references), or manually coerce the data as needed.


Since your script is already built around POSIX files (including the modifyPath() handler and the use of shell commands, I'd recommend with just sticking with POSIX throughout.


Just change the line




tell application "Finder" to set nx to name extension of thisItem

to:


tell application "System Events" to set nx to name extension of disk item thisItem

Sep 29, 2016 11:50 AM in response to Camelot

Dear Camelot! Many Thanks!

Your suggested revision largely works! Caveat:

If the Target Folders contains compressed files, then I have a problem: Those previously compressed files would not be carried to the Destination Folders.

Henceforth! Before I delete the Destination Folders, I must be very much carefully and judiciously manually move those previously compressed files to the Target Folders. It is a very laboriously painstaking process.

This is would be a wonderfully hassle-free scenario: Those previously compressed files in the Target Folders would be carried over to the Target Folders automatically, yet NOT TO BE “compressed” AGAIN.

Help? Please!

Many ThankS!

Max NY

Sep 29, 2016 12:32 PM in response to Max NY

Those previously compressed files in the Target Folders would be carried over to the Target Folders automatically, yet NOT TO BE “compressed” AGAIN

In your original post you stated you wanted to exclude compressed files, but didn't indicate that you wanted to do anything else with them. It's not a big deal, though.


The script is already checking the file type. It's just a matter of changing the logic around the ifnxis notin {"zip", "gz", "sitx"} ... line to perform different actions based on the result. For example:


...

tell application "System Events" to set nx to name extension of disk item thisItem

set destinationPath to modifyPath(thisItem)

if nx is in {"zip", "gz", "sit", "sitx"} then


-- we have a compressed file, so move it

do shell script "/bin/mv " & quoted form of thisItem & space & quoted form of destinationPath

else


-- we have an uncompressed file, so compress it

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

end if

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript to Compress Files in Folder Individually

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