Search Evernote from Alfred

IMPORTANT: Please see additional updates at the end of this post.

Evernote is my ubiquitous deposit spot for the overflow from my brain and everyday (unlike my brain) it grows in size. Being able to access the information held in Evernote in as easy a manner as possible is a key part of the way I work and I recently wrote a little script to make it that little bit better. The script allows me to search Evernote from Alfred (a keyboard driven app launcher on my Mac) which is always just a hot key away.

So first of all why did I bother writing this script? Ignoring the “because it’s fun” element to a lot of the things I do, there are two reasons.

The first is that whilst I spend a lot of time in Evernote, I don’t always have it open and even when I do, I frequently don’t want to navigate away from what I already have open in order to search for something else.

The second relates to the inbuilt Mac search facility - Spotlight. Whilst this can include searching for notes within Evernote, it also includes stuff not in Evernote (and if I want to search Evernote I’m usually pretty sure that what I want is in there). Since Spotlight doesn’t support the more advanced search syntax (e.g. “tag:”, “-notebook:”) this also makes it less useful to me.

Alfred is constantly running on my Mac and at the press of a keyboard shortcut it presents me with a window I can type something into and this can then do things such as open an application, initiate a web search or even run a script. So it made sense to me that I could get Alfred to take a command with an Evernote search string as the parameter and pass that to an AppleScript to search Evernote for me and display the results in a new Evernote window.

So here’s how it’s done…

Step 1 - Create the Search Evernote AppleScript

So the first step is to create the script that Alfred actually calls. It’s really quite a simple script thanks to the Evernote AppleScript integration that does the hard stuff. So open the AppleScript editor and copy the script below into it. You can then save it as something like “search evernote.scpt” in your script folder. I actually have a number of Evernote related AppleScripts so I have a dedicated Evernote folder within my scripts folder (as you’ll see below).

on run lstParameters
    --Get the parameters passed to the script - this is the search query
    set strSearchCriteria to SpaceList(lstParameters)
    
    --Open a new Evernote window populated by the search query
    tell application "Evernote"
        open collection window with query string strSearchCriteria
    end tell
end run

--Take a list of text items and return them as a string with a space between each item
on SpaceList(astrItems)
    --Store what the current list delimiter is
    set tmpDelimiters to AppleScript's text item delimiters
    
    --Set the list delimiter to a space and build the string we want to pass back
    set AppleScript's text item delimiters to " "
    set strReturn to astrItems as string
    
    --Set the list delimiter back to what it was previously
    set AppleScript's text item delimiters to tmpDelimiters
    
    --Return the string we built
    return strReturn
end SpaceList

So what does the script do? When you run the script it takes all of the parameters that you pass to it and then tells Evernote to open a new window using that list of parameters as your search query.

There’s a bit of tweaking to put the parameters back into the format you typed them in. By default a space in your search string would indicate an additional parameter to the script, so we have to take all of the parameters and put a space back between each one to reconstitute the original search term. It saves having to put your search query in double quotes each time when you type it into Alfred.

Step 2 - Bash your script about a bit for Alfred

By default AppleScript will probably want to put your script into the “Library/Scripts” folder in your personal (home) folder. You may have decided to pop it into Dropbox or some other cloud storage sync’d folder so you can keep this script available on all your Macs. It’s all good, just make sure you know exactly where the script is and what it is called.

For example, my script can be defined in terms of location by "/Users/stephen/Library/Scripts/Evernote/search evernote.scpt". Please pay careful attention to alphabetic case.

We now need to create a shell script to call the AppleScript. Fortunately this is really simple to do. Simply add the path to the script after osascript. For my script this would look like this:

osascript "/Users/stephen/Library/Scripts/Evernote/search evernote.scpt"

So the last thing to do is to pass some search parameters. If we were actually running this from the terminal app, we would just type them after the script location path. However because we’re going to use Alfred we need to pop a special placeholder in there instead - the word query in curly braces. So again for me this looks as follows:

osascript "/Users/stephen/Library/Scripts/Evernote/search evernote.scpt" {query}

Step 3 - Configure Alfred

So now we have a shell command for Alfred to carry out we need to configure Alfred to know when to carry out this command. Make sure Alfred is running and use your shortcut key combination to bring it into the foreground to enter your command (I use ALT+Space). Next click the settings icon in the top right corner of the Alfred window. It looks like a little cog. This will bring you into the Alfred settings window. Alternately if you have the Alfred icon in your menu bar, you can get to the settings window from that.

Next select the “Features” panel and look down on the left hand side under “SYSTEM” for an option called “Terminal / Shell”. Selecting this will display Alfred’s preferred terminal application as well as a prefix for executing terminal commands from within Alfred, and a set of shortcuts. In our case we need to configure the shortcuts.

So click on the “+” button beneath the shortcuts list to bring up a new terminal shortcut window. In the “Title” field type a descriptive name for your script (e.g. “Evernote Search”) and a suitable description (e.g. “Quick search within Evernote”) in the “Description” field.

The “Keyword” field should be set to whatever you want to use to call up the Evernote search. For example you might like to use something like $EN?

The “Command” field is where the command set out in step 2 should be entered. Along with this also make sure that the checkbox for “quotes” is also checked (leave the one for “spaces” unchecked).

You can also set an icon for your custom terminal command. You can just drag and drop an image or icon file onto the icon drop area in the top right of the window. If you don’t have one to mind I’d recommend a green striped magnifying glass from Icon Archive.

Once you have all the details configured, click on “Save”. Your custom terminal shortcut will then appear in the list. The entry also has two checkboxes. Tick the “Silent” checkbox. This will mean that when Alfred runs the command it will run it silently without opening a Terminal app window.

Step 4 - Try it out

Everything should be set to give it a go. As you type in your special keyword (e.g. “$EN” into Alfred you simply follow this by the search query you want to perform. There are a couple of examples below, but you can get lots more information from the Evernote search documentation.

Search all notebooks for notes containing the words “apple” and “script”.

  • $en apple script

Search notebook “Evernote Stuff” for notes containing the word “AppleScript” and tagged as a “script”

  • $en notebook:"Evernote Stuff" tag:script AppleScript

Troubleshooting

One issue I had when creating this set-up was if I used “~” in the location of my home folder (step 2), the script ceased to function when the “Silent” checkbox was ticked (in Alfred). Now this may be something peculiar to my Mac configuration or it may be a quirk in Alfred. In any case explicitly setting the path meant it works with or without the “silent” checkbox ticked.

Additional Updates

  1. The latest version of Alfred has an easier way to add this functionality by just importing the package I created - see my Search Evernote from Alfred (Again…) post.
  2. Alfred 2.0 was released into Beta in January 2013. The search is now included as part of the Evernote Search+ workflow for Alfred 2.0.
Author: Stephen Millard
Tags: | alfred | evernote | mac |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read