Help searching Spotlight with AppleScript

I have some scripts that I use with LaunchBar to search with Spotlight for specific file types and return the results back in LaunchBar.

Here’s an example that searches for Photoshop, Illustrator and Sketch documents:


on handle_string(thequery)
	
	try
		set tid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to " "
		set theCount to count of text items in thequery
		set theList to text items of thequery
		set theList2 to {}
		repeat with i from 1 to theCount
			set end of theList2 to (text item i of theList & "*")
		end repeat
		set thequery to theList2 as text
		set AppleScript's text item delimiters to tid
		
		set thecmd to "mdfind -onlyin ~/ 'kMDItemDisplayName == " & quote & thequery & "*" & quote & "wc" & " && (kMDItemContentType == \"com.adobe.photoshop-image\"||kMDItemContentType == \"com.adobe.illustrator.ai-image\"||kMDItemContentType == \"com.bohemiancoding.sketch.drawing\")'"
		
		set Stheresult to do shell script thecmd
		set theList to {}
		set allparas to every paragraph in theresult
		repeat with apara in allparas
			set end of theList to (POSIX file apara) as alias
		end repeat
		if theList is {} then
			set theList to "No items were found for " & quoted form of thequery
			tell application "LaunchBar"
				remain active
				set selection to theList
			end tell
			exit repeat
		end if
		
		tell application "LaunchBar"
			remain active
			set selection to theList
			if (count of theList) is not 1 then
				tell application "System Events"
					tell process "LaunchBar"
						keystroke space
					end tell
				end tell
			end if
		end tell
	on error e
		tell me to activate
		display dialog e
	end try
end handle_string

I just change the ‘set thecmd to.’ line for different scripts to search for different things.
Here’s another example of one I use:


		set thecmd to "mdfind -onlyin ~/ 'kMDItemDisplayName == " & quote & thequery & "*" & quote & "wc" & " && (kMDItemContentType == \"com.apple.iwork.pages.pages\"||kMDItemContentType == \"public.rtf\"||kMDItemContentType == \"com.microsoft.word.doc\"||kMDItemContentType == \"org.openxmlformats.wordprocessingml.document\")'"

The script searches for those specific file types and and for the text I enter in LaunchBar it uses that to search for the file name.

How would I search for the specified document types, but as well as searching for the file name also search the tags, Spotlight comments, text content etc? At the moment they only search for the file names.

I got the original script on the LaunchBar forum and have asked there, but haven’t received a reply. Hoping someone here would be able to help :slight_smile:

.nevermind, I’ve managed to figure this out.

Hey Jono,

Please post your solution. :slight_smile: