Clean-up Folder Script

I’m trying to write a folder action script that reorganizes the contents of a folder dropped into it. I’ve tried to piece the script together from other posts but haven’t been successful. Here’s what I’m trying to do. Forgive me if it’s too complicated:

I have an existing Automator script that creates a set of folders for my projects. The script creates the following numbered “Parent Folder” which contains a set of numbered sub-folders. The “Parent Folder” name will vary:

xxxx-Parent Folder

  xxxx-Collected (contains a folder which contains .indd files and sub-folders named "Links" & "Fonts".
  xxxx-Copy (contains .doc files)
  xxxx-Comps (contains various files and folders)
  xxxx-Images (contains files with extensions: .psd, .tif, .eps, .jpg, etc)
  xxxx-PDF (contains PDF files)
  xxxx-Reference (contains various folders and files)

I’m hoping to run a folder action script or droplet to clean-up the “Parent Folder” when it’s dropped onto it. Here’s what I need it to do:

  1. Scan the “xxxx-Collected” sub-folder and delete all .zip archive files, move contents of “Links” sub-folder to the “xxxx-Images” sub-folder within the “Parent Folder” allowing over right of any duplicate file with no prompt, delete the now empty “Links” sub-folder after move, move “Fonts” folder to be within the “Parent Folder”, move .indd files to be within the Parent Folder allowing over right of any duplicate file with no prompt.

  2. Delete “xxxx-Comps” folder but prompt with OK or Cancel before moving to the Trash. (Cancel the action not the entire script)

  3. Delete “xxxx-Copy” folder.

  4. Delete “xxxx-Reference” folder but prompt with OK or Cancel before moving to the Trash. (Cancel the action not the entire script)

I know this is allot. Please respond with any pieces or parts if you don’t have all of it. Please let me know if any or all is possible or if you need clarification.

Thanks!

Model: Intel Mac Pro
AppleScript: 1.10.7
Browser: Firefox 2.0.0.7
Operating System: Mac OS X (10.4)

Okay here is a working vesrion to get you started. This is a stand alone version that could prolly stand some optimization but it does what you ask for. If you wanted to convert it to a droplet or folder action rather than running and choosing the folder it shouldn’t be too hard to do. =)

-- Choose the parent folder
set parentFolder to (choose folder) as string

-- Set up our base variables
tell application "Finder" to set parentFolderName to name of alias parentFolder
set jobNumber to text 1 thru 4 of parentFolderName
set posixPath to POSIX path of parentFolder

-- Remove all zip archives from Parent Folder:Collected
set posixCollected to posixPath & jobNumber & "-Collected/"
set shellCommand to "find " & quoted form of posixCollected & " -name \"*.zip*\" -exec rm {} \\;"
do shell script shellCommand

-- Move parent Folder:Collected:Links to Parent Folder:Images and remove Parent Folder:Collected:Links folder
set posixCollectedLinks to posixPath & jobNumber & "-Collected/Links/"
set posixImages to posixPath & jobNumber & "-Images/"
set shellCommand to "mv -f " & quoted form of posixCollectedLinks & "* " & quoted form of posixImages & "; rm -rf " & quoted form of (text 1 thru -2 of posixCollectedLinks)
do shell script shellCommand

-- Move the Parent Folder:Collected:Fonts folder to Parent Folder:
set posixCollectedFonts to posixPath & jobNumber & "-Collected/Fonts"
set shellCommand to "mv -f " & quoted form of posixCollectedFonts & space & quoted form of posixPath
do shell script shellCommand

-- Move the Parrent Folder:Collected:InDesign files to Parrent Folder:
set shellCommand to "find " & quoted form of posixCollected & " -name \"*.indd*\" -exec mv -f {} " & quoted form of posixPath & " \\;"
do shell script shellCommand

tell application "Finder"
	-- Send Parent Folder:Comps folder to trash with dialog
	try
		if button returned of (display dialog "Would you like to delete the folder: " & jobNumber & "-Comps" buttons {"Yes", "No"} default button "Yes") is "Yes" then
			move folder (parentFolder & jobNumber & "-Comps:") to trash
		end if
	end try
	
	-- Remove Parent Folder:Copy folder
	set posixCopy to posixPath & jobNumber & "-Copy"
	set shellCommand to "rm -rf " & quoted form of posixCopy
	do shell script shellCommand
	
	-- Send Parent Folder:Reference folder to trash with dialog
	try
		if button returned of (display dialog "Would you like to delete the folder: " & jobNumber & "-Reference" buttons {"Yes", "No"} default button "Yes") is "Yes" then
			move folder (parentFolder & jobNumber & "-Reference:") to trash
		end if
	end try
end tell

Awesome!. I’ll give it a try. Thanks for such a fast reply.

Your welcome, let me know how it works out! =)

James-

I tried running the script and I came across an Applescript error:

One note about the folder example I gave in my first post: the job number varies in length. It can go from 9 to 13 numbers. My example listed 4. I should have been more careful about that. Sorry if it caused confusion. Also, in some cases there is more than one folder in the -Collected sub-folder. See example below.

Here’s the exact folder structure that I am wanting to run the script on as a test. The number and name will vary from job to job:

I hope this clears up the folder structure and how the script needs to identify underlying files and sub-folders. Let me know if you have any questions.

Ahh, LOL - Okay back with an update soon

[Edit] - Okay so in the case of the Fonts folder, what happens when there are more than one job and we have multiple Fonts folders trying to exist? Should one fonts folder be created and all font files be dumped into it? Or multiple font folders with some naming convention?

Awesome! Thanks!

Here’s the actual script I have been using to generate the job folder set.


--Make a choice for server folder type--
display dialog ¬
	"Select New Project to Start or Cancel Process" buttons {"New Project", "Cancel"}
set the button_pressed to the button returned of the result

--To make  New Project server--
if the button_pressed is "New Project" then
	
	-- Prompt for a prefix.
	set prefix to text returned of (display dialog "Please enter a job number for the project folder names." default answer "Project Number-" with icon note)
	if prefix does not end with "-" then set prefix to prefix & "-"
	
	-- Use a "Save as." dialog to set the name and location for this project folder.
	set parentFolder to (choose file name with prompt "Enter a Project Name & Pick a Destination" default name (prefix & "Enter Project Name Here"))
	set parentPosix to quoted form of POSIX path of parentFolder
	
	-- Assemble the mkdir command.
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ""
	set mkdirStr to {"mkdir -p ", parentPosix, "/", prefix, "{Comps,Copy,Images,Collected,Reference,PDF}"} as Unicode text
	set AppleScript's text item delimiters to astid
	
	-- Create the folders.
	do shell script mkdirStr
	
	-- tell application "Finder" to open parentFolder
	
	
	--To make New Folder server--
else if the button_pressed is "New Folder" then
	-- Use a "Save as." dialog to set the name and location for this project folder.
	set parentFolder to (choose file name with prompt "Enter a Folder Name & Pick a Destination" default name ("Enter Folder Name Here"))
	set parentPosix to quoted form of POSIX path of parentFolder
	
	-- Create the folders.
	do shell script mkdirStr

Thanks again!

Great question.

There should only be one Fonts folder within the Parent folder and any duplicate fonts should be over-written. No prompt needed. So yes, one Fonts folder and all fonts dumped into it.

Okay updated script, I went ahead and put the job number before the fonts folder since all other child folders had it, but removing would be easy to do if needed.

-- Choose the parent folder
set parentFolder to (choose folder) as string


-- Set up our base variables
tell application "Finder" to set parentFolderName to name of alias parentFolder
set jobNumber to text 1 thru ((offset of "-" in parentFolderName) - 1) of parentFolderName
set posixPath to POSIX path of parentFolder


-- Remove all zip archives from Parent Folder:Collected
set posixCollected to posixPath & jobNumber & "-Collected/"
set shellCommand to "find " & quoted form of posixCollected & " -name \"*.zip*\" -exec rm {} \\;"
do shell script shellCommand


-- Move Parent Folder:Collected:Links to Parent Folder:Images and remove Parent Folder:Collected:Links folder
tell application "Finder"
	try
		set CollectedLinks to (folders of entire contents of folder parentFolder whose name is "Links") as alias list
	on error
		set CollectedLinks to (folders of entire contents of folder parentFolder whose name is "Links") as alias as list
	end try
end tell
set posixImages to posixPath & jobNumber & "-Images/"
repeat with aLink in CollectedLinks
	set shellCommand to "mv -f " & quoted form of (POSIX path of aLink) & "* " & quoted form of posixImages & "; rm -rf " & quoted form of (text 1 thru -2 of (POSIX path of aLink))
	do shell script shellCommand
end repeat


-- Move the Parent Folder:Collected:Fonts folder to Parent Folder:
tell application "Finder"
	try
		set CollectedFonts to (folders of entire contents of folder parentFolder whose name is "Fonts") as alias list
	on error
		set CollectedFonts to (folders of entire contents of folder parentFolder whose name is "Fonts") as alias as list
	end try
	make new folder at folder parentFolder with properties {name:(jobNumber & "-Fonts")}
end tell
set posixFonts to posixPath & jobNumber & "-Fonts/"
repeat with aFont in CollectedFonts
	set shellCommand to "mv -f " & quoted form of (POSIX path of aFont) & "* " & quoted form of posixFonts & "; rm -rf " & quoted form of (text 1 thru -2 of (POSIX path of aFont))
	do shell script shellCommand
end repeat


-- Move the Parrent Folder:Collected:InDesign files to Parrent Folder:
set shellCommand to "find " & quoted form of posixCollected & " -name \"*.indd*\" -exec mv -f {} " & quoted form of posixPath & " \\;"
do shell script shellCommand


tell application "Finder"
	-- Send Parent Folder:Comps folder to trash with dialog
	try
		if button returned of (display dialog "Would you like to delete the folder: " & jobNumber & "-Comps" buttons {"Yes", "No"} default button "Yes") is "Yes" then
			move folder (parentFolder & jobNumber & "-Comps:") to trash
		end if
	end try
	
	-- Remove Parent Folder:Copy folder
	set posixCopy to posixPath & jobNumber & "-Copy"
	set shellCommand to "rm -rf " & quoted form of posixCopy
	do shell script shellCommand
	
	-- Send Parent Folder:Reference folder to trash with dialog
	try
		if button returned of (display dialog "Would you like to delete the folder: " & jobNumber & "-Reference" buttons {"Yes", "No"} default button "Yes") is "Yes" then
			move folder (parentFolder & jobNumber & "-Reference:") to trash
		end if
	end try
end tell

Let me know how it works and if you have any questions =)

Putting the job number in front of the Fonts folder is a great idea. These job folders get archived on a server with many other job folders and keeping a number reference with the fonts folder is great in case it gets moved out of its Parent folder.

I’ll give it a try. Thanks for all your efforts. I’ll let you know the results.

Works like a charm! My next test is to run it on our Mac server. I don’t expect any problems other than deleting files. Right now it prompts with a warning that files will be permanently deleted. Not sure if this will interfere with the script.

Great work! :smiley:

Tried the script on project folders on the server. Not one problem!

You don’t know how much time and disk space you have helped me save!

Can’t thank you enough.

Always glad to help!

If you can try this version though… I cut back the amount of Finder calls as well brought it down to 1 shell call.

Also I bet I have a good idea… I’m the Sys Admin for an Advertising Agency and out workflow seems to resemble yours =)

tell application "Finder"
	-- Initialize
	set shellCommand to ""
	
	-- Choose the parent folder
	set parentFolder to (choose folder) as string
	
	-- Set up our base variables
	set parentFolderName to name of alias parentFolder
	set jobNumber to text 1 thru ((offset of "-" in parentFolderName) - 1) of parentFolderName
	set posixPath to POSIX path of parentFolder
	
	-- Remove all zip archives from Parent Folder:Collected
	set posixCollected to posixPath & jobNumber & "-Collected/"
	set shellCommand to shellCommand & "find " & quoted form of posixCollected & " -name \"*.zip*\" -exec rm {} \\;;"
	
	-- Move Parent Folder:Collected:Links to Parent Folder:Images and remove Parent Folder:Collected:Links folder
	set shellCommand to shellCommand & my move_remove(parentFolder, "Links", (posixPath & jobNumber & "-Images/"), shellCommand)
	
	-- Move the Parent Folder:Collected:Fonts folder to Parent Folder:
	make new folder at folder parentFolder with properties {name:(jobNumber & "-Fonts")}
	set shellCommand to shellCommand & my move_remove(parentFolder, "Fonts", (posixPath & jobNumber & "-Fonts/"), shellCommand)
	
	-- Move the Parrent Folder:Collected:InDesign files to Parrent Folder:
	set shellCommand to shellCommand & "find " & quoted form of posixCollected & " -name \"*.indd*\" -exec mv -f {} " & quoted form of posixPath & " \\;;"
	
	-- Send Parent Folder:Comps folder to trash with dialog
	try
		if button returned of (display dialog "Would you like to delete the folder: " & jobNumber & "-Comps" buttons {"Yes", "No"} default button "Yes") is "Yes" then
			move folder (parentFolder & jobNumber & "-Comps:") to trash
		end if
	end try
	
	-- Remove Parent Folder:Copy folder
	set posixCopy to posixPath & jobNumber & "-Copy"
	set shellCommand to shellCommand & "rm -rf " & quoted form of posixCopy & ";"
	
	-- Run the shell
	do shell script shellCommand
	
	-- Send Parent Folder:Reference folder to trash with dialog
	try
		if button returned of (display dialog "Would you like to delete the folder: " & jobNumber & "-Reference" buttons {"Yes", "No"} default button "Yes") is "Yes" then
			move folder (parentFolder & jobNumber & "-Reference:") to trash
		end if
	end try
end tell

on move_remove(parentFolder, nameIs, targetMove, shellCommand)
	tell application "Finder"
		try
			set sourceMove to (folders of entire contents of folder parentFolder whose name is nameIs) as alias list
		on error
			set sourceMove to (folders of entire contents of folder parentFolder whose name is nameIs) as alias as list
		end try
	end tell
	repeat with aMove in sourceMove
		set shellCommand to shellCommand & "mv -f " & quoted form of (POSIX path of aMove) & "* " & quoted form of targetMove & "; rm -rf " & quoted form of (text 1 thru -2 of (POSIX path of aMove)) & ";"
	end repeat
	return shellCommand
end move_remove

Thanks! I’ll run this version instead. I’ve implemented the script through Automator and saved it as a Finder Plug-in. I plan to deploy it to all our artist in an effort to keep our project files streamlined and ready for archiving. So far I’ve tested it on a project that was 1.93 Gig and got it down to 844 MB.

I’ve been thinking about this sort of script for a while and had tried piecing it together with my very limited scripting knowledge. I knew it was possible but you have saved me a boat load of time.

I’ll let you know how this latest version works. The last one just flies through the process. It’s amazing! Great job.

Alex

I tried the updated script you sent but it did not work. It ran as normal but when I checked to see if Fonts and Links had been moved from the Collected folder, they had not. I reverted back to the previous script and that one worked just fine.

I’m now having an issue with some of the artists who did not use the project folder creation script properly. They had deleted the number from the Parent Folder so the script encounters an Applescript error and does not run. Artist! Go figure. We’re trying to desperately find a consistent workflow that everyone follows but it’s hard to get some people to change and actually use some scripts that would help. We just migrated to working off an Mac Server and everyone needs to adjust to setting up their files and folders so that anyone can find things easily and consistently. This clean-up script was just next in the workflow to help people but as you can tell I’m running into problems when some have not used the initial script correctly.

Oh well.

Thanks for all your input. I’ll see if I encounter any other problems. But your previous version ran great. Just what I was envisioning. :smiley:

We had some problems similar with “Creatives” being resistant to change… it’s amazing how effective an AUP can be :smiley:

Or you can create a new scrip to standardize job folders :smiley:

Yes, I have a script that standardizes job folders when a Job is created. Is that what you mean? Or, write a script that will modify existing folder sets to conform to the new clean-up script?

Thats the one :smiley:

OK.

Can you tell me what the script should be looking for as an identifier? Because some artists have used the folder creation script but then deleted the number from the Parent folder or they just simply did not use the folder creation script at all.

I’m just real new to Applescript and I’m trying to understand some of the scripting definitions. I’ve been studying existing scripts to try and decipher the code and understand the function. I don’t want to take up more of your time But can you give me some guidance as to the two scenarios I mentioned above?

Thanks:D

well there are a few ways to go about it… What I would do to start is create a script that you run once a night/week on the file server that looks at each job folder and checks to make sure it starts with a number in the first place… then if it does check it’s sub-folders… are they all there? If so are they all named properly?

Eventually at the end you would have a list that contains all the “incorrect” job folders and you could then take that list to appropriate creatives and have them create a proper job folder and have them refile the contents of the old.

That would be a good place to start. So take a stab at that and post back what you come up with, I would be glad to lend you a hand :smiley: