Special Spotlight Searches in LaunchBar

Sharing and discussing custom actions for LaunchBar
ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Special Spotlight Searches in LaunchBar

Post by ptujec » Wed Dec 09, 2009 11:09 am

*** This is exspecially helpful in combination with some tagging tool like this script or Tagger (hasseg.org/tagger) ***

With the help of iRounak and a friend I was able to put together the following scripts, with which you can do special Spotlight Searches inside LaunchBar. (Go here, here or here for background information.)

The first script is looking for a query/queries in names, spotlight comments and OpenMeta tags inside the userfolder (~). The second is looking for a query/queries in names, spotlight comments and keywords of images inside the userfolder (~).

1) Search files

Code: Select all

-- http://forums.obdev.at/viewtopic.php?p=12250#p12250
-- by iRounak
-- additional coding by @ptujec with big help of :kelko:

on handle_string(thequery)
   try
      -- you can comment (with "--" like in this note) the next line if you are using LaunchBar in non-activate mode
      tell application "LaunchBar" to hide
      
      set displayQueryString to ""
      set commentQueryString to ""
      set tagQueryString to ""
      
      set firstLoop to true
      repeat with aWord in every word in thequery
         
         if not firstLoop then
            set displayQueryString to displayQueryString & " && "
            set commentQueryString to commentQueryString & " && "
            set tagQueryString to tagQueryString & " && "
            
         else
            set firstLoop to false
         end if
         
         set displayQueryString to displayQueryString & " kMDItemDisplayName == " & quote & aWord & "*" & quote & "wc "
         set commentQueryString to commentQueryString & "kMDItemFinderComment == " & quote & aWord & "*" & quote & "wc "
         set tagQueryString to tagQueryString & "kMDItemOMUserTags  == " & quote & aWord & "*" & quote & "wc "
         
      end repeat
      
      set endQueryString to "'(" & displayQueryString & ") || ( " & commentQueryString & " ) || ( " & tagQueryString & " )'"
      set thecmd to "mdfind -onlyin ~  " & endQueryString
      
      set theresult 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
         tell application "LaunchBar"
            display in large type "No items found!" with sound "Submarine"
            delay 0.7
            hide
         end tell
      else
         tell application "LaunchBar"
            set selection to thelist
            activate
         end tell
      end if
      
   on error e
      tell application "LaunchBar"
         display in large type e with sound "Submarine"
         delay 1.5
         hide
      end tell
   end try
   
end handle_string


2) Search images
(You need to adapt this if you are using a non-English UI. See comments in script for details.)

Code: Select all

-- Source: http://forums.obdev.at/viewtopic.php?p=12250
-- by iRounak
-- additional coding by @ptujec with big help of :kelko:

on handle_string(thequery)
   try
      -- you can comment (with "--" like in this note) the next line if you are using LaunchBar in non-activate mode
      tell application "LaunchBar" to hide
      
      set nameQueryString to ""
      set commentQueryString to ""
      set keywordQueryString to ""
      
      set firstLoop to true
      repeat with aWord in every word in thequery
         
         if not firstLoop then
            set nameQueryString to nameQueryString & " && "
            set commentQueryString to commentQueryString & " && "
            set keywordQueryString to keywordQueryString & " && "
            
         else
            set firstLoop to false
         end if
         
         ------------------------------------------
         -- You need to replace the word "Image" in the line "(kMDItemKind == \"*Image*\")" if you are using a none-English Userinterface
         -- To see what word you need to use enter "mdls filename.filextention" in the Termnal and look for "kMDItemKind". For a jpg on an german UI you will find the description "JPEG-Bild", which means you need to replace *Image* with *Bild*
         ------------------------------------------
         
         set nameQueryString to nameQueryString & " kMDItemDisplayName == " & quote & aWord & "*" & quote & "wc " & " && (kMDItemKind == \"*Image*\")"
         
         set commentQueryString to commentQueryString & "kMDItemFinderComment == " & quote & aWord & "*" & quote & "wc " & " && (kMDItemKind == \"*Image*\")"
         
         set keywordQueryString to keywordQueryString & "kMDItemKeywords == " & quote & aWord & "*" & quote & "wc "
         
         
      end repeat
      
      set endQueryString to "'(" & nameQueryString & ") || ( " & commentQueryString & " ) || ( " & keywordQueryString & " )'"
      set thecmd to "mdfind -onlyin ~  " & endQueryString
      
      set theresult 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
         tell application "LaunchBar"
            display in large type "No items found!" with sound "Submarine"
            delay 0.7
            hide
         end tell
      else
         tell application "LaunchBar"
            set selection to thelist
            activate
         end tell
      end if
   on error e
      tell me to activate
      display dialog e
   end try
end handle_string
Last edited by ptujec on Mon Apr 26, 2010 9:31 pm, edited 5 times in total.

iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by iRounak » Wed Dec 09, 2009 1:53 pm

Hi,
Firstly, I had to remove
with sound "Submarine"
to compile the script and save it. I am on Leopard.
Secondly, I had to uncomment the line
-- tell application "LaunchBar" to hide
to make the script work. If you bring the script in LaunchBar, press space, then enter the search query and press return it is necessary to hide LaunchBar so that it comes back with the results. If you enter the search query in text mode and press tab and run the script, it is not necessary to hide LaunchBar.
Thirdly, this problem seems to have been solved:
The only thing which is still buggin me is that it only seems to search for the exact query. So it will find a file named "The test file" with the query "test file" but not with the query "file test".

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by ptujec » Wed Dec 09, 2009 2:22 pm

iRounak wrote:… Secondly, I had to uncomment the line
-- tell application "LaunchBar" to hide
to make the script work.

I commented the line because I use LaunchBar in nonactivating mode. I changed that in the script since activating mode is the default mode and put a note above that line instead.

iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by iRounak » Wed Dec 09, 2009 6:16 pm

Thats strange. :) Whether I use activating mode, or non-activating mode, makes no difference, I have to hide LaunchBar. Infact, I never understood what "non-activating mode" meant. Now, I have a faint idea.

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by ptujec » Wed Dec 09, 2009 6:33 pm

iRounak wrote:Infact, I never understood what "non-activating mode" meant. Now, I have a faint idea.

Its no big deal it just doesn't activate LaunchBar … which has its benefits sometimes. Thats it.

ymo
Posts: 3
Joined: Sat Nov 21, 2009 3:57 pm

Re: Special Spotlight Searches in LaunchBar

Post by ymo » Sat Jul 10, 2010 5:26 am

Thanks for the script.
How can I target an other volume than my Home directory?
I have swapped my optical drive to a second HD where all my working files are archived.

I tried to change:

Code: Select all

set thecmd to "mdfind -onlyin ~  " & endQueryString
but I'm not proficient enough with Applescript and it didn't work!

Cheers,

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by ptujec » Sat Jul 10, 2010 11:31 am

ymo wrote:Thanks for the script.
How can I target an other volume than my Home directory?
I have swapped my optical drive to a second HD where all my working files are archived.

I tried to change:

Code: Select all

set thecmd to "mdfind -onlyin ~  " & endQueryString
but I'm not proficient enough with Applescript and it didn't work!

Cheers,


Hhm. Did you try this?

Code: Select all

set thecmd to "mdfind  " & endQueryString


… or relpace just "~" with the directory of your second HD "/Your/Directory"

Unfortunately I can't figure out a way to restrict search to two specific directories. Its either one or just everthing I guess.

sjk
Wizard
Wizard
Posts: 315
Joined: Thu Sep 13, 2007 9:46 pm
Location: Eugene
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by sjk » Fri Apr 12, 2013 12:41 am

ptujec wrote:Unfortunately I can't figure out a way to restrict search to two specific directories.

At least on 10.8.3, mdfind accepts multiple "-onlyin" options, i.e.:

mdfind -onlyin ~/Desktop -onlyin ~/Documents query

iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by iRounak » Sun Jun 22, 2014 1:20 pm

Can someone write an lbaction for these spotlight searches to get suggestions?

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by ptujec » Sun Jun 22, 2014 2:08 pm

iRounak wrote:Can someone write an lbaction for these spotlight searches to get suggestions?


So you want live suggestions for them as with the new Google, Wikipedia … actions? Interesting idea. I guess that would require some more developer skills.

iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by iRounak » Sun Jun 22, 2014 3:26 pm

yes. I couldn't find a single sample action with applescript. They are all either javasscript or ruby or shell. And Actions Documentation is all Arabic to me.

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by ptujec » Sun Jun 22, 2014 3:40 pm

Its not hard to package an applescript in an action. I have a couple of those in here … e.g. the iTunes Fade in/out. The suggestion part is the hard part.

iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by iRounak » Sun Jun 22, 2014 9:13 pm

Turns out it is not very difficult after all. I don't have Xcode installed. So I just edited one of my existing lbactions which had live search.
I left the default script empty and put this in the suggestions script.

Code: Select all

on handle_string(thequery)
   set thecmd to "mdfind  'kMDItemDisplayName == " & quote & thequery & "*" & quote & "wc" & " && (kMDItemKind == \"Folder\")'"
   set theresult to do shell script thecmd
end handle_string

This one searches for folders.


I still need some guidance though.

If i type too fast i get the error,
Only one AppleScript can be executed in background at a time and there’s already another one executing.

How to avoid this error?

Also I get filepaths as results. Here's the screenshot:
http://postimg.org/image/x7l2c8joz/
What code should i add so that i can open the results by hitting return once i select one of the results?

iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by iRounak » Mon Jun 23, 2014 6:33 pm

What code should i add so that i can open the results by hitting return once i select one of the results?

First I used

Code: Select all

<key>LBResultType</key>
         <string>path</string>

This got me proper icons, file names (and below them file paths) for the search results
http://postimg.org/image/ix74qv2pp/

However, I couldn't figure out how to open any of the action results.

So I removed the above mentioned code. This gave me paths as action results. Then I put this code in default.scpt

Code: Select all

on handle_string(_string)
   tell application "Finder" to open _string as POSIX file
end handle_string

Any ideas as to how I can use <key>LBResultType</key> (so as to get proper icons, file names and paths as subtitles etc) and then be able to open the action results?


As regards the error:
Only one AppleScript can be executed in background at a time and there’s already another one executing.

I have now used

Code: Select all

<key>LBBackgroundKillEnabled</key>
<true/>

and try statements in my suggestions.scpt
However, I still get the error though less frequently then before. Any help please?

Jono
Rank 4
Rank 4
Posts: 161
Joined: Mon Feb 25, 2008 11:25 am
Location: UK
Contact:

Re: Special Spotlight Searches in LaunchBar

Post by Jono » Tue Jun 24, 2014 11:01 am

I can't offer any help. But this looks really useful, something I'd use a lot.

Hopefully someone else will be able to help.

Post Reply