Perhaps Bit of More than I can Chew! Please help...

:lol:

Hi there, please help if you can.

I’ve got several thousand files I need to process which are within various folders. They all have an eight digit code and then some form of extension usually after a dash (-) which is followed by the document type eg: 00056198-Spec Sheet.pdf 19908449-Core Artwork.ai 09902349-Core Artwork.eps etc.

There could be one file with the eight digit code or 4 or six all with different extensions.

What I want to do is move the files to another folder, take the eight digit code, make a new folder with this code and put those files with that code in it - so on and so forth…but I can’t get it to work.

I’m new to scripting (first attempt) and have trawled the net looking at other scripts (much appreciated) but haven’t got anything to work.

I’ve started with this script to test working with just one file but can’t get it to work.

on adding folder items to theFolder after receiving stuff
tell application “Finder”
with timeout of 500 seconds
repeat with aFile in myFiles
set folder_name to name of aFile --gets the name of the file
if length of folder_name > 8 then
set folder_name to (text 1 thru 8 of folder_name)
make folder with properties {name:folder_name} at theFolder
move aFile to folder {name:folder_name}
end if
end repeat
end timeout
end tell
end adding folder items to

I thought of using it as Folder Action and drop the files on to the folder so it could go and do the work, but it is to no avail. It’s probably quite simple but the answer escapes me. It’s the first time I’ve seriously looked at scripting something and I’ve found a new interest in it as I wasn’t aware so much could be done with it - however my interest is quickly getting replaced with frustration.

If anyone can help it would be greatly appreciated.

Many Thanks

DaddyCool76 (or not so cool when it comes to scripting!) :cry: [/b]

Ok, let’s see if we can fix this. Here’s your code with a couple of slight (untested) modifications.

on adding folder items to theFolder after receiving stuff
	tell application "Finder"
		with timeout of 500 seconds
			repeat with aFile in stuff
				set folder_name to name of aFile --gets the name of the file 
				if length of folder_name > 8 then
					set folder_name to (text 1 thru 8 of folder_name)
					set newFolder to make folder with properties {name:folder_name} at theFolder
					move aFile to newFolder
				end if
			end repeat
		end timeout
	end tell
end adding folder items to

If I may offer a further suggestion: you say there may be more than one file with the eight-digit code. In that case you’ll be trying to create the target folder more than once - which will give you an error and an unwanted folder named “untitled folder”. Where Rob’s script says:

… try instead:

if (exists folder folder_name of theFolder) then
  set newFolder to folder folder_name of theFolder
else
  set newFolder to make folder with properties {name:folder_name} at theFolder
end if

On my machine, the AppleScript compiler objects to the use of the parameter ‘stuff’ at the top of the handler, but I think this may be due to a terminology clash with one of my scripting additions.

Good catch Nigel! Thanks!

– Rob