can script ignore folder structure?

so I have this line:

copy (every file in folder originals_folder whose creator type is "XPR3") to folder "Page_Layouts" of (container of folder originals_folder)

works nice, but what if the Quark file (creator type - XPR3) happens to be inside of a folder that’s inside of another folder that is copied to “originals_folder” folder

this is part of a folder action script triggered when files are added. “originals_folder” is the folder with script attached

…in other words how do I ignore the folder structure and make the script look at all files in the folder reagrdless how deeply they’re burried??

thanks

Browser: Firefox 1.0.1
Operating System: Mac OS X (10.3.9)

This should work:

tell application "Finder"
	copy (files of entire contents of originals_folder whose creator type is "XPR3" as list) to folder "Page_Layouts" of (container of folder originals_folder)
end tell

A script can contain a handler that is recursive, i.e. a handler that calls itself if conditions are met. The script would build a list the items contained in the top folder, copying an item if it is a file or if the item is a folder, the handler is executed again, this time using that new folder item as it’s parameter. This process of listing items in a folder, copying or going one level deeper repeats until a sub-folder is reached that has files only. The script then works back up the folder structure, returning to the file/folder list of each level where a folder had been detected. If there are additonal files to copy or folder contents to list at that level, the script addresses each before moving up one level.

set originals_folder to choose folder
set destinationFolder to (((originals_folder as string) & "Page_Layouts:") as alias --for duplicateFile handler
copyTheFiles(originals_folder) -- call the file/folder main handler for first time, passing top folder as parameter

on copyTheFiles(aFolder)
	tell application "Finder" to set itemList to every item of aFolder --list of files and folders in aFolder
	repeat with anItem in itemList --step through each item in file/folder list
		set newItem to (anItem as alias) --alias allows using 'info for' and passing file ref to duplicate handler
		set theInfo to info for (newItem) --uses info for command to discover if item is file or folder
		if (folder of theInfo) then --if item is a folder then return to the top handler and repeat
			copyTheFiles(newItem) --list files-folders, passing this subfolder alias as parameter		
		else -- item is a file so we can duplicate file, pass file alias as parameter
			if (file type of theInfo is "XPR3") then -- set for Quark
				duplicateFile(newItem)
			end if
		end if
	end repeat
end copyTheFiles

on duplicateFile(filetoDuplicate)
	try
		tell application "Finder"
			duplicate file fileToDuplicate to folder (my destinationFolder)
		end tell
	on error errMsg
		display dialog errMsg	
	end try
end copyFile

There are a fly in the ointment however. If you have a files with the same name in different subfolders, the script will error since all the files are being dumped into the same directory. You might want to expand the script to duplicate the ‘originals_ folder’ subfolder structure in the destination folder.

The command for the Finder to copy an object is duplicate- take a look through the Finder’s dictionary for information about this.

HTH
Craig
*Edit to change file creator to file type

I’ve tried Jerome’s method first and it seems to work fine. The duplicate file name scenario suthercd mentioned becomes an issue though.

I’m trying to keep this thing really simple so do you guys have any suggestions how to build on Jerome’s way and maybe skip the files that have duplicate file names. suthercd, my purpose for this script is to have the files distributed to directories without any copies and subfolders. that’s why I’m trying to resolve this by simply not copying the files with the same name that have been copied already.

thank you guys

You could trap for the error that is thrown when Finder tries to duplicate a file to a folder where a file with that name exists. Create the list of files, then loop through the files on the list, using try… on error… end try to identify when a file with the same name exists. The try block captures the error and allows the script to continue, ignoring the problematic file.

tell application "Finder" to set fileList to (files of entire contents of originals_folder)

repeat with aFile in fileList
	copyFile(aFile as alias)
end repeat

on copyFile(aFile)
	try
		tell application "Finder"
			duplicate file (aFile) to folder "Page_Layouts" of (container of folder originals_folder)
		end tell
	on error
		return
	end try
end copyFile

Craig

this works indeed, I hate to be a pain in the neck since with your help I’m one tiny step of hopefully making it all work but how do I mix the last thing you’ve just proposed with the detecting of creator type, like I have it in the original post?

and btw, you script work on its own but when I try to plug it as part of my script, it does not accept the “on” in the “on copyFile(aFile)” line, says that it expected to find “end” but found “on” instead

thanks

– update: I think I might’ve found a way to marry the two… it should work although it looks like an unnecesary workaround, but as long as it works
if you could shed some light on the “on” issue though, I’d appreciate it

Comment out most of your script and then un-comment one function/step at a time. Applescript may not pinpoint the exact location of an error and often the actual syntax error precedes the reported point. You’ll probably find some missing word in your syntax or missing puntuation.
Here is a revision of the pertinent code to reflect your latest request:

tell application "Finder" to set fileList to (files of entire contents of originals_folder whose creator type is "XPR3")

HTH
Craig

all right, this is what I got investigating the script,
for some reason the handler copyFile(aFile) does not allow me to put tell application “Finder” and end tell around it.
in other words…

tell application "Finder"

--preceeding script

tell application "Finder" to set fileList to (files of entire contents of originals_folder whose creator type is "XPR3")

repeat with aFile in fileList
	copyFile(aFile as alias)
end repeat

on copyFile(aFile)
	try
		tell application "Finder"
			duplicate file (aFile) to folder "Page_Layouts" of desktop
		end tell
	on error
		return
	end try
end copyFile

end tell

… will not work, or even accept this format

from what it looks like I have to have the “tell” part around it for the script as a whole to work

If you post your full script it will help figure this out. At this point it is a bit like the elephant in the dark fable.

Craig

Your call to the copyFile subrutine is within the Finder tell statement, for this reason you need to “tell me” for the script to recognize the subrutine or end the Finder tell statement before you call the subrutine. So you would have:

set originals_folder to choose folder
tell application "Finder"
	set FileList to (files of entire contents of originals_folder)
	tell me
		repeat with AFile in FileList
			copyFile(AFile as alias)
		end repeat
	end tell
end tell

on copyFile(AFile)
	display dialog "Working"
end copyFile

or:

set originals_folder to choose folder
tell application “Finder”
set FileList to (files of entire contents of originals_folder)
repeat with AFile in FileList
tell me to copyFile(AFile as alias)
end repeat
end tell

on copyFile(AFile)
display dialog “Working”
end copyFile


or:

set originals_folder to choose folder

tell application “Finder”
set FileList to (files of entire contents of originals_folder)
end tell
repeat with AFile in FileList
copyFile(AFile as alias)
end repeat

on copyFile(AFile)
display dialog “Working”
end copyFile


or, finally:

set originals_folder to choose folder

tell application “Finder” to set FileList to (files of entire contents of originals_folder)
repeat with AFile in FileList
copyFile(AFile as alias)
end repeat

on copyFile(AFile)
display dialog “Working”
end copyFile

I tried going with the second option:

tell application "Finder"
	set JobFolder to "0000"
	set customer to "client1"
	set OriginalFolder to choose folder
	
	with timeout of 60 seconds
		copy (every folder in OriginalFolder) to folder "Cust_Originals" of folder JobFolder of folder customer of disk "Production"
		copy (every file in OriginalFolder) to folder "Cust_Originals" of folder JobFolder of folder customer of disk "Production"
	end timeout
	
	set originals_folder to folder "Cust_Originals" of folder JobFolder of folder customer of disk "Production"
	tell application "Finder"
		set fileList to (files of entire contents of originals_folder)
		repeat with aFile in fileList
			tell me to copyFile(aFile as alias)
		end repeat
	end tell
end tell

on copyFile(aFile)
	try
		tell application "Finder"
			duplicate file (aFile) to folder "Page_Layouts" of desktop
		end tell
	on error
		return
	end try
end copyFile

… and it works

now all I change is:

set fileList to (files of entire contents of originals_folder whose creator type is "XPR3")

and it doesn’t… how is that small detail thorwing the whole thing off? does it have to be addressed again in the last ‘on copyFile(aFile)’ section?

another thing to note…

swap the line that does not work:

set fileList to (files of entire contents of originals_folder whose creator type is "XPR3")

for:

set fileList to (files of entire contents of originals_folder whose name contains "-some characters here-")

and it works again. So it’s not as much having something follow the ‘files of entire contents of originals_folder’ but the creator/file type detection that screws things up

??