spotlight filter results by type

I’m trying to get a list of all jpg’s with the name that contains 9159_02_002_115


--set theList to {}
set tempList to {}
set thetext to "9159_02_002_115"
set thePath to quoted form of POSIX path of (choose folder with prompt "Set the volume to SEARCH")
try
	set Titem to "*" & thetext & "*"
	set Theitem to quoted form of Titem
	set spotlightquery to "\"kMDItemFSName == " & Theitem & "\""
	set command to "mdfind -onlyin " & thePath & " " & spotlightquery
	set founditems to paragraphs of (do shell script command)
	set itemcount to count of items of founditems
	if itemcount = 1 then
		set Ext to (info for founditems)
		if Ext's name extension is "jpg" then
			set itemList to founditems
			display dialog itemList as text
		end if
	else
		repeat with temp in founditems
			set Ext to (info for temp)
			if Ext's name extension is "jpg" then
				set tempList's end to Ext
			end if
		end repeat
		set itemList to choose from list tempList default items item 1 of tempList with multiple selections allowed
	end if
	--set theList's end to itemList
end try

Help please

I’ve altered the do shell script so the code below isn’t needed. The output of mdfind is piped to grep and only the lines that ends with “.jpg” are printed. The option -i means grep is case insensitive.

set tempList to {}
set thetext to "IMG"
set thePath to quoted form of POSIX path of (choose folder with prompt "Set the volume to SEARCH")
set Titem to "*" & thetext & "*"
set Theitem to quoted form of Titem
set spotlightquery to "\"kMDItemFSName == " & Theitem & "\""
set command to "mdfind -onlyin " & thePath & " " & spotlightquery
every paragraph of (do shell script command & " | grep -i '.*\\.jpg$'")

You Rock
Thank You

Hello.

I am not sure if everybody knows it, and it is somewhat related, so I’ll just state it:

In Spotlight, you can start a query with name: in the Spotlight bar, and the start writing something from the filename, and you should get the results up. This works at least from Snow Leopard onwards. I have never tried to filter by volume though.