Help needed: search finder through applescript

Hi Everyone,

I’m looking for a way to use the search field in the top right corner of the finder through applescript.
I couldn’t find any articles on this task online so I hope you can help me.

the idea is that I can create a search query in finder through applescript.
so say I want to search for all pictures associated with the word “mango” in the folder “fruits”.

I haven’t got a lot of experience with applescript…

my OSX is 10.6

your help would be very much appreciated.

Kind regards,

J. Aanen

Hi,
you can search with Spotlight using the shell command mdfind.

For example this script searches the main library for all images


do shell script "mdfind -onlyin " & quoted form of POSIX path of (path to library folder) & " 'kMDItemContentTypeTree == \"*image*\"'"

Look also here

Hi Stefan,

Thank you for your quick reply.
I know this code, it returns the query in the result window, however I need it to show the selected files in the finder window.
and one other thing. this search only returns literal matches, such as the exact file name if type specification. I i use spotlight it also searches through the metadata of files such as keywords assigned in photo library applications such as Picasa.
I hope you can help me with these matters.

Kind regards,

Joost

A behavior like in a Finder window is not possible with AppleScript unless you create a custom .search file (which is actually a xml file) place it in the appropriate folder and select it in the Finder window like a smart search section.
but this is quite complicated.

The search parameters of mdfind can be combined in various ways, the result is always a list of file paths

I’m not sure that I understood well your question but maybe this piece of code may help :


set le_dossier to (path to desktop as text) & "Plants"
tell application "Finder"
	set mes_fichiers to every file of folder le_dossier whose name contains "leave"
	if (count of mes_fichiers) > 0 then
		reveal (mes_fichiers as alias list)
	end if
end tell

As I have no folder named Fruits containing files related to mangos, I tested on a folder named Plants containings some pictures of leaves.

Yvan KOENIG (VALLAURIS, France) jeudi 24 janvier 2011 17:14:26