Problem getting file paths from a list and passing to shell script

Hello,

I’m in need of some help on a script I need. This may be a fair amount of work so I 'm willing to pay a little for someone’s assistance. First some background on what I’m doing.

I’m a photographer and use an application called Capture One Pro to process our Raw files to TIFF and JPEG. When we start a new job in C1Pro we always name the job a specific way which is a Job Number and Client Name pair (i.e. 0000-00 ClientName) so C1Pro creates a folder by that name in our Work In Progress folder. Inside the job folder “0000-00 ClientName” C1Pro creates three folders (Captures, Processed and Trash). Inside of “Processed” C1Pro will create a folder called “Raw_Exports” in which our final high resolution files are placed.
I need to test for and create a new folder, if it doesn’t exist, then zip each item individually and move the zipped file to the “Delivered” folder.

***Here is an overview of what I need to accomplish.

  • Select “Job Folder” via prompt and place in variable “jobFolder”
  • Create variables that contain the paths to “Job Folder:Processed” and “Job Folder:Processed:Raw_Exports”
    (variable names “processedFolder” and “exportsFolder” respectively)
  • Test for the existence of the folder “Delivered” which should be at “Job Folder:Processed:Delivered”
    and create the folder if needed. then create variable for “Delivered” called deliveredFolder.
  • Get a list of every file in the folder “Raw_Exports”
  • Get a list of every file in the “Delivered” folder
  • Compare the lists and omit files that have been zipped and placed in the “Delivered” folder.
  • Compress each item in “Raw_Exports”, excluding items already compressed, individually using ditto as zip
    and do not keep parent
  • add “hr_” to the begining of each compressed filename and remove original extension (i.e. name.tif.zip
    remove .tif)
  • Move each compressed file to the “Delivered” folder
  • Temporarily rename the folder “Delivered” to (“Job_” & jobNumber) (i.e. Job_0000-00)
  • Upload to webserver
  • Rename folder back to “Delivered”
  • Display completion Dialog

Here is the script I’ve been trying to get working


(* Select the job folder *)

choose folder with prompt "Choose a job folder to upload HiRes files to the web for:"
set jobFolder to result
set processedFolder to ((jobFolder as string) & "Processed") as alias
set exportsFolder to ((processedFolder as string) & "Raw_Exports") as alias

(* Test for Delivered folder and create if it doesn't exist*)
tell application "Finder"
	if not (folder ((processedFolder as string) & "Delivered") exists) then
		make new folder at processedFolder with properties {name:"Delivered"}
	end if
	
	set deliveredFolder to ((processedFolder as string) & "Delivered") as alias
	set the destination_folder to deliveredFolder as alias
	set the destination_directory to POSIX path of ¬
		the destination_folder
end tell

(* NOT WORKING - Get list of items in processedFolder and compress each item individually.
	also add hr_ to beginning of compressed file name and move compressed file to deliveredFolder *)

with timeout of 1000000 seconds
	try
		set fileNames to {}
		tell application "Finder"
			set all_files to every file of entire contents of folder exportsFolder
			repeat with a_file in all_files
				set end of fileNames to (quoted form of POSIX path of (a_file as Unicode text))
			end repeat
			--repeat the command to compress each item as an individual archive
			repeat with a_file in all_files
				--used to extract the name and location of the file
				set itemProp to properties of a_file
				--where the file is
				set itemPath to quoted form of POSIX path of a_file
				--where the compressed file should end up
				set destFold to quoted form of POSIX path of deliveredFolder
				--what the name of the file is
				set itemName to name of a_file
				set itemName to ("hr_" & itemName)
				--do it, do it now
				do shell script ¬
					("ditto -c -k -X --rsrc " & itemPath & ¬
						" " & destFold & "'" & itemName & "'" & ".zip")
				set itemName to ("hr_" & itemName)
			end repeat
		end tell
	on error errmsg
		--should anything go wrong let the user know
		display dialog errmsg
	end try
end timeout
-- If someone can get it working through to this point I should be able to handle the rest
-- Most everything below here works
(* Extract client name and job number from jobFolder *)

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

set webFolder to ((jobFolder as string) & "Processed:Delivered") as alias
tell application "Finder" to set name of result to ("Job_" & jobNumber)

(* Upload via FTP using Transmit *)

set oldDelim to text item delimiters
set text item delimiters to " "

set the_server to "[url=ftp://ftp.webserver.com]ftp.webserver.com[/url]"
set the_user to "username"
set the_pass to "password"
set the_proto to "FTP"
set the_port to "21"
set base_path to "/server/path/to/clientfolders/"
set the_path to (base_path & clientName)


-- Make sure Transmit exists before anything else

tell application "Finder"
	
	try
		set transmitVersion to (get version of application file id "TrAn")
	on error
		return "Couldn't find Transmit"
	end try
	
	if transmitVersion does not contain "Transmit 3" then
		return "Transmit 3 required"
	end if
	
end tell


-- Find out if Transmit was already running, we'll use this later

tell application "System Events"
	set wasRunning to name of every process whose name contains "Transmit"
end tell

-- Now work magic

tell application "Transmit"
	
	-- Get the old value
	set oldSuppressValue to (SuppressAppleScriptAlerts of application "Transmit")
	
	-- Turn off alerts
	set SuppressAppleScriptAlerts to true
	
	-- Maybe make a new window
	
	if (wasRunning is not {}) then
		set myDoc to make new document
	else
		set myDoc to document 1
	end if
	
	set didConnect to false
	set didChange to false
	set didUpload to false
	
	-- Tell the session what to do
	set mySession to current session of myDoc
	tell mySession
		
		-- Try to connect to the server
		
		if the_proto = "FTP" then
			set didConnect to connect to (the_server) using port (the_port) as user (the_user) with password (the_pass) with connection type FTP
		else if the_proto = "SFTP" then
			set didConnect to connect to (the_server) using port (the_port) as user (the_user) with password (the_pass) with connection type SFTP
		else if the_proto = "WebDAV" then
			set didConnect to connect to (the_server) using port (the_port) as user (the_user) with password (the_pass) with connection type WebDAV
		end if
		
		if (didConnect = true) then
			
			-- Try to change directories if requested
			
			if (the_path is not "") then
				set didChange to set their stuff to (the_path)
			else
				set didChange to true
			end if
			
			
			-- UPLOAD: Upload the files here
			
			try
				with timeout of 3600 seconds
					upload item webFolder with resume mode resume
				end timeout
				set didUpload to "true"
			end try
			if (didUpload = false) then
				error
			end if
			
			disconnect
		end if
	end tell
	
	-- Error out if we need to, and don't close the window
	
	if (didConnect = false) then
		set SuppressAppleScriptAlerts to oldSuppressValue
		return "Couldn't connect"
	end if
	
	if (didChange = false) then
		set SuppressAppleScriptAlerts to oldSuppressValue
		return "Couldn't set path"
	end if
	
	-- Set back to the old value
	set SuppressAppleScriptAlerts to oldSuppressValue
	
	-- Quit Transmit or Close the Window, depending on if it was running
	
	if (wasRunning is {}) then
		quit
	else
		tell myDoc
			close
		end tell
	end if
	
	set webFolder to ((jobFolder as string) & "Processed:Job_" & jobNumber) as alias
	tell application "Finder" to set name of result to ("Delivered")
end tell

set text item delimiters to oldDelim

display dialog "The HiRes files for Job " & jobNumber & " have been uploaded to the " & clientName & " folder in the Client Area.
Would you like to go to the Client Folder now?" buttons {"No", "Yes"} default button 2
copy the result as list to {the button_pressed}

if button_pressed = "Yes" then
	set the target_URL to "http://www.webserver.com/cgi-bin/ImageFolio31/imageFolio.cgi?direct=" & clientName & "/" & "Job_" & jobNumber
	open location target_URL
end if

This script is probably messy as it has been pieced together from samples and tutorials and I’m obviously fairly new to Applescript at least anything of this magnitude. If this needs more explaining please contact me. I have placed a group of folders that can be downloaded that show the folder structure.

http://www.bwstudios.com/ScriptHelp.zip

Many thanks in advance.
Mark

Mark:

I am going to take a crack at your ditto section and see how I do. From what I can see, you already have the POSIX path of where you want your zip files to end up, and it is assigned to your variable destination_directory like this:

set the destination_directory to POSIX path of ¬
       the destination_folder

As long as that is the folder you want ditto to write the zip files, this script should work for you:

tell application "Finder"
	set all_files to every file of entire contents of folder exportsFolder
	repeat with a_file in all_files
		set fnm to name of a_file--This sets the name of the file (including name extension) to the variable fnm
		set p_off to offset of "." in fnm--This finds the period, so we can discard the former extension in the next line
		set comp_file_name to quoted form of ("hr_" & ((characters 1 thru (p_off - 1) of fnm) & ".zip") as string)--This builds the new name for the file we are currently working on and stores it as the variable comp_file_name
		set itemPath to (quoted form of POSIX path of (a_file as Unicode text))
		do shell script ¬
			("ditto -c -k -X --rsrc " & itemPath & ¬
				" " & destination_directory & comp_file_name)
	end repeat
end tell

This code worked for me; I am using OSX 10.4.3. Good luck, I hope this helps

casdvm

Hi Casdvm

I placed your block of code in my script and I get the following error.

ditto: Can’t archive multiple sources
Usage: ditto [ ] src [ …src] dst

If there is only one item in the exportsFolder the script runs but doesn’t zip the file and move it to the delivered folder. If there is more than one item I get the error shown above.

Thanks for trying :slight_smile:

mt

mt:
I think I know where I messed up. Do any of your filenames already have a period (.) in them other than the period before the file extension?

Also, you should have a dummy section set up to test this baby, inside of another folder, so we can mess with it without touching anything real.

And, you should test each portion one at a time, before executing the whole giant script at once.

I have not tested your top portion yet, perhaps therein lies another problem. I will work on it today.

casdvm

mt:

I am not sure what is wrong on your end. I set up complete dummy folders that meet the criteria in the top of your script. Here is the layout:

Folder MTTest
-contains folder Processed
–contains folder Delivered
–contains folder Raw_Exports

I put a bunch of random files in the Raw_Exports folder and ran the script below:

choose folder with prompt "Choose a job folder to upload HiRes files to the web for:"
set jobFolder to result
set processedFolder to ((jobFolder as string) & "Processed") as alias
set exportsFolder to ((processedFolder as string) & "Raw_Exports") as alias

(* Test for Delivered folder and create if it doesn't exist*)
tell application "Finder"
	if not (folder ((processedFolder as string) & "Delivered") exists) then
		make new folder at processedFolder with properties {name:"Delivered"}
	end if
	
	set deliveredFolder to ((processedFolder as string) & "Delivered") as alias
	set the destination_folder to deliveredFolder as alias
	set the destination_directory to POSIX path of ¬
		the destination_folder
end tell

tell application "Finder"
	set all_files to every file of entire contents of folder exportsFolder
	repeat with a_file in all_files
		set fnm to name of a_file --This sets the name of the file (including name extension) to the variable fnm
		set fxt to a_file's name extension
		set p_off to offset of fxt in fnm --This finds the period, so we can discard the former extension in the next line
		set comp_file_name to quoted form of ("hr_" & ((characters 1 thru (p_off - 2) of fnm) & ".zip") as string) --This builds the new name for the file we are currently working on and stores it as the variable comp_file_name
		set itemPath to (quoted form of POSIX path of (a_file as Unicode text))
		do shell script ¬
			("ditto -c -k -X --rsrc " & itemPath & ¬
				" " & destination_directory & comp_file_name)
	end repeat
end tell

This script is only SLIGHTLY different from my first script that left you. It processed (zipped) every file in the Raw_Exports folder and placed the zipped files in the Delivered folder and added a [hr_] to the front of each filename.

I believe that is what you were shooting for. Please try this script all by itself, see if it works for you, then let’s move on. Just in case, please identify which OSX version you are running; and please state whether or not you have installed the Developer Tools. I know it sounds strange, but I have had some weird behaviors with shell scripts unless the Developer tools package was installed.

Good luck,

casdvm

Hey Casdvm,

I still get the same error. However I agree about the Developer Tools needing to be installed. I recently did a clean install on a new hard drive on the Powerbook I’m using as well as my other work stations. Let me get the dev tools installed tonight and I’ll get back to you.

Thanks Again

mt

Forgot to answer some previous questions.

  1. I don’t use more then one period in a filename.

  2. I have a test directory on my local machine and my webserver.

My test folder structure can be downloaded using the link under the script in my original post.

THX

mt

mt:

Sorry about that, I had forgotten you put that link there. I appreciate your gentle reminder.

It is now fixed. We both forgot to use the
[quoted form]
of the destination directory:

set the destination_directory to quoted form of (POSIX path of ¬
		the destination_folder)

Simply use this line to replace the line just before the first [end tell] in the upper portion (your portion) of the last script I uploaded, and all will work mahvelously. At least, it did when I ran it on your sample folder I downloaded. Oh yes, I got the same error you did until I put the
[quoted form]
in there.

Good luck,

casdvm

casdvm

you da bomb diddy!
it does work mahvelously :smiley:

I tested the code you provided then ran the entire script and it operated correctly!
Thanks a lot for your help. If you see any potential problems in the rest of my script your input
would be greatly appreciated.

THX

mt

Excellent! That’s great news.

Actually, I did look at the rest of your code, and it frightened me immensely. It may be sloppy (I don’t know), but if it does what you want it to do; save it, back it up somewhere, and use it!

casdvm