Hi,
I must tell you that I don't really understand spoken English, but I understand English better when I read it.
I must tell you that the folder actions are not reliable, but it depends on many factors.
1- Depending on the time to copy the item dropped in this folder.
2- Depending on the number of files dropped or created at the same time in this folder.
3- Depending on the time interval between the creation of two images.
4- Depending on the duration that take the script to complete its task.
5- Depending on the number of script or workflow activated to this folder action.
6- There are slightly more problem with Automator.
7- Depending on unknown factors on some systems.
Just testing to see if some script works for you.
Here's what I understood.
The script must search a subFolder by the beginning of its name, but it can be in different level.
Example: the fileName is "bigmac_ld_4panel.png"
The script must find (recursively descends the directory tree) a subfolder whose name starts with "4_panel_"
After that, I guess the name of the destination folder is the exact name ("ld" in this example).
I also assume that the "ld" folder is in the "4_panel_xxxxxx" folder (not in another level).
Here is the script, use it as a AppleScript folder action, but not in Automator :
---------------------------
property nameList : {"_4panel.png", "_5panel.png"} -- the end of the fileName to check
property subFoldersList : {"4_panel_", "5_panel_"} -- the beginning of the folderName to search
on adding folder items to masterFolder after receiving these_items
set masterPath to quoted form of POSIX path of masterFolder
repeat with tFile in these_items
tell application "System Events" to set tName to name of tFile
set {subName, destName} to my checkName(tName)
if destName is not "" then -- this file name is in the list
-- search (depth levels) the subfolder whose name start with subName and that folder contains the destination folder.
set f to do shell script "/usr/bin/find " & masterPath & " -type d -regex " & (quoted form of (".*/" & subName & ".*/" & destName))
-- if it didn't find anything or it found two or more subfolders then do nothing
if f is not "" and (count of paragraphs of f) = 1 then
set destFolder to (POSIX file f) as alias
tell application "Finder" to move tFile to destFolder with replacing --move this file
end if
end if
end repeat
end adding folder items to
on checkName(tName)
set tc to count nameList
repeat with i from 1 to tc
if tName ends with (item i of nameList) then -- this Name is in the list
-- return the beginning of the subFolder name and the name of the destination folder
return {item i of subFoldersList, my getNameOfDestinationFolder(tName, item i of nameList)}
end if
end repeat
return {"", ""} -- this name is not in the list
end checkName
on getNameOfDestinationFolder(thisName, theEndOfName) -- e.g. this Name = "bigmac_ld_4panel.png"
set tName to text 1 thru -((count theEndOfName) + 1) of thisName -- get text before "_4panel.png" --> "bigmac_ld"
if "_" is not in tName then return "" -- no underscore
set {oTID, text item delimiters} to {text item delimiters, "_"}
set r to last text item of tName -- get text after last underscore --> ld
set text item delimiters to oTID
return r
end getNameOfDestinationFolder
---------------------
You must add the names in the lists (first line and second line of this script) according to your needs.
The underscore at beginning of each item in the first list is important, It's to find the name of the destination folder.
The items in these two lists must absolutely match (the index).
Example: the third item in the first list (the end of the file name) must match the third element in the second list (the beginning of the subfolder name ).
--------------------------------
I don't know if it suits you, but it can be interesting.
I have a small suggestion about your naming convention, the script would be easier and you will not have to fill the lists in the script.
Example :
Instead of renaming the file like this "bigmac_ld_4panel.png", If you rename like this "bigmac;ld;4_panel_.png" or "bigmac_;ld;4_panel_.png".
"4_panel_" is the exact text to search for a subdirectory whose name starts with that.
"ld" is the exact name of the destinaton folder.
So, the script looks in the file name:
The text between the semicolon character and the name extension. (It's the beginning of the subfolder name).
The text between the two semicolon characters. (It's the name of the destination folder).
Here is the script for this naming convention.
---------------------------
property nameExtensionList : {"png", "tif"} -- list of name extension that the script will be processed..
on adding folder items to masterFolder after receiving these_items
set masterPath to quoted form of POSIX path of masterFolder
repeat with tFile in these_items
tell application "System Events" to set {name:tName, name extension:nExtension} to tFile
if nExtension is in nameExtensionList then
set {destName, subName} to my checkName(tName, nExtension)
if destName is not "" and subName is not "" then
-- search (depth levels) the subfolder whose name start with subName and that folder contains the destination folder.
set f to do shell script "/usr/bin/find " & masterPath & " -type d -regex " & (quoted form of (".*/" & subName & ".*/" & destName))
-- if it didn't find anything or it found two or more subfolders then do nothing
if f is not "" and (count of paragraphs of f) = 1 then
set destFolder to (POSIX file f) as alias
tell application "Finder" to move tFile to destFolder with replacing --move this file
end if
end if
end if
end repeat
end adding folder items to
on checkName(thisName, nameExt) --e.g. name = "bigmac_;ld;4_panel_.png"
set r to {"", ""}
set tBaseName to text 1 thru -((count nameExt) + 2) of thisName -- remove name extension --> "bigmac_;ld;4_panel_
if ";" is not in tBaseName then return r
set {oTID, text item delimiters} to {text item delimiters, ";"}
-- to get text between semicolon character --> "ld" and "4_panel_"
tell (text items of tBaseName) to if (count) > 2 then set r to items -2 thru -1 -- two semicolons minimum
set text item delimiters to oTID
return r
end checkName