Generate a list from folder, prompt list

I can make a manual prompt list but wondered if i could make a list from the contents of a folder.

This is what i would like to happen

A folder contains 1+ text files
I would like to generate a prompt list that reads this folder and inserts the filename in the list
When the user clicks the filename then it reads the details of the files and saves the information.

I can do most of this but am at a loss as to how to make the list in the ifrst place.

Do i read the folder as a list and then display the propmt?? as i can do that, but how do i set a variable that reads the choosen file??

You can use the magic command “list folder”.
This code assumes that there are only files in the folder, so you avoid trying to read a folder. If you are not sure, you can use instead “find”'s shell command, looking only for files and excluding directories (but that is another question):

set theFolder to alias "path:to:folder:"
choose from list (list folder theFolder)
if result ? false then read file ((theFolder as text) & result)

Here’s another way


set filePath to choose folder
set thelist to list folder filePath
try
	set oldTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to space
	set thelist to text items of hidFiles as list
	set AppleScript's text item delimiters to oldTIDs
on error
	set AppleScript's text item delimiters to oldTIDs
end try

set theChosen to (choose from list thelist OK button name "OK" without multiple selections allowed and empty selection allowed)
if theChosen = false then return

set theChosen to (filePath as string) & theChosen as alias
set hello to read theChosen as list using delimiter return
display dialog hello

I have this and it works, is this the tidiest way to do it??

Greg!
Your code will allways throw an error, since hidFiles should be allways an undefined var (unless I’m missing something). What are you trying with the tid’s stuff? (I’m puzzled with this)

hmm, yeah that is weird as this works:


set filePath to choose folder
set thelist to list folder filePath


set theChosen to (choose from list thelist OK button name "OK" without multiple selections allowed and empty selection allowed)
if theChosen = false then return

set theChosen to (filePath as string) & theChosen as alias
set hello to read theChosen as list using delimiter return
display dialog hello

Hmmmmmm…

also i have a bit of a problem.

If i set it so that i can choose more than one file and try and display the list then it goes tits up.

What am i doing wrong??? i need it to be able to choose more than one file.

I have solved it. This snippet will read a folder and list in propmt box, when you choose 1 or more text files of format
hannehd,ajsuije,ajsia

i.e. a single line

It will read this line and return the value no matter how many files have been selected, have choosen max of 3 but works with that.


set filePath to choose folder
set thelist to list folder filePath

set fileList to {}

set theFiles to (choose from list thelist OK button name "OK" with multiple selections allowed and empty selection allowed)
if theFiles = false then return
set fileCount to count theFiles
repeat with i from 1 to fileCount
	
	set theFile to (filePath as string) & item i of theFiles as alias
	set theKeys to read theFile as string
	copy theKeys to end of fileList
	
end repeat

display dialog fileList as string

Cheers for the help guys!!

That code I posted earlier was a copy/paste from an existing script, I forgot to edit out the TIDs. I edited the post – sorry 'bout that.