Spotlight search with applescript

Hi there

How can I do a Spotlight search in a specific folder with applescript?

The return value should be a list with references to the items found by spotlight…

thank for your help
simon

Hi Simon,

You can do this by combining the «mdfind» and «do shell script» command. Here is an example to search the desktop folder for PDF files (suffix = *.pdf):


set desktoppath to quoted form of POSIX path of (path to desktop)
set spotlightquery to "\"kMDItemFSName == '*.pdf'\""
set command to "mdfind -onlyin " & desktoppath & " " & spotlightquery
set founditems to paragraphs of (do shell script command)

If you need to learn about the Spotlight Query Expression Syntax, then please point your browser to this URL:

Spotlight Query Programming Guide: Query Expression Syntax

In case you want to list the metadata properties available for a certain file or folder, then you can use the «mdls» command:


set filepath to quoted form of POSIX path of ((path to preferences folder from user domain as Unicode text) & "com.apple.mail.plist")
set command to "mdls " & filepath
set metadata to (do shell script command)

There’s a tutorial on using Spotlight from AppleScript here

and some more examples here

Ultimate?

set spotlightquery to quoted form of text returned of (display dialog "What do you want to search?" default answer "" buttons {"Search!"} default button 1)
set thefolders to {"Documents", "Downloads", "Desktop", "Movies", "Pictures", "Music"}
set founditems to {}
repeat with i in thefolders
	set thepath to quoted form of POSIX path of ((path to home folder as string) & (i as string) as string)
	if exists thepath then
		set command to "mdfind -onlyin " & thepath & " " & spotlightquery
		set founditems to founditems & (paragraphs of (do shell script command))
	end if
end repeat
repeat with i in founditems
	set i to i as string
	set thesize to size of (get info for (POSIX file i))
	set thekind to kind of (get info for (POSIX file i))
	set thename to displayed name of (get info for (POSIX file i))
	set creation to creation date of (get info for (POSIX file i))
	set visibility to visible of (get info for (POSIX file i))
	set dialog to "Name: \"" & thename & "\"" & return & return & "Kind: " & thekind & return & return & "Size: " & thesize & " bytes" & return & return & "Creation Date: " & (creation as string) & return & return & "Visibility: " & (visibility as string) & return & return & "Path: " & i
	set button to button returned of (display dialog dialog buttons {"End", "Open", "Next"} cancel button {"End"} default button {"Next"} with title "Your search for \"" & spotlightquery & "\"")
	if button is "Open" then
		try
			set theapp to default application of (get info for (POSIX file i)) as string
			tell application theapp to open (POSIX file i as string)
		on error e
			display dialog "An error has occured trying to open your file:" & return & return & e buttons {"OK"} default button 1
		end try
		exit repeat
	end if
end repeat

almost :wink:

Hi,

all coercions are not needed, path to returns an alias which has a POSIX path property.
This does the same

set thepath to quoted form of (POSIX path of (path to home folder) & i)

Now is it ultimate?

set spotlightquery to quoted form of text returned of (display dialog "What do you want to search?" default answer "" buttons {"Search!"} default button 1)
set thefolders to {(path to applications folder as string), (path to home folder as string) & "Documents", (path to home folder as string) & "Downloads", (path to home folder as string) & "Desktop", (path to home folder as string) & "Movies", (path to home folder as string) & "Pictures", (path to home folder as string) & "Music"}
set founditems to {}
repeat with i in thefolders
	set thepath to quoted form of POSIX path of (i as string)
	if exists thepath then
		set command to "mdfind -onlyin " & thepath & " " & spotlightquery
		set founditems to founditems & (paragraphs of (do shell script command))
	end if
end repeat
set numb to 1
repeat
	set i to item numb of founditems as string
	set thesize to size of (get info for (POSIX file i))
	set thekind to kind of (get info for (POSIX file i))
	set thename to displayed name of (get info for (POSIX file i))
	set creation to creation date of (get info for (POSIX file i))
	set visibility to visible of (get info for (POSIX file i))
	set dialog to "Name: \"" & thename & "\"" & return & return & "Kind: " & thekind & return & return & "Size: " & thesize & " bytes" & return & return & "Creation Date: " & (creation as string) & return & return & "Visibility: " & (visibility as string) & return & return & "Path: " & i & return
	set button to (choose from list {"Open", "Back", "Next", "End"} with prompt dialog with title "Your search for \"" & spotlightquery & "\"") as string
	if button is "Open" then
		try
			set theapp to default application of (get info for (POSIX file i)) as string
			tell application theapp to open (POSIX file i as string)
			activate application theapp
		on error e
			display dialog "An error has occured trying to open your file:" & return & return & e buttons {"OK"} default button 1
		end try
		exit repeat
	else if button is "Back" then
		if numb is not 1 then
			set numb to numb - 1
		end if
	else if button is "Next" then
		if numb is not (count founditems) then
			set numb to numb + 1
		end if
	else if button is "End" then
		exit repeat
	end if
end repeat
if founditems is {} or founditems is {""} then
	display dialog "No search results!" buttons {"OK"} default button 1 with title "Your search for \"" & spotlightquery & "\""
end if

my ultimate version without any useless coercion :slight_smile:


set spotlightquery to quoted form of text returned of (display dialog "What do you want to search?" default answer "" buttons {"Search!"} default button 1)
set thefolders to {path to applications folder, path to documents folder, path to downloads folder, path to desktop folder, path to movies folder, path to pictures folder, path to music folder}
set founditems to {}
repeat with i in thefolders
	set thepath to quoted form of POSIX path of i
	if exists thepath then
		set command to "mdfind -onlyin " & thepath & " " & spotlightquery
		set founditems to founditems & (paragraphs of (do shell script command))
	end if
end repeat
set numb to 1
repeat
	set i to item numb of founditems
	set {size:thesize, kind:thekind, displayed name:thename, creation date:creation, visible:visibility} to (info for POSIX file i)
	set dialog to "Name: \"" & thename & "\"" & return & return & "Kind: " & thekind & return & return & "Size: " & thesize & " bytes" & return & return & "Creation Date: " & (creation as text) & return & return & "Visibility: " & (visibility as text) & return & return & "Path: " & i & return
	set button to (choose from list {"Open", "Back", "Next", "End"} with prompt dialog with title "Your search for \"" & spotlightquery & "\"") as text
	if button is "false" then return
	if button is "Open" then
		try
			set theapp to default application of (info for POSIX file i)
			tell application theapp to open (POSIX file i as string)
			activate application theapp
		on error e
			display dialog "An error has occured trying to open your file:" & return & return & e buttons {"OK"} default button 1
		end try
		exit repeat
	else if button is "Back" then
		if numb is not 1 then
			set numb to numb - 1
		end if
	else if button is "Next" then
		if numb is not (count founditems) then
			set numb to numb + 1
		end if
	else if button is "End" then
		exit repeat
	end if
end repeat
if founditems is {} or founditems is {""} then
	display dialog "No search results!" buttons {"OK"} default button 1 with title "Your search for \"" & spotlightquery & "\""
end if

Oh… forgot I could have saved space! :smiley:

not just space, it saves also time. because the info for function is called only once instead of 5 times

Those links are broke. To fix, remove the “bbs.” from the URL.

For example:
http://macscripter.net/viewtopic.php?id=24765