Zipping (Compressing) a File

I am working on a script which requires compressing a file into a zip file (same thing as the Finder’s “Compress” command). I hear that this cannot be done with regular AppleScript, and requires a shell script. I am a novice at shell scripts, and after much trial and error (with code from other sites), I am still unsuccessful.

In short, are there any good way to compress a file (in .zip format) with AppleScript/shell script?

Also look at this post.

Thanks Tom. the cd shell script worked flawlessly (although it did take me a few tries to copy it just right).

Hello,

I found this script here and I thought I could use it to help me compress a bunch of indesign packaged folders…that are stored in one main folder… into archive compressed files.
Problem is, it creates one archive.zip for all the folders. Is there a way to have it make a single archive.zip (with the file name of the original folder) for each folder and its contents?

thanks
babs

Hello

I don’t see any clickable area allowing me to get the described script.
Am I missing something ?

KOENIG Yvan (VALLAURIS, France) mardi 20 août 2013 11:40:51

Hi Yvan!!

Sorry, I should have been more clear…the script is in this post, #2-written by Tom_X.

:slight_smile:

thanks!!!
babs

It seems that this one may fit your needs.

If the script fails to zip one or several items, you will get a text file named “@@@@@@@.txt” on the Desktop.
It will list the description of issued errors.


property report : ""

on run
	display dialog "Choose a file or folders?" buttons {"Cancel", "Folders", "File"} default button 3 with icon note
	
	if button returned of result is "File" then
		choose file with prompt "Create ZIP file of this file:" without invisibles
		get {result}
	else
		choose folder with prompt "Create ZIP files of every file in these folders:" with multiple selections allowed
	end if
	
	open result
end run

on open droppedItems
	set report to ""
	repeat with sourceFile in droppedItems
		try
			my exploreTraite(sourceFile as text, "")
		end try
	end repeat
	
	if report > "" then
		beep
		(path to desktop as text) & "@@@@@@@.txt"
		my writeto(result, report, text, false)
		beep
	end if
	# Don't store the property in the script.
	set report to ""
end open


#=====

on exploreTraite(elem, ptree) (*
elem est un Unicode text
¢ elem is an Unicode text *)
	local cl_
	tell application "System Events" to tell disk item elem to set cl_ to class
	set cl_ to cl_ as text
	
	if cl_ is in {"file package", "«class cpkg»"} or cl_ is not in {"folder", "«class cfol»"} then
		my TraiteUnFichier(elem)
	else if cl_ is in {"folder", "«class cfol»"} then
		my ExploreUnDossier(elem, ptree)
	end if # cl_ is .
end exploreTraite

#=====

on ExploreUnDossier(dossier, ptree)
	local listItems, nomElement, cheminElement, c
	if dossier does not end with ":" then set dossier to dossier & ":"
	tell application "System Events" to set listItems to name of disk items of folder dossier whose visible is true
	repeat with nomElement in listItems
		tell application "System Events" to set c to name of disk item dossier
		my exploreTraite(dossier & nomElement, ptree & c & ":")
	end repeat
end ExploreUnDossier


on TraiteUnFichier(sourceFile)
	# sourceFile is a text path
	tell application "System Events" to tell disk item sourceFile
		set {sourceName, sourceExtension, sourceIsFolder, sourceParent} to {name, name extension, folder, path of container}
	end tell
	
	set sourceParent to POSIX path of sourceParent
	
	# Force the file name to lowercase and replace embedded space chars by "-"
	do shell script "/usr/bin/python -c \"import sys; print unicode(sys.argv[1], 'utf8').lower().replace(' ','-').encode('utf8')\" " & quoted form of sourceName
	
	sourceParent & "/" & result
	if sourceExtension is in {"applescript", "scpt", "scptd", "app"} then
		get result & "_.zip"
	else
		get result & ".zip"
	end if
	POSIX path of result
	try
		do shell script "/usr/bin/zip -qo9r " & quoted form of result & space & quoted form of POSIX path of sourceFile
	on error errMsg number errNum
		set report to report & "Error while creating ZIP file. (" & errNum & ")" & errMsg & return
	end try
	
end TraiteUnFichier

#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		set openFile to open for access file targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeto

#=====

KOENIG Yvan (VALLAURIS, France) mardi 20 août 2013 17:02:41

Hi Yvan,

ahh…OK, at first I couldn’t see what it was doing and now I can…its taking the files inside of the folders and making them into .zips…

What I actually just needed, was the folders as a whole to be zipped. What I am doing is packaging a bunch of indesign jobs…so each folder contains the indesign file, the fonts and links to that job. So i wanted each folder to be compressed with the contents of what was inside each folder?

So inside a main folder I would have Folders that would look something like this:

Job 1 Folder
Job 2 Folder
Job 3 Folder

and when the script ran: I would have this:

Job 1 Folder
Job 1 Folder.zip
Job 2 Folder
Job 2 Folder.zip
Job 3 Folder
Job 3 Folder.zip

or the .zips can go into another folder if prompted …but this is the scenario

Does that make sense???

Thank you so much!!!
babs

It make sense but I see a problem.

Is it possible to have this structure:

Job 1 Folder
one or several subfolders
Job 2 Folder
one or several subfolders
Job 3 Folder
one or several subfolders

It may be an odd asumption but I never used inDesign.

If we may have the structure above we will get :

Job 1 Folder
Job 1 Folder.zip
subfolder 1
subfolder 1.zip
subfolder 2
subfolder 2.zip
Job 2 Folder
Job 2 Folder.zip
subfolder 1
subfolder 1.zip
subfolder 2
subfolder 2.zip
Job 3 Folder
Job 3 Folder.zip
subfolder 1
subfolder 1.zip
subfolder 2
subfolder 2.zip

Complementary question :

which is the better behavior :
the zipped files located with the originals
or
the zipped files located in a dedicated folder.

KOENIG Yvan (VALLAURIS, France) mardi 20 août 2013 19:08:59

Hello,

I see the problem… I wanted to send you a snapshot of the actual contents but couldn’t attach one…so I will type out the actual scenario and I hope it helps…

Main Folder- named “Folders to Compress
Sub Folder named “job 1 Folder
Inside EACH Job # Folder would always be 4 items
Item 1 - the InDesign file named Job 1.indd
Item 2- an instructions txt file named instruction.txt
Item 3 - Folder - named Links
Item 4 - Folder named Document Fonts

Sub Folder named “job 2 Folder
Inside Job 2 Folder would always be 4 items
Item 1 - the InDesign file named Job 2.indd
Item 2- an instructions txt file named instruction.txt
Item 3 - Folder - named Links
Item 4 - Folder named Document Fonts

and so on… so, one thing to keep in mind is, I do NOT need to place the folders for each job collected in any main folder (Folders to Compress) . The script could just ask to choose multiple Folders (Which would be the Job 1 Folder, Job 2 Folder etc… (if that helps)… This way, we can ask it to just zip those folders and not look for any subfolders??? That might help here… since right now, it goes and gets all the sub-folders from the main folder and then gets all the sub folders from each folder, maybe just asking the script to choose the folders, but not go any deeper, could help solve this?

Regarding complimentary question, placing the zipped files in another folder would be great, but if that is too much trouble then the same location is fine…

I hope this helps clarify this a bit more…hard without being able to send you a screen shot…but hopefully it make more sense :wink:
thanks!!!
babs

This edited version is supposed to match your needs.

As is, it creates a folder on the Desktop and store the zip files in it.


property storeInDedicatedFolder : true
(*
true = store the zipped files in the folder zipped on yyyymmdd_hhmmss (example : "zipped on 20130820_220709") on the Desktop
false = store the zipped file in the location of the original
*)

# Two properties used for global storage. They are resetted on exit
property report : ""
property dedicatedFolder : ""

on run
	choose folder with prompt "Create ZIP files of every file in selected folder(s):" with multiple selections allowed
	open result # result is always a list
end run

on open droppedItems
	# initialize two properties in case of preceeding crash
	my nettoie()
	if storeInDedicatedFolder then
		# Create the dedicated folder in which zip files will be stored
		tell (current date) to (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
		tell application "System Events" to set my dedicatedFolder to path of (make new folder at end of (path to desktop) with properties {name:"zipped on " & result})
	end if
	# Main loop
	repeat with sourceFile in droppedItems
		try
			my exploreTraite(sourceFile as text, "")
		end try
	end repeat
	
	if my report > "" then
		beep
		(path to desktop as text) & "@@@@@@@.txt"
		my writeto(result, my report, text, false)
		beep
	end if
	my nettoie()
end open

#=====

on exploreTraite(elem, ptree) (*
elem est un Unicode text
¢ elem is an Unicode text *)
	local cl_
	tell application "System Events" to tell disk item elem to set cl_ to class
	if (cl_ as text) is in {"folder", "«class cfol»"} then
		my ExploreUnDossier(elem, ptree)
	end if # cl_ is .
end exploreTraite

#=====

on ExploreUnDossier(dossier, ptree)
	local listItems, nomElement, cheminElement, c
	# dossier is a text path
	if dossier does not end with ":" then set dossier to dossier & ":"
	tell application "System Events" to tell disk item dossier
		set listItems to name of disk items whose visible is true
		set sourceName to name
		set sourceParent to path of container
	end tell
	
	if (sourceName starts with "job ") and (sourceName ends with " Folder") then
		# Force the folder name to lowercase and replace embedded space chars by "-"
		set normalizedName to do shell script "/usr/bin/python -c \"import sys; print unicode(sys.argv[1], 'utf8').lower().replace(' ','-').encode('utf8')\" " & quoted form of sourceName
		if storeInDedicatedFolder then
			# CAUTION ! dedicatedFolder ends with a colon
			my dedicatedFolder
		else
			sourceParent & ":"
		end if
		result & normalizedName & ".zip"
		POSIX path of result
		try
			# I dropped zip and use ditto which I am accustomed to use
			--do shell script "/usr/bin/zip -qo9r " & quoted form of result & space & quoted form of POSIX path of dossier
			do shell script "ditto -ck " & quoted form of POSIX path of dossier & " " & quoted form of result
		on error errMsg number errNum
			set my report to my report & "Error while creating ZIP file. (" & errNum & ")" & errMsg & return
		end try
		
	end if
	repeat with nomElement in listItems
		tell application "System Events" to set c to name of disk item dossier
		my exploreTraite(dossier & nomElement, ptree & c & ":")
	end repeat
end ExploreUnDossier

#=====

on nettoie()
	# Don't store these properties in the script.
	set my report to ""
	set my dedicatedFolder to ""
end nettoie

#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		set openFile to open for access file targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeto

#=====

As explained in comments, the property storeInDedicatedFolder is ruling the storage location.

KOENIG Yvan (VALLAURIS, France) mardi 20 août 2013 22:21:02

Almost…
OK-It lets me pick multiple folders to zip, and it creates the storeInDedicatedFolder with the date and info as indicated in the script…but nothing goes is that folder, and there is no error stopping the script (and I see there should be one, if the zip is not created)…So, it would appear that it thinks its done.
I have searched the system just to make sure the zip files weren’t anywhere else, but they are not.
Any thoughts… I feel its really close…
thanks
babs

I’m puzzled;

Here it works flawlessly. Here is the log report:

tell application “AppleScript Editor”
choose folder with prompt “Create ZIP files of every file in selected folder(s):” with multiple selections allowed
→ {alias “Macintosh HD:Users:me:Desktop:dossier sans titre:”}
end tell
tell current application
path to desktop
→ alias “Macintosh HD:Users:me:Desktop:”
current date
→ date “mercredi 21 août 2013 09:45:47”
end tell
tell application “System Events”
make new folder at end of alias “Macintosh HD:Users:me:Desktop:” with properties {name:“zipped on 20130821_094547”}
→ folder “Macintosh HD:Users:me:Desktop:zipped on 20130821_094547:”
get path of folder “Macintosh HD:Users:me:Desktop:zipped on 20130821_094547:”
→ “Macintosh HD:Users:me:Desktop:zipped on 20130821_094547:”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ folder
get name of every disk item of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:” whose visible = true
→ {“extract 5digits numbers.app”, “get ID.applescript”, “Graph, manuel.doc”, “Graph, manuel.pages”, “imbécillité.doc”, “imbécillité.pages”, “job 1 Folder”, “job 2 Folder”, “System_fonts.doc”, “System_fonts.pages”}
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “dossier sans titre”
get path of container of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “Macintosh HD:Users:me:Desktop:”
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “dossier sans titre”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:extract 5digits numbers.app”
→ file package
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “dossier sans titre”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:get ID.applescript”
→ file
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “dossier sans titre”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:Graph, manuel.doc”
→ file
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “dossier sans titre”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:Graph, manuel.pages”
→ file package
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “dossier sans titre”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:imbécillité.doc”
→ file
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “dossier sans titre”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:imbécillité.pages”
→ file package
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “dossier sans titre”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder”
→ folder
get name of every disk item of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:” whose visible = true
→ {“about GREP - more.doc”, “about GREP - more.pages”, “Lion DiskMaker.doc”, “Lion DiskMaker.pages”, “zdossier1-1”}
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:”
→ “job 1 Folder”
get path of container of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:”
→ “Macintosh HD:Users:me:Desktop:dossier sans titre:”
end tell
tell current application
do shell script “/usr/bin/python -c "import sys; print unicode(sys.argv[1], ‘utf8’).lower().replace(’ ‘,’-').encode(‘utf8’)" ‘job 1 Folder’”
→ “job-1-folder”
do shell script “ditto -ck ‘/Users/me/Desktop/dossier sans titre/job 1 Folder/’ ‘/Users/me/Desktop/zipped on 20130821_094547/job-1-folder.zip’”
→ “”
end tell
tell application “System Events”
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:”
→ “job 1 Folder”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:about GREP - more.doc”
→ file
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:”
→ “job 1 Folder”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:about GREP - more.pages”
→ file package
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:”
→ “job 1 Folder”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:Lion DiskMaker.doc”
→ file
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:”
→ “job 1 Folder”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:Lion DiskMaker.pages”
→ file package
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:”
→ “job 1 Folder”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:zdossier1-1”
→ folder
get name of every disk item of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:zdossier1-1:” whose visible = true
→ {“Tournette.doc”, “Tournette.pages”}
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:zdossier1-1:”
→ “zdossier1-1”
get path of container of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:zdossier1-1:”
→ “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:”
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:zdossier1-1:”
→ “zdossier1-1”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:zdossier1-1:Tournette.doc”
→ file
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:zdossier1-1:”
→ “zdossier1-1”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 1 Folder:zdossier1-1:Tournette.pages”
→ file package
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “dossier sans titre”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder”
→ folder
get name of every disk item of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:” whose visible = true
→ {“about GREP - more.doc”, “about GREP - more.pages”, “Lion DiskMaker.doc”, “Lion DiskMaker.pages”, “zdossier1-1”}
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:”
→ “job 2 Folder”
get path of container of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:”
→ “Macintosh HD:Users:me:Desktop:dossier sans titre:”
end tell
tell current application
do shell script “/usr/bin/python -c "import sys; print unicode(sys.argv[1], ‘utf8’).lower().replace(’ ‘,’-').encode(‘utf8’)" ‘job 2 Folder’”
→ “job-2-folder”
do shell script “ditto -ck ‘/Users/me/Desktop/dossier sans titre/job 2 Folder/’ ‘/Users/me/Desktop/zipped on 20130821_094547/job-2-folder.zip’”
→ “”
end tell
tell application “System Events”
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:”
→ “job 2 Folder”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:about GREP - more.doc”
→ file
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:”
→ “job 2 Folder”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:about GREP - more.pages”
→ file package
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:”
→ “job 2 Folder”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:Lion DiskMaker.doc”
→ file
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:”
→ “job 2 Folder”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:Lion DiskMaker.pages”
→ file package
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:”
→ “job 2 Folder”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:zdossier1-1”
→ folder
get name of every disk item of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:zdossier1-1:” whose visible = true
→ {“Tournette.doc”, “Tournette.pages”}
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:zdossier1-1:”
→ “zdossier1-1”
get path of container of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:zdossier1-1:”
→ “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:”
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:zdossier1-1:”
→ “zdossier1-1”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:zdossier1-1:Tournette.doc”
→ file
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:zdossier1-1:”
→ “zdossier1-1”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:job 2 Folder:zdossier1-1:Tournette.pages”
→ file package
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “dossier sans titre”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:System_fonts.doc”
→ file
get name of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:”
→ “dossier sans titre”
get class of disk item “Macintosh HD:Users:me:Desktop:dossier sans titre:System_fonts.pages”
→ file package
end tell
Résultat :
“”

And the contents of the dedicated folder is :

job-1-folder.zip
job-2-folder.zip

May you check that your folder names are really matching what you described which is :

starts with “job "
ends with " folder”

I tested the script four ways :
(1) I ran it and choose the folder embedding the different job folders.
(2) I ran it and choose the two job folders
(3) dropped the folder embedding the folder on the icon of the script saved as an application (droplet)
(3) dropped the two job folders on the icon of the script saved as an application (droplet)

I made a last test replacing the unix command name by ditta to check the behavior when the command supposed to zip failed. I got the logical report :

Error while creating ZIP file. (127)sh: ditta: command not found
Error while creating ZIP file. (127)sh: ditta: command not found

This is why I assume that the folder names aren’t matching the defined rule.
In such case,
(a) the target folder is created
(b) the script doesn’t find folder matching the rules so it zip nothing
(c) nothing failed in the process so there is no reason to create a report.

If you want I may add a piece of code counting the zipped folders and issuing a report not only if something failed but if the count is zero.

KOENIG Yvan (VALLAURIS, France) mercredi 21 août 2013 09:52:02

You may send a screenshot to :
koenig yvan sfr fr

Better choice, send me the log report issued by the script when you run it from the Editor like the one which I posted above. With it, it will be quite easy to discover what is failing on your side.

KOENIG Yvan (VALLAURIS, France) mercredi 21 août 2013 10:56:42

Here is an edited version which doesn’t rely upon the names of the folders.

It zip every folder containing :
a folder named : “Links” and a folder named “Document Fonts”
a file named “instruction.txt”) and a file whose name contains “.indd”


property storeInDedicatedFolder : true # activate this one to store the zipped files in the folder zipped on yyyymmdd_hhmmss (example : "zipped on 20130820_220709") on the Desktop
# property storeInDedicatedFolder : false # activate this one to store the zipped file in the location of the original


# Two properties used for global storage. They are resetted on exit
property report : ""
property dedicatedFolder : ""

on run
	choose folder with prompt "Create ZIP files of every file in selected folder(s):" with multiple selections allowed
	open result # result is always a list
end run

on open droppedItems
	# initialize two properties in case of preceeding crash
	my nettoie()
	if storeInDedicatedFolder then
		# Create the dedicated folder in which zip files will be stored
		set p2d to path to desktop
		tell (current date) to (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
		tell application "System Events" to set my dedicatedFolder to path of (make new folder at end of p2d with properties {name:"zipped on " & result})
	end if
	# Main loop
	repeat with sourceFile in droppedItems
		try
			my exploreTraite(sourceFile as text, "")
		end try
	end repeat
	
	if my report > "" then
		beep
		(path to desktop as text) & "@@@@@@@.txt"
		my writeto(result, my report, text, false)
		beep
	end if
	my nettoie()
end open

#=====

on exploreTraite(elem, ptree) (*
elem est un Unicode text
¢ elem is an Unicode text *)
	local cl_
	# Here we must check the class of items because we can't know for sure if the dropped ones are really folders
	tell application "System Events" to tell disk item elem to set cl_ to class
	if (cl_ as text) is in {"folder", "«class cfol»"} then
		my ExploreUnDossier(elem, ptree)
	end if # cl_ is .
end exploreTraite

#=====

on ExploreUnDossier(dossier, ptree)
	local listItems, nomElement, cheminElement, c
	# dossier is a text path
	if dossier does not end with ":" then set dossier to dossier & ":"
	tell application "System Events" to tell disk item dossier
		# As we will only zip folders, grab the list of folders
		set listItems to name of folders whose visible is true
		# grab the list of files to check that it contain : "instruction.txt" and "xxx.indd"
		set listFiles to name of files whose visible is true
		set sourceName to name
		set sourceParent to path of container
	end tell
	
	#if (sourceName starts with "job ") and (sourceName ends with " Folder") then
	if (listItems contains "Links") and (listItems contains "Document Fonts") and (listFiles contains "instruction.txt") and ((listFiles as text) contains ".indd") then
		# Force the folder name to lowercase and replace embedded space chars by "-"
		set normalizedName to do shell script "/usr/bin/python -c \"import sys; print unicode(sys.argv[1], 'utf8').lower().replace(' ','-').encode('utf8')\" " & quoted form of sourceName
		if storeInDedicatedFolder then
			# CAUTION ! dedicatedFolder ends with a colon
			my dedicatedFolder
		else
			sourceParent # As we grab sourceParent through System Events, the colon is already available --& ":"
		end if
		result & normalizedName & ".zip"
		POSIX path of result
		try
			# I dropped zip and use ditto which I am accustomed to use
			--do shell script "/usr/bin/zip -qo9r " & quoted form of result & space & quoted form of POSIX path of dossier
			do shell script "ditto -ck " & quoted form of POSIX path of dossier & " " & quoted form of result
		on error errMsg number errNum
			set my report to my report & "Error while creating ZIP file. (" & errNum & ")" & errMsg & return
		end try
		
	else
		repeat with nomElement in listItems
			tell application "System Events" to set c to name of disk item dossier
			my exploreTraite(dossier & nomElement, ptree & c & ":")
		end repeat
	end if
end ExploreUnDossier

#=====

on nettoie()
	# Don't store these properties in the script.
	set my report to ""
	set my dedicatedFolder to ""
end nettoie

#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		set openFile to open for access file targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeto

#=====

KOENIG Yvan (VALLAURIS, France) mercredi 21 août 2013 11:33:44

Good morning Yvan…

SUCCESS!!!
At first it didn’t work, but that is because I gave you the names of the items in the folder, but didn’t realize you were going to use them in the script, so I just had to make them match exactly …

“instructions.txt” is the exact way it is spelled and I gave you instruction.txt (no s at the end)
“Document fonts” is how the folder is named and I gave you Document Fonts (Upper case F)

Once I matched the names exactly it worked perfectly!!!

many many many many thanks…this will be a really nice time saver!!!

babs :smiley: :smiley:

Thanks for the feedback.

KOENIG Yvan (VALLAURIS, France) mercredi 21 août 2013 17:01:51

Thank you for all your time and effort!!! :slight_smile:

I’m wondering if it would be OK to edit the instruction :

if (listItems contains "Links") and (listItems contains "Document fonts") and (listFiles contains "instructions.txt") and ((listFiles as text) contains ".indd") then

by

if (listItems contains "Links") and (listItems contains "Document fonts") and (listFiles contains "instructions.txt") and (listFiles contains (sourceName and ".indd")) then

KOENIG Yvan (VALLAURIS, France) mercredi 21 août 2013 17:52:53

Hi Yvan,

sorry I just saw this post…didn’t get an email alert…I tried it, but no it didn’t work…the original one did. :slight_smile:

I do have a question though…I realized I will occasionally have jobs that are text only, and therefore will not produce a .zip of that folder, since it doesn’t meet the criteria for having the Links Folder enclosed… I just realized this…
Is there any way to have the script make the .zip if the Links folder is there…or Is NOT there?

Is that possible???

thanks!!!
babs