Skip to content

Instantly share code, notes, and snippets.

@hiilppp
Created January 5, 2014 14:27
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiilppp/8268816 to your computer and use it in GitHub Desktop.
Save hiilppp/8268816 to your computer and use it in GitHub Desktop.
Python script to append the current location (either as address or Google Maps link) to the provided text (which may be "") in Pythonista and send the result (back) to Drafts.
# -*- coding: utf-8 -*-
# To call script from Drafts, use the follwing URLs as URL Actions:
# - <pythonista://insert_location.py?action=run&argv=[[draft]]&argv=address>
# (Will insert the address of your current location.)
# - <pythonista://insert_location.py?action=run&argv=[[draft]]&argv=link>
# (Will insert a Google Maps link to your current location.)
import location
import re
import sys
import urllib
import webbrowser
a = re.sub(r"(.*\S)$", "\\1 ", sys.argv[1])
a = re.sub(r" \n$", "\n", a)
location.start_updates()
b = location.get_location()
location.stop_updates()
if sys.argv[2] == "address":
b = location.reverse_geocode({"latitude": b["latitude"], "longitude": b["longitude"]})
b = b[0]["Street"] + ", " + b[0]["ZIP"] + " " + b[0]["City"] + ", " + b[0]["Country"]
# b = re.sub(r"(P|p)latz", "\\1l.", b)
# b = re.sub(r"(S|s)trasse", "\\1tr.", b)
# b = re.sub(r", Switzerland", "", b)
b = b.encode("utf-8")
else:
b = "<http://maps.google.com/?q=" + str(b["latitude"]) + "," + str(b["longitude"]) + ">"
c = a + b
webbrowser.open("drafts://x-callback-url/create?text=" + urllib.quote(c))
@hiilppp
Copy link
Author

hiilppp commented Feb 22, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment