Page 1 of 1

Help with some JavaScript for an action

Posted: Sun Mar 20, 2016 10:46 pm
by Jono
I don't really know JavaScript and thought it would be a fun little exercise to try and make a LaunchBar action using it (learning some JavaScript along the way).

The code I have at the moment searches Spotlight for PDFs and shows the results. If it doesn't find any results with the search term it shows some text saying 'No results found etc.' It also shows another item below (with placeholder text) that I'd like to re-run the action allowing you to enter a new search term. Is there some way to make it possible to re-run the search?


Also, in LaunchBar's Action Editor in it's Script Settings I have it set to Return Results as 'Paths'. With this setting it correctly shows the PDF search results, but it there aren't any matching results the messages that should display the specified icons don't show th I icons. I also found that if I added file paths, URLs etc. (just to test things) they wouldn't open in Finder, Safari etc.

If I switch the Script Settings to Return Results as 'Items' is correctly shows the specified icons, opens file paths, URLs etc. But then shows the PDF search results as paths (of course), which is not what's wanted.

Any help in getting this behaving how I'd like would be apreciated.


Here's the code:

Code: Select all

// Search for PDFs
function runWithString(string) {
     
        var result = LaunchBar.execute('/usr/bin/mdfind','kind:pdf','name:' + string);
        result = result.split('\n') // New line for each item
       
    if (result == 0) {
        return [
                    { 'title' : 'No results found for \'' + string + '\'',
                    'icon' : 'at.obdev.LaunchBar' },
                   
                    { 'title' : 'Modify your search?',
                    'icon' : 'Spotlight',
                    'url' : 'http://www.obdev.at/' } // Just for testing if file paths, URLs etc. successfully open/work
                ];

    } else {
       
        if (result.length > 20) {
            return result.slice(0,20); // Just return the first 20
    } else {
        return result.slice(0,-1);
        }
    }
   
}


And here's the action if anyone wants to have try http://d.pr/f/12MmN


I realise that this could be done with AppleScript (and I already have some AppleScripts that do this). I just wanted to have a go at it in JavaScript and replicate them.
It would also be useful making new/different actions in the future.

Re: Help with some JavaScript for an action

Posted: Mon Mar 21, 2016 10:47 am
by veke71
This is a great idea! I would like it to search for metadata in PDFs, not just file names. Is that easily done?

Re: Help with some JavaScript for an action

Posted: Mon Mar 21, 2016 10:54 am
by Jono
veke71 wrote:This is a great idea! I would like it to search for metadata in PDFs, not just file names. Is that easily done?

Sure, just change the line from:

Code: Select all

var result = LaunchBar.execute('/usr/bin/mdfind','kind:pdf','name:' + string);


to

Code: Select all

var result = LaunchBar.execute('/usr/bin/mdfind','kind:pdf' + string);

Re: Help with some JavaScript for an action

Posted: Mon Mar 21, 2016 11:12 am
by veke71
yes, I actually managed to that myself! [proud]

Re: Help with some JavaScript for an action

Posted: Mon Mar 21, 2016 12:10 pm
by Jono
Excellent 8)