moving files in a folder-actioned folder to it's container

Recently I downloaded about a gig of comics, and I didn’t like the way the directory was set up. So instead of manually moving the files out of each chapter folder to the book folder (there were over 250 folders with 20-30 files in each) I wanted to try making a script to move them for me.
Now, I’m no coder, but I know boolean logic and such and had tried apple script in os-9, simple stuff like setting the background picture and such. So I figured I’d use what I remembered and attach a folder action to each folder, then open them to trigger the file movement. But it doesn’t seem to want to work the same as os-9.
The directory is such:
comic-> volumes (around 20) → chapters (8-ish per vol.) → pics (20-30)

I’ve gone through the help guide and the script dictionaries, but I can’t figure out why at least one of these doesn’t work.

method one

on opening folder this_folder
	tell application "System Events"
		activate
		set these_files to every file of this_folder
		repeat with i from 1 to the count of these_files
			set this_file to (item i of these_files as alias)
			set this_info to info for this_file
			if visible of this_info is true and alias of this_info is false then
				move this_file to container of this_folder
			end if
		end repeat
	end tell
end opening folder

#2

on opening folder this_folder
	tell application "Finder"
		count files in this_folder
		repeat result - 1 times
			move second file of this_folder to parent of this_folder (*first file being .ds_store*)
		end repeat
	end tell
end opening folder

I finally used automator for the first time to move those files, but now I’m curious about the solution.
Any help would be appreciated.

Hi and welcome to MacScripter.

Do you really want to trigger the script, when the folder is opened?
I guess, the files should be moved when they are added to the folder.

You can simply move all added items to the parent folder with:

on adding folder items to thisFolder after receiving theseItems
	tell application "Finder" to move theseItems to container of thisFolder
end adding folder items to

Unlike System Events, the Finder doesn’t move invisible items, so the .DS_Store file is untouched

Hi, Stonebrow.

Besides what Stefan’s written, the simplest form of the Folder Action script you were trying would be:

on opening folder this_folder
	tell application "Finder"
		move this_folder's files to this_folder's container
	end tell
end opening folder

The Finder doesn’t move invisible files, though it might create a “.DS_Store” itself at the destination. There’s not much you can do about that.

Folder Action scripts have to be in a folder called “Folder Action Scripts” in the “Scripts” folder in your user “Library” folder. The scripts must be “attached” to the folders concerned and Folder Actions must be enabled in System Events.

If any files in the folder you’re opening have the same names as files that have been moved to the container already, the script will error.

It would be far more satisfactory to write a single script that did the whole job, rather than attaching this one to (around 20) * (8-ish) different chapter folders!

Thanks! I don’t know why it didn’t occur to me to move every file instead of trying to move each one. It also turns out that since I was using some 3rd party software that made invisible files visible, the .ds_store actually was causing problems. And as for why I used on opening to trigger it, I ust was trng to usha I remembered.