Assign each item in a list to a sequentially numbered variable?

This sounds as if it should be easy, but I haven’t found an obvious way to do it.

I’m writing a script that will manipulate a list of POSIX filenames. I do not in advance how many filenames will be in the list, but probably no more than ten.

I want to assign each POSIX filename in the list to a different variable (for example, fileName1, fileName2, etc.). I can’t figure out a straightforward way to do this. Am I missing something obvious?

Many thanks for any help.

You already have them in a variable if they’re in a list. Just use:

(item i of mylist)

where i is a number from 1 to 10, or whatever the number of items is.

Thank you! That should have been obvious, but I didn’t realize that I could manipulate “item i” exactly as if it were a variable.

Actually, I do have a further question that is a variation on the original question:

My script needs to parse the POSIX paths in the list into filenames and paths so that I can display them in a dialog box.

What I don’t understand is how I can use (item i of mylist) so that I can parse it to produce a series of variables named fileName1 and filePath1, then fileName2 and filePath2, and so forth. So I think my question should be:

How can I create a series of sequentially numbered variables when I don’t know how many numbers I will need?

I hope I’ve expressed the problem clearly enough - if there’s another way to solve it, I’ll be grateful for any advice.

You don’t really need to know how many are in a list. You only need to loop through the entire list. Here’s an example that will create a list of posix paths from files in a folder. After doing that, it will loop through the list displaying the name of each file, without the path. Lastly, it will display mylist (going by item number) so that you can see the entire list of posix paths.

set mylist to {}
tell application "Finder"
	set thisfolder to choose folder
	set thefiles to (every file in thisfolder) as alias list
	repeat with eachfile in thefiles
		set end of mylist to POSIX path of eachfile -- Sets up list with posix paths of file names in a folder.
	end repeat
	repeat with eachpath in mylist -- Loops through list of posix paths, displaying the name of each file.
		set y to name of (POSIX file (eachpath as text) as alias)
		display dialog y as text
	end repeat
	set x to ""
	set thecount to count of items in mylist
	repeat with i from 1 to thecount
		set x to x & return & return & ((item i of mylist) as text)
	end repeat
	display dialog x
end tell

Use this on a folder with more than one file, but not too many!

This is getting closer, but I’m still puzzled about one thing, and I probably should have gone into more detail.

I want to display a dialog box that says (essentially)

"Your setup uses the following first file:

fileName1 in the folder filePath1

It also uses the following additional files: <–the rest of this appears only if there are more than 1 file

filename2 in the folder filePath2
filename3 in the folder filePath3

As far as I can tell, I need to be able to parse each item in the list into a series of variables so that I can display them in this dialog, then manipulate the files (by giving the user the chance to remove them from the list, add new ones to the list, etc.) and then display a similar list with the final result.

I know that I should have explained all this from the beginning, but I thought my initial question asked for what I was looking for. I’ll be very grateful for any further help.

One way to avoid making more lists would be functions that takes the index as arguments.
This is an example of what at file name function might look like.
I’m new to scripting, is “function” the word to describe the routine FileNameByIndex?

global myListOfFiles

on FileNameByIndex(i)
	-- returns the filename of item i of myListOfFiles
	set AppleScript's text item delimiters to {":"}
	return last item of text items of (item i of myListOfFiles)
end FileNameByIndex

-- DEMO routine
set myListOfFiles to ¬
	{"Macintosh HD:Users:merickson:Desktop:Crew Shows:BUZZ08.xls", ¬
		"Macintosh HD:Users:merickson:Desktop:Crew Shows:PDay08.xls", ¬
		"Macintosh HD:Users:merickson:Desktop:scrptColumnWidth.scpt"}

display dialog (item 2 of myListOfFiles) ¬
	& return & return ¬
	& FileNameByIndex(2) ¬
--Macintosh HD:Users:merickson:Desktop:Crew Shows:PDay08.xls

This will list single and multiple files differently. I’m not sure what you need posix paths for in this case. This script doesn’t use them.

tell application "Finder"
	set thisfolder to choose folder
	set thecount to count files in folder thisfolder
	if thecount > 1 then
		set thefiles to (every file in thisfolder) as alias list
		set x to name of (item 1 of thefiles) as text
		set Thedisplay to "Your setup uses the following first file: " & return & x & return & return & "It also uses the following additional files:" & return as text
		repeat with i from 2 to thecount
			set Thedisplay to Thedisplay & (name of (item i of thefiles) as text) & return
		end repeat
		display dialog Thedisplay
		set thefiles to (every file in thisfolder) as alias
		display dialog "Your setup uses the following file: " & return & thefiles
	end if
end tell

Thanks for that. The reason I start with POSIX paths is that I read a list of files from the configuration file of the BasiliskII or SheepShaver Mac emulators - and they use standard UNIX paths. So I get a list of paths from the .basilisk_ii_prefs file, and then offer the user the chance to add files, remove files, choose different ones, change the order, etc. Since the configuration file gives me a list of POSIX paths, it seemed easiest to continue using them.

The files can be in different folders - the only thing they have in common is that they’re listed in the preference files. So I build the list by extracting the paths from the preference file, and then manipulate it from there.

Wow! That’s some old stuff you’re dealing with.

Very old stuff: this is for setting up an emulator-based system that will let people who have old, password-protected files created in WordPerfect for the Mac open those files again in an old version of WordPerfect for the Mac, and then save the files to another format that can be read by modern software.

(, Abiword, MacLink Plus, etc., can open NON-password-protected WordPerfect files, but only WordPerfect for the Mac can open password-protected WordPerfect for the Mac files. I’m explaining this in detail in order to help prevent other people from wasting their valuable time posting replies saying that or MacLink etc. can open those files - because, to repeat, and MacLink, etc., cannot open WordPerfect files that were saved with password-protection. Also, the text content of those files can’t be read by a text editor, because password-protected WordPerfect files are also encrypted.)