public
Created

ST2 Mountain Lion Notifier Access

  • Download Gist
terminal-notifier.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
import sublime
import sublime_plugin
from subprocess import call
import os.path as path
import urllib2
import os
from zipfile import ZipFile
from io import BytesIO
import platform
import json
import shutil
 
 
VERSION = "1.3.0"
URL = "https://github.com/downloads/alloy/terminal-notifier/terminal-notifier_%s.zip" % VERSION
APP = "terminal-notifier_%s/terminal-notifier.app/" % VERSION
APP_ROOT_DIR = "terminal-notifier_%s/" % VERSION
SCRIPT_PATH = path.dirname(path.abspath(__file__))
BIN = "terminal-notifier.app/Contents/MacOS/terminal-notifier"
NOTIFIER = path.join(SCRIPT_PATH, BIN)
VERSION_LOG = path.join(SCRIPT_PATH, "terminal-notifier.json")
CURRENT_VERSION = "0.0.0"
 
 
class NotifyCommand(sublime_plugin.ApplicationCommand):
def run(
self, message=None, title=None, group=None,
remove=None, activate=None, open_url=None, execute=None
):
if message != None or remove != None:
try:
params = [NOTIFIER]
if message != None:
params += ["-message", message]
if remove != None:
params += ["-remove", remove]
if title != None:
params += ["-title", title]
if group != None:
params += ["-group", group]
if activate != None:
params += ["-activate", activate]
if open_url != None:
params += ["-open", open_url]
if execute != None:
params += ["-execute", execute]
call(params)
except:
sublime.error_message("Error sending notifier message!")
return
else:
sublime.error_message("Bad Command!")
 
def is_enabled(self):
if sublime.platform() != "osx":
return False
v, _, _ = platform.mac_ver()
v = float('.'.join(v.split('.')[:2]))
return (v >= 10.8 and path.exists(NOTIFIER))
 
 
def get_notifier():
status = False
try:
unzip_notifier(urllib2.urlopen(URL))
j = json.dumps({"version": VERSION}, sort_keys=True, indent=4, separators=(',', ': '))
with open(VERSION_LOG, 'w') as f:
f.write(j + "\n")
except:
status = True
return status
 
 
def unzip_notifier(bfr):
z = ZipFile(BytesIO(bfr.read()), "r")
for name in z.namelist():
if name.startswith(APP):
file_name = name.replace(APP_ROOT_DIR, "")
if name.endswith("/"):
os.makedirs(path.join(SCRIPT_PATH, file_name))
else:
with open(path.join(SCRIPT_PATH, file_name), "wb") as f:
f.write(z.read(name))
os.chmod(path.join(SCRIPT_PATH, file_name), 0777)
z.close()
 
 
def init():
global CURRENT_VERSION
 
# Get current version if available
if path.exists(VERSION_LOG):
with open(VERSION_LOG, "r") as f:
CURRENT_VERSION = json.load(f)["version"]
 
# Get the notifier app if needed
# Upgrade if out of date
if not path.exists(NOTIFIER):
if get_notifier():
sublime.error_message("Failed to acquire terminal-notifier!")
else:
CURRENT_VERSION = VERSION
elif CURRENT_VERSION != VERSION:
try:
shutil.rmtree(path.join(SCRIPT_PATH, "terminal-notifier.app"))
except:
sublime.error_message("Failed to remove old version of terminal-notifer!")
return
if get_notifier():
sublime.error_message("Failed to acquire terminal-notifier!")
else:
CURRENT_VERSION = VERSION
 
init()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.