process a file selected during loop of selection

Hi all,

I am trying to write a script to choose group of files from different directories using repeat loop the apply a task on selected files using a proper program.

I wrote first part of the script but every time I can only get the last selected file not all selected files. So, is there any way to improve or rewrite the script to get all selected files in order to be processed?

Thank you very much In advance for your help.

display dialog "How many files would you like to process?

" default answer "" buttons {"OK", "Cancel"} ¬
	with title ""

set variable to text returned of result

if variable = "" then
	display dialog "INVALID ENTRY! "
	error "User canceled." number -128
end if

repeat with loopvariable from 1 to variable
	set theFiles to (choose file with prompt "Select file number" & loopvariable of type {"pdf"})
end repeat

Hi, jsoamag. Welcome to MacScripter.

By default, the ‘choose file’ command only selects one file at a time. Your variable ‘theFiles’ is set to this each time round the repeat, so you either have to do something with it immediately (inside the repeat) or store the file in a list instead for processing later:

repeat with loopvariable from 1 to variable
	set theFile to (choose file with prompt "Select file number" & loopvariable of type {"pdf"})
	-- Do something with 'theFile'.
end repeat

Or:

set theFiles to {}
repeat with loopvariable from 1 to variable
	set end of theFiles to (choose file with prompt "Select file number" & loopvariable of type {"pdf"})
end repeat

repeat with loopvariable from 1 to variable
	-- Do something with item loopvariable of theFiles.
end repeat

If the files are all in the same folder and it doesn’t matter in which order they’re returned, you can get them all at once like this:

set theFiles to (choose file with prompt "Select the PDF files you want to process.." of type {"pdf"} with multiple selections allowed)

repeat with i from 1 to (count theFiles)
	-- Do something with item i of thefiles
end repeat

Thank you very much Nigel, and I am sorry for the delay thanking.

It worked with me, script will look like;


set theFiles to {}
set i to {}

display dialog "How many files would you like to process?

" default answer "" buttons {"OK", "Cancel"} ¬
	with title ""

set variable to text returned of result

if variable = "" then
	display dialog "INVALID ENTRY! "
	error "User canceled." number -128
end if

repeat with loopvariable from 1 to variable
	set theFiles to (choose file with prompt "Select file number" & loopvariable of type {"pdf"})
end repeat



repeat with i in theFiles
	
	set thePath2 to POSIX path of i
	tell application ""
		activate
		delay 3
		open thePath2
                -- Do something with i of theFile
	end tell
	
end repeat