Search
« PDF Quick Save in Word 2007 | Main | How to rename a service on Mac OS X »
Sunday
Jun122011

Search Evernote from Alfred

PLEASE NOTE: 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
 
PLEASE ALSO NOTE: 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.

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


So 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.


PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (11)

Stephen,

Let me start by saying a big THANK YOU! I have recently been looking at ways to improve my workflow and finally drank the Evernote koolaid. I also recently discovered the wonders of Alfred and was wondering if there was a way to search evernote. I followed your steps even as a someone that has never used Apple Script and I was able to get this working my first shot.

You have saved my untold hours and improved my productivity and I thank you for sharing your script!

I also see your love of martial arts. I have studied many different styles on my path to Shaolin Kung Fu. Enjoyed your martial arts posts. I'm following you on Twitter now and look forward to connecting! It seems we have a lot in common.

All the best,
Phil

June 21, 2011 | Unregistered CommenterPhil Montero

Stephen,

now that saved my day. I use Alfred as my swiss army knife to search, open apps and do lots of custom queries. I wondered how to integrate Alfred with Evernote, and your tutorial worked like a charm. Thanks a lot!

Axel.

June 30, 2011 | Unregistered CommenterAxel

thank you

August 2, 2011 | Unregistered Commenterdantetemp

This is the very first time I have ventured into Applescript land, and I am pretty clueless. A couple of problems:

1. When I first enter the script as above in the Applescript Editor (minus the line numbers...), I get the following error when I try to run or compile:

error "Can’t make «script» into type string." number -1700 from «script» to string

Then, it seems as the Alfred 0.9.9 version is somewhat different from what you show above. I'm not sure quite how to follow directions.

And where should the osascript stuff be put?

Thanks for your help. I'm looking forward to seeing this work, since I rely on both these programs.

RL

August 14, 2011 | Unregistered CommenterRichard Levenson

Richard.

I'll do my best to answer your queries, but please read through to the end of the comment before doing anything as there may be another less complicated way for you ....

It sounds maybe like you're trying to run the script from inside the AppleScript editor. It is only to be called from Alfred, not run or compiled from the editor. It is trying to do something with the text to search for ... but that has to be given to the script by Alfred. If you just run it, it has nothing to search for and so it just falls on its backside.

Step 3 explains where you need to put the command from step 2.

Okay, as you quite rightly noted, Alfred 0.9.9 looks a bit different to the Alfred described in this post. That's simply because Andrew over at AlfredApp.com has been a very busy coder and has made quite a few cosmetic as well as functional changes between the release that this was written for and the public release of 0.9.9. However, one of the things he did was improve the ease of adding these extensions.

Whilst this new improvement was in beta testing I wrote a little post about how to add this in using the new and improved method. You can download the package from the post to. So this should get around you needing to mess around with any sort of scripting.

Here's ...

· the link to the blog post, and
· the link to the package.

I've just had a quick look on the AlfredApp site and noticed that they've added a link to this article (which is very nice of them and I totally hadn't realised) rather than the second one. I'll ask them to re-link to the second blog post and also add a note to the top of this post.

I hope that helps, but if you have any issues, please let me know.

Stephen.

August 14, 2011 | Registered CommenterStephen Millard

Thanks for the great tip. However, I would like to run the search in the existing Evernote window instead of opening a new window. Could you show how to modify the code to achieve this? Thanks.

September 24, 2011 | Unregistered CommenterKalle Kantola

In case anyone else is interested, I discovered that you can also restrict the search to a specific stack by adding stack:name to the query. (I even added it to my default Alfred query string since I only ever want to search a specific stack, not my entire Evernote DB.)

January 16, 2012 | Unregistered CommenterPeter Couvares

To run the query in the existing Evernote window (rather than opening a new window), replace the following piece of code:

tell application "Evernote"
open collection window with query string strSearchCriteria
end tell

with this:

tell application "Evernote"
activate
set query string of window 1 to strSearchCriteria
end tell

February 1, 2012 | Unregistered CommenterKalle Kantola

Only discovered Alfred last night and thought Spotlight was pretty good. This is another gear. Just run your Search Evernote extension, works like a dream and wanted to thank you for your time & effort in putting it out there for people like me to use.

February 25, 2012 | Unregistered CommenterSeth East

@Kalle: this will only work if you already have a collection window open as the frontmost window. Not if a note window is open.

The most recent version of Alfred (incl powerpack) supports applescript without the terminal.

Both the terminal route and the applescript route share the same drawback: if you provide 2 search words, then they get both surrounded with quotes. This delivers different search results then without quotes. I have not found the solution to this.

March 2, 2012 | Unregistered CommenterRobert

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>