Dynamic Gmail Searches
Dynamic Gmail Searches

Lots of people use their email inbox as a running to-do list, it’s the first place they check at the beginning of their day. I do the same thing, but the problem is that literally everything is in your inbox, you need something to filter down the emails depending on what you’re trying to do at the moment. A helpful way to process your emails is just to look at today—just the emails you’ve received or sent today. You can do this using gmail’s search operators and entering todays date into the search:

after:2019/12/21

The way my mind works I’m aware of what month it is but on most days I wouldn’t be able to tell you what date it is without looking at a calendar, so every time I do a search like this I look at todays date in my calendar and then type it in.

I recently learned a way that you can automate this by using Bookmarklets. Bookmarklets let you make a bookmark in your browser that, when clicked, will always bring you to your gmail inbox showing only the emails from today.

This is the javascript that you plug in to the url field of the bookmark:
javascript:(function(){ var d=new Date(); window.open(‘https://mail.google.com/mail/u/0/#search/after:’+d.getFullYear()+’-‘+(d.getMonth()+1)+’-‘+d.getDate()); })();

More Explanation

A bookmarklet is like a regular bookmark but it also has javascript that gets executed before bringing you to the url in question. So in this case it’s like a bookmark leading you to your gmail inbox that first uses javascript to figure out the current date and then plugs that date in to your gmail search bar.

Everywhere you go within gmail has it’s own unique url. Your inbox is https://mail.google.com/mail/u/0/#inbox. Your sent mail is https://mail.google.com/mail/u/0/#sent.

Similarly, if you type in a search to search within your emails the page that gets returned showing you all the search results also has its own unique url. If you search for ‘apples’ the url will be https://mail.google.com/mail/u/0/#search/apples

Searching for todays date will have this url: https://mail.google.com/mail/u/0/#search/after%3A19%2F12%2F21.

Looking closer at the url
https://mail.google.com/mail/u/0/#search/after%3A19%2F12%2F21
Is the portion that tells the browser to bring you to your inbox.

https://mail.google.com/mail/u/0/#search/after%3A19%2F12%2F21
Is the portion that says to return the results of a search within your inbox.

https://mail.google.com/mail/u/0/#search/after%3A19%2F12%2F21
And this is the portion that says what you’re searching for.
If you’re wondering why ‘after:19/12/21’ is displayed as ‘after%3A19%2F12%2F21’ it’s because the colon and slash characters are reserved for other things when they’re in a url so something called Percent Encoding (https://en.wikipedia.org/wiki/Percent-encoding) is used to translate them into neutral characters.

So back to the bookmarklet, first you make a bookmark in your browser, and then you edit the url that the bookmark leads to, pasting in the code below. I don’t know javascript but I found this code and then just edited what was being searched for.

javascript:(function(){ var d=new Date(); window.open(‘https://mail.google.com/mail/u/0/#search/after:’+d.getFullYear()+’-‘+(d.getMonth()+1)+’-‘+d.getDate()); })();

Next

I’d like to figure out how to use javascript to do more dynamic searches that are relative to the current date, like Yesterday or Last Week. Gmail already lets you search for messages older or newer than a time period.

Example: newer_than:7d

So technically this can be used to search for emails within the week but I find that this falls short and more lets you search in the way that computers think instead of the way people think. For instance, if I were to ask “What did you do last week?”, you would probably think of what you did at work last week and what you did over the weekend. You would think of the last calendar week. You aren’t thinking of the last 7 day period that has most recently elapsed.

So on a Wednesday if I want to do a search for the emails I’ve sent so far this week I don’t want those results to include email from last weekend and the end of last week—which is what I would get if I used ‘newer_than:7d’ as my search criteria.

Links

Leave a Reply

Your email address will not be published. Required fields are marked *