This line doesn't look right to me:
filenameArray := 1 to: filesInSameFolder size collect: [:i | i].
That's going to make an array of integers from 1 to the number of files in the folder.
This should work:
filenameArray := filesInSameFolder asArray.
I just tried evaluating these expressions with the following results:
| filesInSameFolder filenameArray |
filesInSameFolder := 'count.aif' sampleFileNamesInSameFolder.
"Collect the filenames into an array."
filenameArray := 1 to: filesInSameFolder size collect: [:i | i].
filenameArray.
(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25)
and
| filesInSameFolder filenameArray |
filesInSameFolder := 'count.aif' sampleFileNamesInSameFolder.
"Collect the filenames into an array."
filenameArray := filesInSameFolder asArray.
filenameArray.
('Kyma 7 Folder:Samples:Speech:Alien threat.aif' 'Kyma 7 Folder:Samples:Speech:Alien threat2.aif' ...
So yes, "asArray" should work. The sampleFileNamesInSameFolder message returns an OrderedCollection and you can convert those to arrays using the asArray message.
The next thing is the "filesNamesInSameDirectory" message needs to be sent to a Filename object, not a string. In your code the variable "file" is a String.
Try converting it into a Filename after the Preferences expression:
file := file asFilename.
and then this should work (note it's ...Directory not ...Folder):
filesInSameFolder := file fileNamesInSameDirectory.