Selecting particular files

I need to replace an AppleScript prompt for the user to select a file with having the script automatically select a file. The file will have a slightly different name each time the script is run but it will start with the same two letters. The script will run and read information in the file.

I also need to repeat the select file until all files in this particular folder have selected. It will be the same folder each time.

Thanks in advance.

You can’t quite do what you want.

The standard ‘choose file’ command does support a ‘default location’ parameter which lets you define where the Open file dialog should start, but it only works on the folder level , not the file level (meaning it will open in the specified folder, but it will not highlight a specific file in that folder).

Are you sure you want to repeatedly ask the user to select files. I can see that this would get tedious for the user, especially if there’s a large number of files to process.

You can sort of do it by choosing a folder once, setting it’s contents to a list, then instead of allowing the user to choose the file using “choose file” use “choose from list” which does allow you to set the default item. Once chosen, you build the alias from the folder reference & name and then you can remove that item from the list. You can keep doing it until all the items are processed.

Jon

Let me see if I can explain this a little clearer…

The script takes the user selected file taht has variables in the file so it can process documents for publishing. As it is now, the user must manually select the file to be used. There potentially could be up to one hundred fifty files that would need to be selected, but typically there would be about twenty.

I want to automate the file selection process and do away with the user prompt or any other user interaction so this process can over night entirely from the script. The script should choose the first file then repeat the select file until every file in this folder has been chosen. The folder will be the same folder every time.

The spec’s were just updated. Instead of several files, there’s just one file. Each line in this file is the path to a control file for the script to process on. So the script will need to read the contents of file then pass each line as the path & file to be selected then repeat for all lines.

e.g.
The files contents would be something like…
path:to:file:file1
path:to:file:file2
path:to:file:file3

If I’m understanding the terminology, I want to extract each line as a variable for input into the script. I think it may be better to have one script that reads file then activates the other script and feeds it the variable.

Oh, well that’s much easier. Use the script below to process the input file. You can save it as a script application and then just drop the file with the list of input files as paragraphs on to its icon in the Finder. You’ll need to adjust the handler “process_file”, of course, to do something productive with each of the files, but this should get you going:

on run
	open {choose file with prompt "Locate the parameter file:"}
end run

on open the_parameter_file
	try
		if (count of the_parameter_file) > 1 then
			my display_message("Only drop a single file on this script. Please try again.", 0)
			return
		end if
		set the_parameter_file to the_parameter_file as string
		if character -1 of the_folder = ":" then
			my display_message("You dropped a folder. Please try again.", 0)
			return
		end if
		set the_input_files to paragraphs of (read file the_parameter_file)
		repeat with i from 1 to (count of the_input_files)
			my process_file(item i of the_input_files)
		end repeat
		my display_message(((current date) as string) & return & return & "Finished processing " & the_parameter_file & ".", 1)
	on error the_error
		beep
		my display_message(the_error, 0)
	end try
end open

on process_file(the_file)
	--do something with the file here:
	try
		set the_name to (name of (info for (the_file as alias)))
		my display_message("The name of this file is: " & the_name, 1)
	on error the_error
		my display_message(the_error, 0)
	end try
end process_file

on display_message(the_message, the_icon)
	activate
	display dialog the_message buttons {"OK"} default button 1 with icon the_icon
end display_message

Jon