• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: mdfind – How to Find a Specific Word?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: mdfind – How to Find a Specific Word?


  • Subject: Re: mdfind – How to Find a Specific Word?
  • From: Shane Stanley <email@hidden>
  • Date: Wed, 15 Mar 2017 15:57:03 +1100

On 15 Mar 2017, at 7:38 am, Christopher Stone <email@hidden> wrote:

Is there a way to force Spotlight to find what I want?

You can use a compound predicate:

mdfind -onlyin . '(kMDItemFSName == "find"c) || (kMDItemFSName == "* find *"c) || (kMDItemFSName == "find *"c) || (kMDItemFSName == "* find"c)'

Or in AppleScript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set thePath to POSIX path of (path to desktop) -- whatever
my searchPath:thePath searchPredicate:"(kMDItemFSName == [c]%@) OR (kMDItemFSName LIKE [c]%@) OR (kMDItemFSName LIKE [c]%@) OR (kMDItemFSName LIKE [c]%@)" predicateArgs:{"find", "* find *", "find *", "* find"}

on searchPath:thePath searchPredicate:predString predicateArgs:argList
set thePred to current application's NSPredicate's predicateWithFormat:predString argumentArray:argList
set targetURL to current application's |NSURL|'s fileURLWithPath:thePath
set theQuery to current application's NSMetadataQuery's new()
theQuery's setPredicate:thePred
theQuery's setSearchScopes:{targetURL}
theQuery's startQuery()
repeat while theQuery's isGathering() as boolean
delay 0.01
end repeat
theQuery's stopQuery()
set theCount to theQuery's resultCount()
set theResults to current application's NSMutableArray's array()
repeat with i from 1 to theCount
set aResult to (theQuery's resultAtIndex:(i - 1))
set thePath to (aResult's valueForAttribute:(current application's NSMetadataItemPathKey))
(theResults's addObject:thePath)
end repeat
return (theResults's sortedArrayUsingSelector:"compare:") as list
end searchPath:searchPredicate:predicateArgs:

Spotlight doesn't support regular _expression_ searches.

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: mdfind – How to Find a Specific Word?
      • From: 2551phil <email@hidden>
References: 
 >mdfind – How to Find a Specific Word? (From: Christopher Stone <email@hidden>)

  • Prev by Date: Re: mdfind ­ How to Find a Specific Word?
  • Next by Date: Re: mdfind – How to Find a Specific Word?
  • Previous by thread: Re: mdfind ­ How to Find a Specific Word?
  • Next by thread: Re: mdfind – How to Find a Specific Word?
  • Index(es):
    • Date
    • Thread