Droplet to copy file(s) based on the name of parent directory

Hello all, thanks for taking the time to read this!

Basically, I’m trying to assemble a script that runs the following logic on a folder/selection of folders, either via a drag and drop applet or multiple manual selections:

Script Logic******
It looks in the specified folder(s) for any folders/subfolders (no matter how deep) with the name “Outlined”, and if it finds any, it copies all the files inside that/those folders to a pre-determined location. (A folder on the desktop).


That’s about it. I was able to cobble together a script which would run successfully with one folder that I had to manually target with a prompt, but it failed both as a droplet and when I tried to run it on multiple folders at once. I unfortunately don’t have that hacked code with me at the moment, but it was based off this piece of coding that I found:

tell application “Finder”
set theFiles to a reference to every file of (entire contents of folder “old:folder:path”)
duplicate theFiles to folder “new:folder:path”
end tell

In case it’s of use, here’s a little more background/specifics on what I’m trying to do:

My company’s software auto-generates folders per job with the following structure: (Parens indicate a folder)

(JobName)
–(Outlined)
–>–JobName.pdf
–(Web)
–>–JobName web.pdf
–JobName Report.pdf

There’s literally hundreds of these folders generated a day, and the only document that I’m generally interested in out of that is the JobName.pdf, in this example. So what I’m trying to do is point a script at either specific ‘JobName’ directories (in multiple) or occasionally point it at the top-level directory to parse through everything inside. And it would extract that particular file (which can only uniquely be found by the name of its parent folder, ‘Outlined’) into, as I said, a pre-existing folder on the desktop.

I hope that was clear, and thanks in advance if anyone has any advice on how I can proceed!

Hi,

you can do this actually with an one-liner


set baseFolder to "/path/to/baseFolder"
set destinationFolder to "/path/to/destinationFolder"

do shell script "/usr/bin/find " & quoted form of baseFolder & " -type f -path */Outlined/*.pdf -print -exec /usr/bin/ditto  {} " & quoted form of destinationFolder & " \\;"

The shell command /usr/bin/find lists all files, whose path ends with /Outlined/*.pdf
and passes them to /usr/bin/ditto. ditto copies (duplicates) the files to the destination folder.
If there is more than one pdf file in the Outlined folder, you could probably refine the search path parameter

Hm I won’t be able to try it out til tomorrow but it certainly sounds about right, and seems elegantly simple! Is this something that will convert easily into a ‘droplet’? Would I add:

on open destinationfolder

end open

to do so?

It looks like this implementation would be great for those times when I want to rip all the ‘outlined’ files out of the base directory, but there are many times when I just care about maybe 10 or so of the hundreds of folders in there.

Either way, thanks for the quick response and advice! I’ll definitely be playing around with this tomorrow!

Cheers!

If you want to make the script into a droplet, keep in mind AppleScript works with aliases and the terminal works with POSIXes. Thus you’ll have to convert the aliases to POSIXes.

property DESTINATION_FOLDER : "/path/to/destination/Folder"

on open baseFolders
	-- we already have the folders, so go on and process them
	repeat with i in baseFolders
		main(POSIX path of i, DESTINATION_FOLDER)
	end repeat
end open

on run
	-- let user chose baseFolder
	set myFolders to choose folder with prompt "Choose a folder" with multiple selections allowed
	
	-- process every chosen folder
	repeat with i in myFolders
		main(POSIX path of i, DESTINATION_FOLDER)
	end repeat
end run

on main(baseFolder, destFolder)
	do shell script "/usr/bin/find " & quoted form of baseFolder & " -type f -path */Outlined/*.pdf -print -exec /usr/bin/ditto  {} " & quoted form of destinationFolder & " \\;"
end main

Hope it helps,
ief2

a shorter version with a folder check to avoid errors


property destinationFolder : "/path/to/destination/Folder"

on open baseFolders
	repeat with i in baseFolders
		set {folder:Fo, package folder:Pa} to info for i
		if Fo and not Pa then
			do shell script "/usr/bin/find " & quoted form of POSIX path of i & " -type f -path */Outlined/*.pdf -print -exec /usr/bin/ditto  {} " & quoted form of destinationFolder & " \\;"
		end if
	end repeat
end open

on run
	open (choose folder with prompt "Choose a folder" with multiple selections allowed)
end run