Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

hiilppp commented Feb 22, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.