Sequentially create archives from multiple folders?

Hello everyone (and sorry if my english turns out not to be that good :] ),

I would like to know if it is possible for an Automator newbiee like myself to create an action that would take several folders and zipped them one by one and preserve their names? Like “folder001” → “folder001.zip”, etc.?

So far I can take a bunch of folders and archive them in a single archive. I’m not very familiar w/ Apple Script or stuff like that :[

Also, there are hundreds of folders I would like to archive this way and it seems to me that any archive made with Automator has a generic name “archive” or, worse, I would have to rename every archive manually (via “Show action when run”) if I want to avoid this.

Could Automator take an advantage of Finder’s “Create Archive” command?

Thank you!

Hi B.

you can use this droplet (save the script as application)

property deleteOriginal : false

on open theseFolders
	repeat with oneFolder in theseFolders
		if (folder of (info for oneFolder)) then
			tell (POSIX path of oneFolder) to set {source, destination} to {it, it's text 1 thru -2 & ".zip"}
			do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of source & " " & quoted form of destination)
			if deleteOriginal then do shell script "rm -r " & quoted form of source
		end if
	end repeat
end open

or this version for a Run AppleScript action in Automator

property deleteOriginal : false

on run {input, parameters}
	repeat with oneFolder in input
		if (folder of (info for oneFolder)) then
			tell (POSIX path of oneFolder) to set {source, destination} to {it, it's text 1 thru -2 & ".zip"}
			do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of source & " " & quoted form of destination)
			if deleteOriginal then do shell script "rm -r " & quoted form of source
		end if
	end repeat
	return input
end run

If you want to delete the original folder(s) right after making the archive, set the deleteOriginal property to true

Stefan, thank you very much for your prompt reply.

This is exactly what I was looking for, it’s very helpfull. Thank you one more time!

Best regards,
B. C.

Nice Script, but it only works on folders.
I tried to make it also work for files but I’m getting an error ‘cannot get item’

property deleteOriginal : true

on open theseItems
	repeat with oneItem in theseItems
		if (item of (info for oneItem)) then
			tell (POSIX path of oneItem) to set {source, destination} to {it, it's text 1 thru -2 & ".zip"}
			do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of source & " " & quoted form of destination)
			if deleteOriginal then do shell script "rm -r " & quoted form of source
		end if
	end repeat
end open

You can’t just replace “folder” with “item” in the if statement. If you want it to act on files either leave out the if statement (it will act on anything) or make it “if not (folder of (info for oneItem)) then” which will act only on files but not folders.

Hello,
I tried to modify this script to work on files and archive the one by one. I have many files in many folders that i want to make sitx for each file. First i tried to make it work with your indications as .zip but it didnt work and nothing happens, no error message. This is my modified script:

property deleteOriginal : false

on open theseFolders
	repeat with oneItem in theseFolders
		if not (folder of (info for oneItem)) then
			tell (POSIX path of oneItem) to set {source, destination} to {it, it's text 1 thru -2 & ".zip"}
			do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of source & " " & quoted form of destination)
			if deleteOriginal then do shell script "rm -r " & quoted form of source
		end if
	end repeat
end open

Can you tell me what am i doing wrong?

Hi,

if the source is a folder it’s sufficient to add the .zip suffix.
If the source is a file, you have to remove the extension and replace ist with .zip


property deleteOriginal : false

on open theseFolders
	repeat with oneItem in theseFolders
		set {name:Nm, name extension:Ex, folder:Fo, package folder:Pa} to info for oneItem
		set source to POSIX path of oneItem
		if Fo and not Pa then
			set destination to text 1 thru -2 of source & ".zip"
		else
			if Ex is missing value then set Ex to ""
			if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
			set destination to text 1 thru ((offset of Nm in source) - 1) of source & Nm & ".zip"
		end if
		do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of source & " " & quoted form of destination)
		if deleteOriginal then do shell script "rm -r " & quoted form of source
	end repeat
end open

The ideea was to archive each file from a folder i drag in a sitx archive. After some research i found this script that works like a charm:

property type_list : {"SITX"}
-- since file types are optional in Mac OS X, 
-- check the name extension if there is no file type 
-- NOTE: do not use periods (.) with the items in the name extensions list 
-- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"} 
property extension_list : {"sit", "sitx", "zip", "rar", "tar"}

-- This droplet processes both files or folders of files dropped onto the applet 
on open these_items
	repeat with i from 1 to the count of these_items
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false and the file type of the item_info is not in the type_list) or (the name extension of the item_info is not in the extension_list) then
			process_item(this_item)
		end if
	end repeat
end open

-- this sub-routine processes folders 
on process_folder(this_folder)
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as text) & (item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false and the file type of the item_info is not in the type_list) or (the name extension of the item_info is not in the extension_list) then
			process_item(this_item)
		end if
	end repeat
end process_folder

-- this sub-routine processes files 
on process_item(this_item)
	-- NOTE that the variable this_item is a file reference in alias format 
	-- FILE PROCESSING STATEMENTS GOES HERE 
	
	with timeout of 7200 seconds
		
		tell application "DropStuff"
			--display dialog currentFolder buttons {"Inainte de STUFF"} default button 1
			
			stuff this_item with delete originals -- dont recompress = true
			
			--display dialog newFile buttons {"Dupa de STUFF"} default button 1
		end tell
		
	end timeout
	
	tell application "Finder"
		move this_item to trash
		empty trash
	end tell
	
end process_item

Thank a lot for your fast reply!
George

Edit: In fact when i tested it with a folder with 3 files it worked, but now when i try to drag a folder with many filed i get this error: “File some object wasn’t found”…

if you

stuff this_item with delete originals

the original file will be deleted

Therefore theFinder cannot

 move this_item to trash

:wink:

Note: I recommend to use always the .zip format to compress files.
.sitx is a proprietary format of StuffIt and needs always a third party tool to decompress the files
.zip is a standard format and can be handled by OS X itself

Yes ofcourse, it makes sense :). Thanks a lot Stefan. I prefer to stuff because it compress better than zip and i am the only one who will use these archives.

George