Create .zip archive of each item in a folder

Hi All,

I creating a script to push images up to my web server. Before the image is uploaded I would like to Zip each file and move the original to a different folder. I already have a variable called webFolder that represents the folder whose contents should be zipped then the original file should be moved up one directory level to a folder named “Delivered” which should checked for and created if it doesn’t exist.

Some background on it’s use.
I’m a photographer and use Capture One Pro to process RAW image files. I name each job folder with the structure “####-## Client_Name” (i.e. 0000-00 Test_Client) the first two digits are the year the second two are the month and the two after the hyphen are the job for that month. So 0508-01 would be the first job of the 8th month of the year 2005.
Capture One Pro will create the folders “Captures”, “Processed” and “Trash” inside of the job folder.
When I process any HiRes files they are placed at
0000-00 Test_Client:Processed:Raw Exports
The contents of “Raw Exports” is what needs to be zipped then the original image file needs to be moved to a new folder one level up called “Delivered” which needs to be created if it doesn’t already exist. The “Raw Exports” folder, known by variable name webFolder, is renamed “Job_0000-00” for uploading so it’s contents merge with an existing folder on the webserver then the name is reset in a later part of the script after uploading. I did not include the entire script since the uploading portion is not needed and will upload whatever is in webFolder.


choose folder with prompt "Choose the job folder you would like to upload HiRes files to the web for:"
set jobFolder to result

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set clientName to every text item of (name of (info for jobFolder))
set jobNumber to first item of result
set clientName to (items 2 through -1 of clientName) as string
set AppleScript's text item delimiters to ASTID

--Rename folder for upload
tell application "Finder"
	try
		set webFolder to ((jobFolder as string) & "Processed:Raw Exports") as alias
		tell application "Finder" to set name of result to ("Job_" & jobNumber)
		set newFoldername to ("Job_" & jobNumber)
	on error
		set webFolder to ((jobFolder as string) & "Processed:" & "Job_" & jobNumber) as alias
		set newFoldername to ("Job_" & jobNumber)
	end try
end tell

--Add create archive routine here or before rename routine above. Archive each item in webFolder and move original image to folder "Delivered" of the folder "Processed" of the folder jobFolder.

Thanks in advance,
Mark

Hi Mark,

Here is a handler that makes a copy of the file as a zipped file in the same location:

-- File Zipper
-- Creates zipped file in same folder -- Pass file/folder path to this handler
--File/Folder Zipper -- John Maisey -- 25 Jan 2005
on fileZipper(myFile)
	if (myFile as text) ends with ":" then
		display dialog (myFile as text)
		set myDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {":"}
		set myFile to (text items 1 thru -2 of (myFile as text) as text)
		set AppleScript's text item delimiters to myDelimiters
	end if
	set zipFile to (myFile & ".zip") as text
	do shell script "/usr/bin/ditto -c -k -rsrc --keepParent " & (quoted form of (POSIX path of myFile)) & " " & (quoted form of (POSIX path of zipFile))
	return zipFile
end fileZipper

Best wishes

John M

I was hoping to use this applescript in an Automator “run applescript” action and have it zip the file passed into the action. But it doesn’t seem to work. Any idea how I could do that?

on fileZipper(myFile)
if (myFile as text) ends with “:” then
display dialog (myFile as text)
set myDelimiters to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to {“:”}
set myFile to (text items 1 thru -2 of (myFile as text) as text)
set AppleScript’s text item delimiters to myDelimiters
end if
set zipFile to (myFile & “.zip”) as text
do shell script "/usr/bin/ditto -c -k -rsrc --keepParent " & (quoted form of (POSIX path of myFile)) & " " & (quoted form of (POSIX path of zipFile))
return zipFile
end fileZipper

:smiley:

I think that’s it! I removed “–keepParent” and that’s exactly what I was looking for. Thanks Jacques.

To pass the resulting .zip file to the next Automator action are you saying I need to replace:

on run {input}
repeat with i in input
fileZipper(i)
end repeat
end run

with what’s below?

on run {input}
set Zipped_Files to {}
repeat with i in input
tell fileZipper(i) to if it is not “” then set end of Zipped_Files to it
end repeat
return Zipped_Files --list of path of zipped files
end run

I was able to update an app I made for sending files in a custom dmg using the script!
check it out at the Scriptbuilder section. I made it for personal use, then decided to freeware it ; )

http://scriptbuilders.net/files/makeadelivery1.1.html

thanks a ton jacques

Thanks, this code is cool:cool:

I’m using the discussed script with small modifications, such as this one:
instead of the following block


	if (myFile as text) ends with ":" then
		set myDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {":"}
		set myFile to (text items 1 thru -2 of (myFile as text) as text)
		set AppleScript's text item delimiters to myDelimiters
	end if

I put these lines:


	set myFile to myFile as Unicode text
        if myFile ends with ":" then set myFile to (myFile's characters 1 thru -2) as Unicode text

I don’t know about performance - haven’t tested that.

Thanks!

I was looking for this kind of script for a long time. I’ve added this part

& " ; rm -fR " & (quoted form of (POSIX path of myFile))

at the end of the shell script to remove the original file or folder.

Here’s the whole thing


on fileZipper(myFile)
	if (myFile as text) ends with ":" then
		display dialog (myFile as text)
		set myDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {":"}
		set myFile to (text items 1 thru -2 of (myFile as text) as text)
		set AppleScript's text item delimiters to myDelimiters
	end if
	set zipFile to (myFile & ".zip") as text
	do shell script "/usr/bin/ditto -c -k -rsrc --keepParent " & (quoted form of (POSIX path of myFile)) & " " & (quoted form of (POSIX path of zipFile)) & " ; rm -fR " & (quoted form of (POSIX path of myFile))
	return zipFile
end fileZipper