Find and show in finder using mdfind (possibly)

Hi I just need to know how to finish this script.

This allows me to scan a barcode and convert it to a sku it then looks it up via spotlight. all I need to do is display the results. How Can I finish this please?

property csvAlias : alias ((path to home folder as text) & "WorkFlow:john_sku_list.csv") --path to your csv-File as alias
property csvDelimiter : "," --delimiter used by your csv-File
set thekind to "PSD"

set EAN to ""
display dialog "Please Scan Barcode" default answer EAN
set EAN to text returned of result

tell application "System Events" to set {fName} to {(EAN)}
set csvText to read csvAlias
if csvText contains fName then
	set AppleScript's text item delimiters to fName & csvDelimiter
	set newListName to (text 1 thru -1 of (paragraph 1 of (text item 2 of csvText)))
end if
set spotlightqueryList to newListName

do shell script "mdfind -name " & spotlightqueryList & " " & thekind


Assuming everything else works:

set xxx to do shell script "mdfind -name " & spotlightqueryList & " " & thekind
display dialog xxx

That does reveal it in finder though?
Only where it is located?

as far as I am aware its a spotlight search?
so I was hoping to show its results in a finder window?

You can reveal the results separately like this:

set myFiles to every paragraph of (do shell script "mdfind -name " & spotlightqueryList & " " & thekind)
set revealList to {}
repeat with aFile in myFiles
	try
		set end of revealList to POSIX file aFile
	end try
end repeat

tell application "Finder" to reveal revealList

Quite excited I worked it out!!!

(It think)

property csvAlias : alias ((path to home folder as text) & "WorkFlow:john_sku_list.csv") --path to your csv-File as alias
property csvDelimiter : "," --delimiter used by your csv-File
set thekind to "PSD"
repeat
	activate
	set EAN to ""
	display dialog "Please Scan Barcode" default answer EAN
	set EAN to text returned of result
	
	tell application "System Events" to set {fName} to {(EAN)}
	set csvText to read csvAlias
	if csvText contains fName then
		set AppleScript's text item delimiters to fName & csvDelimiter
		set newListName to (text 1 thru -1 of (paragraph 1 of (text item 2 of csvText)))
	end if
	set spotlightqueryList to newListName
	
	set thelocation to (do shell script "mdfind -name " & spotlightqueryList & " " & thekind) as POSIX file
	log thelocation
	tell application "Finder"
		reveal thelocation
	end tell
	
end repeat


I don’t think this will work if the find command finds more than one file …

set thelocation to (do shell script "mdfind -name " & spotlightqueryList & " " & thekind) as POSIX file

Very True, so still open to suggestions (theoretically it should only find one)

Indeed

Result:
error “Finder got an error: AppleEvent handler failed.” number -10000

Did you try post #5?

No I missed that, that opens up lots of windows each for there results.

Also it picks up on the cache of adobe bridge. Need to get it to avoid that if possible

reveal {file “Images:2012-2013:WK16:Matalan:Matalan_WK16_PSD:MC65NW03648SV1OS.psd”, file “Hal 9000:Users:matthew:Desktop:Matalan_WK23_PSD:MC65NW03648SV1OS.psd”, file “Hal 9000:Users:matthew:Library:Caches:Adobe:Bridge CS5.1:Cache:256:Matalan_6AD02D5F:MC65NW03648SV1OS.PSD.jpg”, file “Hal 9000:Users:matthew:Library:Caches:Adobe:Bridge CS5.1:Cache:1024:Matalan_6AD02D5F:MC65NW03648SV1OS.PSD.jpg”}
→ {document file “MC65NW03648SV1OS.psd” of folder “Matalan_WK16_PSD” of folder “Matalan” of folder “WK16” of folder “2012-2013” of disk “Images”, document file “MC65NW03648SV1OS.psd” of folder “Matalan_WK23_PSD” of folder “Desktop” of folder “matthew” of folder “Users” of startup disk, document file “MC65NW03648SV1OS.PSD.jpg” of folder “Matalan_6AD02D5F” of folder “256” of folder “Cache” of folder “Bridge CS5.1” of folder “Adobe” of folder “Caches” of folder “Library” of folder “matthew” of folder “Users” of startup disk, document file “MC65NW03648SV1OS.PSD.jpg” of folder “Matalan_6AD02D5F” of folder “1024” of folder “Cache” of folder “Bridge CS5.1” of folder “Adobe” of folder “Caches” of folder “Library” of folder “matthew” of folder “Users” of startup disk}

Hi,

a workaround is either a AppleScriptObjC capable AS runner (like AppleScriptObjC Explorer) performing this code

current application's NSWorkspace's sharedWorkspace()'s showSearchResultsForQueryString_(spotlightqueryList & " " & thekind)

or a pure AppleScript solution GUI scripting the Finder

Instead of the mdfind line write


tell application "Finder"
	activate
	make new Finder window
end tell
tell application "System Events"
	tell process "Finder"
		keystroke "f" using command down
		repeat until exists group 6 of tool bar 1 of window 1
			delay 0.2
		end repeat
		set value of text field 1 of group 6 of tool bar 1 of window 1 to spotlightqueryList & " " & thekind
	end tell
end tell

depending on the custom toolbar settings the index of the group might differ

Something odd happening there goes on forever doing this

	--> false
exists group 6 of tool bar 1 of window 1 of process "Finder"
	--> false
property csvAlias : alias ((path to home folder as text) & "WorkFlow:john_sku_list.csv") --path to your csv-File as alias
property csvDelimiter : "," --delimiter used by your csv-File
set thekind to "PSD"
repeat
	activate
	set EAN to ""
	display dialog "Please Scan Barcode" default answer EAN
	set EAN to text returned of result
	
	tell application "System Events" to set {fName} to {(EAN)}
	set csvText to read csvAlias
	if csvText contains fName then
		set AppleScript's text item delimiters to fName & csvDelimiter
		set newListName to (text 1 thru -1 of (paragraph 1 of (text item 2 of csvText)))
	end if
	set spotlightqueryList to newListName
	
	tell application "Finder"
		activate
		make new Finder window
	end tell
	tell application "System Events"
		tell process "Finder"
			keystroke "f" using command down
			repeat until exists group 6 of tool bar 1 of window 1
				delay 0.2
			end repeat
			set value of text field 1 of group 6 of tool bar 1 of window 1 to spotlightqueryList & " " & thekind
		end tell
	end tell
	
	
end repeat


Try last group instead of group 6