Alfred Custom Searches
October 8, 2014



alfred-logo

Two weeks ago I wrote an article about Alfred, the productivity app that can replace your Spotlight while doing so much more. One of the extra features I discussed in that “review” was the Custom Search function. With this function, you are able to search any online site through the app. I went over this a little bit in the previous article, but I wanted to do a more in depth explanation so you can easily create your own custom searches.

Custom searches can be an extremely powerful tool for sites you search a lot. I highly recommend them for sites you frequent, especially for programming. Another great thing about the custom searches is that they are available to free users as well asĀ PowerPack users, so even if you only have the free version of Alfred – you can still use them.

Being a WordPress Developer, I search the WordPress Function Reference on the codex as well as the PHP Online Manual a lot. I made both of these searches into custom search functions in Alfred, and I really think I get things done a lot faster when using them. I did end up finding a workflow (this is a PowerPack feature only) that searches the codex and returns suggested results, but the custom search did work when I was using it. By the end of this article, you should know how to create custom searches like these and

Let’s start by looking at how to create a custom search function. First thing first, install Alfred. Once you have Alfred installed, open Alfred’s preferences – if you can do this by opening the normal Alfred search and then typing in “Alfred Preferences” and hitting enter when it pops up.

Show-Alfred-Preferences

This will open up the Alfred Preference pane. In order to modify or create your custom searches, navigate to the “Features” tab at the top and then to “Web Search” on the left hand side. Once here you will see a listing of all the search functions you have right now. To add a new search, simply click the “Add Custom Search” button in the bottom right hand corner. To edit an existing search, double click the search.

Alfred-Web-Search

PHP Manual Custom Search

Let’s first take a look at my PHP search to see how we can create a search. If I double click on the search I will get the following:

Alfred-PHP-Manual-Search

Here you can see that the custom search is made up of a search url (more on how to find this in a second), the title, the keyword, and the validation. You can also add an icon for the search which makes it look a lot more professional. Let’s first take a look at how we find a search.

When creating a new custom search, the first thing I do is go to the website and search something. For the PHP Manual, I simply went to the manual URL (http://ca3.php.net/manual/en/) and entered in a search of “get the date”. This produces a URL that looks like “http://ca3.php.net/manual-lookup.php?pattern=get+the+date&scope=quickref“. Using this URL I can create my custom search.

The first thing you’ll notice about the URL is that my search is located after the pattern= segment. This is a GET variable, but that really isn’t important. All you need to do is locate where your search is in the URL. Note that the search ends right before &scope=. This means that we want to replace that segment (where our search is located) with {query} in Alfred.

What this will do is insert the query we type in Alfred into the URL and then open up the web browser with that page. You may have also noticed that our search has “+” between the words. All you need to do to identify this is check the box that says “Encode Spaces as +”. The end result for the Search URL looks like http://ca3.php.net/manual-lookup.php?pattern={query}&scope=quickref.

The title can be anything you want, but make it descriptive as it will show up when you type your keyword. The keyword is the phrase you will type in Alfred that will activate this custom search. In my case, when I type PHP it triggers the search. I then follow up the keyword with the query to do the full search.

Alfred-PHP-Get-Time-Search

The icon is optional but it looks a lot more professional if you have it in there. Just Google “PHP Logo” and you should be able to find one that will work (use a .png for transparency for the best results). The last thing to go over is the validation. This is a test string to see if the custom search is working properly. It is a good idea to use something with a space in it to make sure they will work. You can then click test to see if the custom search works. This way you don’t have to close preferences and test it out then reopen preferences and modify something.

One of the really nice things about the PHP Manual is that searches will automatically redirect to the correct page if you search something that exists. For example, if I search “time” then it redirects me directly to the time manual page since it exists. If it doesn’t exist then it takes you to the search page so you can see what there is to choose from.

WordPress Function Reference Custom Search

The next custom search we are going to look at is the WordPress Function Reference. The first thing we want to do is head to http://codex.wordpress.org/Function_Reference and search something so we can figure out our URL. However, when you get to that page you may notice something funny. There isn’t actually a way to search the function reference. Instead you can just click on the functions on that page to get around.

It may already seem like we can’t use a custom search on this page, but that would be a bit too quick of a judgement. Let’s click on one of the functions, lets say get_adjacent_post. This will bring us to the URL “http://codex.wordpress.org/Function_Reference/get_adjacent_post“. As you can see, the URL contains the function name – therefore we can use the function name as our query. Our search URL will end up looking like “http://codex.wordpress.org/Function_Reference/{query}“. Now when you search for a function, it will bring up its page if it exists.

This brings us to the first tricky thing about this custom search. If the function does not exist, then it will not give a default search page like our PHP search did. Rather it will just give us a 404 page saying the function doesn’t exist. This is unfortunate but there is no way around it (that I’ve found). Still, if you know your functions or see one in the code of a website you want to look up, then this tool works super fast. And if you happen to mess it up the webpage is open and you can do a simple Google search from there.

Conclusion

I hope reading this has given you the knowledge you need to setup a custom search of your own on a site you frequent. The basic steps are to search on the website you want to use, copy the url of the search, replace your search with {query}. You can then fill out the title, keyword and icon to your liking.

If you have any good examples, I would love to hear about them in the comments section below (I might even start using them too). As always thank you for reading and please share it around as much as you can!


Comments