Rename files to a folder name, save to different folder & add prefix

I really need you help and will be very grateful for it. I think this should be no problem for you.

So I have folder with different subfolders in it. In subfolders are images. What I need is:

Change each image name to it’s subfolder name + 001 and so on («1stSubfolder001, 1stSubfolder002, .», «2ndSubfolder001, 2ndSubfolder002, .»)
Move all images from all subfolders to one folder (or at least to root folder)
Add random prefix number to names.
I have script to third task:

But it only works with opened folder on the foreground. Maybe there is a way to make one script to run it on the background?

Huge thank you in advance.

I found cool automator app on stackoverflow, but I need to correct it a little bit.

This script does everything I need BUT it renames all files to ONE folder name, not each different.

Here is another applescript that does rename files to folders, but it uses 2 folders name (folder + subfolder), and I need only subfolder prefix.

(MacBook Pro Late 2013, OS X Yosemite 10.10.1)

Guys, anyone?

Hi bagrov. Welcome to MacScripter.

Here’s a script which uses the Finder. It’s not particularly fast, but I think it does what you want. The random-number prefixes are included with the renaming of the files inside the subfolders, so that the renaming only needs to be done once. There’s no name checking, so if you run the script on a subsequent occasion and it happens to generate a name which has already been given to a file in the root folder, it’ll error out. I don’t know what you want to do in such a case. Try generating another name or overwrite the existing file?

set rootFolder to (choose folder)

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."

tell application "Finder"
	set subfolders to rootFolder's folders
	repeat with thisSubfolder in subfolders
		set subfolderName to thisSubfolder's name
		set theseFileNames to name of thisSubfolder's files
		repeat with i from 1 to (count theseFileNames)
			-- Get this file's current name.
			set thisName to item i of theseFileNames
			-- Extract the extension.
			if (thisName contains ".") then
				set extn to "." & text item -1 of thisName
				set extn to ""
			end if
			-- Generate a five-digit random number as text.
			set prefix to text 2 thru 6 of ((100000 + (random number 99999)) as text)
			-- Get a three-digit postfix number based on the repeat index. 
			set postfix to text 2 thru 4 of ((i + 1000) as text)
			-- Put together the new name.
			set newName to prefix & subfolderName & postfix & extn
			-- Rename the file.
			set name of file thisName of thisSubfolder to newName
		end repeat
		-- Move all the files from this subfolder to the root folder.
		move thisSubfolder's files to rootFolder
	end repeat
end tell

set AppleScript's text item delimiters to astid

beep 3 -- To signal completion.

Edit: Typo in script corrected.

Oh my god thank you very much!
You are a lifesaver!