Wednesday, October 03, 2018

Python script: MapIt.py


Based on an example, script of mapIt.py, from the video of Automate the Boring Stuff with Python, I figured out that the codes should be changed in Mac OS X, as I mentioned that there are multiple versions of python in Max OS in my previous blog.

In addition to those changes, there are some steps that should done in Mac to make the script as executable in terminal. I further make it working in Spotlight as well. Here are my notes.

Script

Based on the video, I made minor changes on the script. Here is what I have now:


  1. #! /usr/bin/env python3
  2. import webbrowser, sys, pyperclip
  3. sys.argv  # example ['mapit.py', '870', 'Valencia', 'St.']
  4. address = 'Home'
  5. if len(sys.argv) > 1 :
  6.    address = ' '.join(sys.argv[1:]) # ['870', 'Valencia', 'St.']
  7. else :
  8.    address = pyperclip.paste()
  9. # https://www.google.com/maps/place/<address>
  10. webbrowser.open('https://www.google.com/maps/place/' + address)

The first line is important if you want to run the script directly from terminal or Spotlight. Still there are some other steps to be done in order to run the script directly from terminal or Spotlight.

Notice that I specify the name of python is python3! Another minor change is the default address value: Home. I am using Mac and the Google map knows where my home is in my local Mac account.

I saved the script at my local user account path: ~/Programming/py/mapit.py With script ready, I test the script in terminal:

...$ python3 Programming/py/mapit.py Park Ave 83 St New York

As soon as I type in the above command, a new tab page of Google Map is opened in Safari with the correct address.



Make the Script Executable

So far so good. However, I would prefer to open the script directly from terminal, or from Spotlight, without specifying python3. For the case of terminal, first I have to make the script executable with the following commands:

...$chmod a+x ~/Programming/py/mapit.py

The second step is to add my script path to env PATH so that I can simply type in mapit.py anywhere in command line. This is done by adding the path in ~/.bash_profile:

  1. PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}:/Users/dchu/Programming/py"
  2. export PATH

With above setup ready, now I can run the script anywhere in terminal:

...$ mapit.py Donggaodi Hongxing St Beijing

Isn't that cool?




Make the Script Runnable in Spotlight


Spotlight on Mac is a very convenient way to launch an app, to open a file, and to find information. However, unlike Windows Run, it not the way to direcly launch an app with arguments. Even though I set my script mapit.py as executable, I cannot run it from Spotlight. It will only open the script in Xcode!

One way to get it work in Spotlight is to change the script to .command extension. Mac OS will recognize this type of file as a runnable command.

I prefer to keep my script as .py. In UNIX or Mac, I can create an alias file of script: hard link(only hard link works in Spotlight). Here is the command to set it up:

  1. ...$ cd ~/Programming/py
  2. ...$ ln mapit.py mapit.command

The good thing is that any change to my script will be automatically reflected in the command file.

In order to open the Google Map with specified address, I need to copy the address to clipboard first. Then I open my Spotlight and type in mapit.command. It will pick up the address and open Google map in my default browser.

The only thing is that I cannot specify address directly as arguments in Spotlight. This is the limitation of it, and I cannot find a way to do that.

This is why I need to use pyperclip package in my script.




Note: another minor thing has to be done to make it runnable cleanly. The command is running in terminal. When the Spotlight launches the command and opens the map in browser, it will leave the terminal in open status. I prefer to close the terminal automatically.

To do that, go to terminal Preferences..., change the shell setting to Close the Windows:




The last word I have to say is that even the python script is very powerful and convenient to open a map with an address. I think Mac's Automator is much more powerful. It is easy to create (as a service), and can be set to selected text to open a map. Even though, I think it is a very good practice and great programming experience.

References


0 comments: