script with odd response

In the following script:

tell application "Finder"
	set theFolder to folder choose folder with prompt "Choose the folder to sort"
	repeat while (count of ((document files in folder theFolder))) is not 0
		set theFile to first document file of theFolder
		set theFileName to name of theFile
		set AppleScript's text item delimiters to "_"
		set newFolderName to text item 1 of theFileName & "_" & text item 2 of theFileName & "_" & text item 3 of theFileName & "_" & text item 4 of theFileName
		set AppleScript's text item delimiters to ""
		if exists folder newFolderName in theFolder then
			set theNewFolder to newFolderName
		else
			set theNewFolder to (make new folder at theFolder with properties {name:newFolderName})
		end if
		set moveList to {}
		set moveList to (every document file of theFolder whose name starts with newFolderName & "_")
		if moveList is not {} then
			repeat with theDocument in moveList
				move theDocument to theNewFolder
			end repeat
		end if
	end repeat
end tell

It works if theNewFolder doesn’t exist, but not if it does - with the rseponse below

tell application “Finder”
choose folder with prompt “Choose the folder to sort”
alias “WD400:1091DA_TRSHS:”
count every document file of folder (alias “WD400:1091DA_TRSHS:”)
2
get document file 1 of alias “WD400:1091DA_TRSHS:”
document file “A802P_SP170_30_test_041.TIF” of folder “1091DA_TRSHS” of disk “WD400”
get name of document file “A802P_SP170_30_test_041.TIF” of folder “1091DA_TRSHS” of disk “WD400”
“A802P_SP170_30_test_041.TIF”
exists folder “A802P_SP170_30_test” of alias “WD400:1091DA_TRSHS:”
true
get every document file of alias “WD400:1091DA_TRSHS:” whose name starts with “A802P_SP170_30_test_”
{document file “A802P_SP170_30_test_041.TIF” of folder “1091DA_TRSHS” of disk “WD400”, document file “A802P_SP170_30_test_042.TIF” of folder “1091DA_TRSHS” of disk “WD400”}
move document file “A802P_SP170_30_test_041.TIF” of folder “1091DA_TRSHS” of disk “WD400” to “A802P_SP170_30_test”
“Finder got an error: Can’t get item 1 of {document file "A802P_SP170_30_test_041.TIF" of folder "1091DA_TRSHS" of disk "WD400", document file "A802P_SP170_30_test_042.TIF" of folder "1091DA_TRSHS" of disk "WD400"}.”

Why, and how do I fix it?

Hi,

if the folder exists, you just pass the name of the folder, not the file specifier
try this


tell application "Finder"
	set theFolder to folder choose folder with prompt "Choose the folder to sort"
	repeat while (count of ((document files in folder theFolder))) is not 0
		set theFile to first document file of theFolder
		set theFileName to name of theFile
		set AppleScript's text item delimiters to "_"
		set newFolderName to text items 1 thru 4 of theFileName as text
		set AppleScript's text item delimiters to ""
		if exists folder newFolderName in theFolder then
			set theNewFolder to folder newFolderName of theFolder
		else
			set theNewFolder to (make new folder at theFolder with properties {name:newFolderName})
		end if
		move (every document file of theFolder whose name starts with newFolderName & "_") to theNewFolder
	end repeat
end tell

Thanks, that sorted it!