Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 2, 2012, 11:51 AM   #1
jtcdesigns
macrumors newbie
 
Join Date: Dec 2008
AppleScript to search for folder name

I'm pretty new to AppleScript but not new to programming. I've recently come across a project where I think scripting will be the best idea.

A user puts files in a folder and puts the job number as the name of the folder. They drop this folder into a release folder.. this then triggers the script to detect the job number and move that folder to an archive folder.

Example: user drops folder "103945" into release folder. The script detects that it starts with "103" and moves it into the archive folder "103000"

I hope its possible to do, It would be good if the script can detect the first 3 digits to the job number and then search archives for the folder starting with the same first 3 digits. Then move that folder into the folder on archives. And if possible.. if the folder doesn't exist then it creates a folder by the first 3 digits of the job number plus 000
jtcdesigns is offline   0 Reply With Quote
Old Apr 2, 2012, 04:45 PM   #2
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
This should get you started

The script wil move the folders like you wanted. The only thing still missing
is this part "if the folder doesn't exist then it creates a folder by the first 3 digits
of the job number plus 000". Got something else to do right now.
Will get back when I finish my other work. This is a droplet not a folder action. Script has some errors, sorry for that.

Code:
global FolderName
global this_folder
global archive_item_info
-- This droplet processes folders dropped onto the applet 
on open these_items
	repeat with i from 1 to the count of these_items
		set this_item to item i of these_items
		set the item_info to info for this_item
		copy displayed name of item_info to FolderName
		--	set FolderName to text 1 thru 3 of FolderName
		if (alias of the item_info is false) and (folder of the item_info is true) then
			process_item(this_item)
		end if
	end repeat
end open

-- this sub-routine processes folders 
on process_item(this_item)
	-- NOTE that the variable this_item is a folder reference in alias format 
	-- FOLDER PROCESSING STATEMENTS GOES HERE
	--CHANGE THE LINE BELOW TO YOUR ARCHIVES FOLDER !
	set archivesFolder to (path to desktop as text) & "Archives" as alias
	--	set archivesFolderList to list folder archivesFolder
	tell application "Finder"
		set archivesFolderListAlias to (entire contents of (archivesFolder) as alias list)
	end tell
	repeat with aFolder from 1 to the count of archivesFolderListAlias
		set this_folder to item aFolder of archivesFolderListAlias
		set archive_item_info to info for this_folder
		copy displayed name of archive_item_info to archiveFolderName
		if archiveFolderName contains text 1 thru 3 of FolderName then
			tell application "Finder"
				move this_item to this_folder
			end tell
		end if
	end repeat
end process_item

Last edited by kryten2; Apr 2, 2012 at 07:06 PM.
kryten2 is offline   0 Reply With Quote
Old Apr 3, 2012, 07:22 AM   #3
jtcdesigns
Thread Starter
macrumors newbie
 
Join Date: Dec 2008
I appreciate the help. I'll look over this with my coworker and see if we can iron out the errors.
jtcdesigns is offline   0 Reply With Quote
Old Apr 3, 2012, 08:50 AM   #4
jtcdesigns
Thread Starter
macrumors newbie
 
Join Date: Dec 2008
would there be a way a windows user could use a droplet? I probably should have posted that. We use an automation program called FullSwitch. What I can do in that is setup some canned rules that say if job name begins with "103" then it goes to folder "103000". The only problem is I'll have to create those folders ahead of time. That is why I thought going to a script may be better because it will detect the name and put it in its right folder. So I basically just need a script that I can have FullSwitch run everytime a folder is placed in a folder.

I like the droplet idea, I think I may actually use that for use at home with my photography.. everytime I get a new project I have to copy the files over to a certain location.. I could just make it automatic

I may have to for now just make a bunch of folders ahead of time so users can just drop in the folders into one location that switch monitors until I can get a script to automate the task.
jtcdesigns is offline   0 Reply With Quote
Old Apr 3, 2012, 05:30 PM   #5
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
My apologies once again for the provided script. I made it very quickly without
thoroughly testing it. I don't know if a window user could use a droplet or not.
Making a bunch of folders on a mac should only take a few seconds for example :
mkdir {1..999}000 will make folders 1000 to 999000 in a flash.
You can try posting your problem on http://stackoverflow.com/questions/tagged/applescript
The guys there really know their stuff and should provide you with a working
script in no time.
kryten2 is offline   0 Reply With Quote
Old Apr 4, 2012, 09:18 AM   #6
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Back again with working folder action

As usual I made things more complicated then they were with my first attempt. Two things needed for this. First thing : your release folder with this script attached to it. Second thing : An empty archives folder. Folders will be created by the first 3 digits of the job number plus 000.


Code:
on adding folder items to source_folder after receiving these_items
	-- insert actions here
	if folderReady(source_folder) then
		--CHANGE THE LINE BELOW TO YOUR ARCHIVES FOLDER !
		set archivesFolder to (path to desktop as text) & "Archives" as alias
		repeat with i from 1 to number of items in these_items
			set this_item to item i of these_items
			-- insert actions here
			set the item_info to info for this_item
			copy displayed name of item_info to FolderName
			set FolderNamePref to text 1 thru 3 of FolderName
			set archivesFolderCheck to (archivesFolder as text) & FolderNamePref & "000" --as alias
			if (alias of the item_info is false) and (folder of the item_info is true) then
				tell application "Finder"
					if exists archivesFolderCheck then
						set archivesFolderCheck to archivesFolderCheck as alias
						--					duplicate this_item to archivesFolderCheck
						move this_item to archivesFolderCheck
					else
						set new_folder to make new folder at archivesFolder with properties {name:FolderNamePref & "000"}
						--					duplicate this_item to new_folder
						move this_item to new_folder
					end if
				end tell
			else
				display dialog "I only process folders" buttons {"OK"} default button 1
			end if
		end repeat
	end if
end adding folder items to

on folderReady(tFolder)
	set myFolder to tFolder as alias -- use the path to the folder that's loading
	set firstSize to size of (info for myFolder) --get initial size
	delay 5 --wait 5 seconds
	set newSize to size of (info for myFolder) --get a newer size, bigger
	repeat while newSize ≠ firstSize --if they don't equal, loop until they do
		set firstSize to newSize --new base size
		delay 3 --wait three seconds
		set newSize to size of (info for myFolder) --get a newer size
	end repeat --once the sizes match, the transfer is complete
	return true
end folderReady
After a few more tests I noticed a problem. When copying 15 folders only 7 get moved. I'm on a Leopard machine and I've seen other people also on Leopard report strange and erratic behaviour with folder actions. Going to test this on a Tiger install.

I've just done a test on Tiger and the script works flawlessly. Tested with 20 folder with 500 MB of data in them. If someone else on Leopard could test this I would be grateful for the feedback.

Last edited by kryten2; Apr 4, 2012 at 04:06 PM. Reason: Added check to start processing after all folders are copied.
kryten2 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Copy files to folder using Applescript Kitkat88 Mac Programming 7 Jun 23, 2014 07:45 AM
Applescript Is there a way to search for a time or date withi text wild4life2013 Mac Programming 5 Sep 20, 2013 12:35 PM
I want to make AppleScript Create Folder DavidBlack Mac Programming 27 Mar 15, 2013 06:18 PM
Automator / Applescript > copy to folder mynameismatt89 Mac Programming 2 Feb 6, 2013 04:32 PM
applescript search for person by email mieslep Mac Programming 2 Oct 12, 2012 02:25 PM

Forum Jump

All times are GMT -5. The time now is 05:36 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC