Conditionally Selecting Items from an Alias List

I am trying to write a script that will select a file from a folder based on it’s name and then perform an action with that file.

For example:
My folder contains files with names that end in A1, A2, B1, B2, etc. I want to then say “if the file in the chosen folder ends with B1 then set it as my desktop image.”

I have this that I’ve been playing with but can’t figure out how to extract individual aliases based on their names.

tell application "Finder"
	set ImageList to (entire contents of (choose folder) as alias list)
end tell

Thanks.

This works for me (as an example):

tell application "Finder"
	set ImageList to (entire contents of (choose folder) as alias list) -- I chose a folder of camera pix.
	set pNames to {}
	repeat with k from 1 to count ImageList -- the pix end in 002x thru 005x
		set N to name of item k of ImageList
		-- grab the 3s and number the choices with reference to ImageList position
		if N contains "3" then set end of pNames to (k as text) & ") " & N
	end repeat
	set theChosen to choose from list pNames
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ")"
	set tNum to text item 1 of item 1 of theChosen
	set AppleScript's text item delimiters to tid
	set tFile to item (tNum as integer) of ImageList -- the desktop pic
end tell

Hi zpeletz,

Also, you may be able to use the filter reference form. Something like this:


set f to choose folder
tell application "Finder"
	every file of entire contents of f whose displayed name ends with "b1"
end tell

It might take longer than Adam’s script.

gl,

Model: MacBook Pro
AppleScript: 2.2.3
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)