Site Navigation

Mar 2, 2014

Google Date Search from Mac Command Line

Google Logo

Something has been annoying me over the last few weeks. To limit a Google search to a week, month or year, I need to do several clicks repeatedly in my browser. Well when done repeatedly, this becomes a bit of a chore. So I decided to find out if there was a way to specify this in the Google Search box.

After some searching around, the short answer is no. You can not specify a week, month or year range in the search box. This can only be done by creating a custom URL. (Details can be found here.)

So I decided to write a command line script that allows me to type in a search string and a letter and then the search I want is executed in the browser.

Here is the Ruby script I wrote for a default browser (Safari in this case.)

ws.rb

   1 #!/usr/bin/ruby -w
   2 require 'CGI'
   3 
   4 if ARGV.length != 2
   5     puts "Usage: ws \"SearchString\" duration"
   6     puts "s - second n - minute h - hour d - day w - week"
   7     puts "w - week m - month y - year"
   8     exit
   9 end
  10 
  11 searchString = ARGV[0]
  12 duration = ARGV[1]
  13 durationSearch = "&tbs=qdr:"
  14 
  15 googPreamble = "https://www.google.com/search?q="
  16 finalString = "open \"" + googPreamble + CGI::escape(searchString) + 
  17   durationSearch + duration + "\""
  18 
  19 exec(finalString)
The usage is as follows.
Usage: ws "SearchString" duration
s - second n - minute h - hour d - day w - week
w - week m - month y - year

For example: ws "javascript html5" m

Typing this will launch a new window or tab in the default browser and perform a Google search for javascript and html5 during the past month.

The script should work on any Mac. I haven't tested it on Linux, but it should work with some minor modifications.

Here is a version of the script that will specifically launch Firefox. Enjoy!

wsff.rb

   1 #!/usr/bin/ruby -w
   2 require 'CGI'
   3 
   4 if ARGV.length != 2
   5     puts "Usage: ws \"SearchString\" duration"
   6     puts "s - second n - minute h - hour d - day w - week"
   7     puts "w - week m - month y - year"
   8     exit
   9 end
  10 
  11 searchString = ARGV[0]
  12 duration = ARGV[1]
  13 durationSearch = "&tbs=qdr:"
  14 
  15 googPreamble = "https://www.google.com/search?q="
  16 finalString = "open -a FireFox \"" + googPreamble + CGI::escape(searchString) + 
  17   durationSearch + duration + "\""
  18 
  19 exec(finalString)
Post a Comment

Favorite Links Feed