From 07edebe011f3a17d0113e4f34b537e9e3e725a2a Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Tue, 2 Feb 2016 11:47:15 +0200 Subject: Initial commit --- Troublegun.app/Contents/Resources/main.py | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 Troublegun.app/Contents/Resources/main.py (limited to 'Troublegun.app/Contents/Resources/main.py') diff --git a/Troublegun.app/Contents/Resources/main.py b/Troublegun.app/Contents/Resources/main.py new file mode 100755 index 0000000..76cbbb0 --- /dev/null +++ b/Troublegun.app/Contents/Resources/main.py @@ -0,0 +1,62 @@ +#! /usr/bin/env python + +import os +import subprocess +import ConfigParser +from string import Template +from tempfile import NamedTemporaryFile + + +def getresource(res): + from os.path import realpath, join, dirname + return join(dirname(realpath(__file__)), res) + +def getconfig(): + config = dict() + ini = ConfigParser.ConfigParser() + inipath = getresource('config.ini') + ini.read(inipath) + for i in ini.options('message'): + config[i] = ini.get('message', i) + + return config + +def gethelper(): + helper = subprocess.check_output(['sh', getresource('gethelper.sh')]) + return helper.strip() + +def screencapture(): + f = NamedTemporaryFile(delete=False, suffix=".png") + f.close() + subprocess.call(['screencapture', '-C', f.name]) + return f.name + +def makemail(config): + helper = gethelper() + + if "com.apple.mail" in helper: + scpt = file(getresource("mail.scpt"), "r") + elif "entourage" in helper: + scpt = file(getresource("entourage.scpt"), "r") + else: + raise Exception("%s is not a supported mail app" % helper) + + compiled_scpt = Template.substitute(Template(scpt.read()), config) + exe = NamedTemporaryFile(delete=False, suffix=".scpt") + exe.write(compiled_scpt) + exe.close() + return exe.name + +def main(): + config = getconfig() + + if 'screencapture' in config.get('attachments'): + config['filename'] = screencapture() + + exe = makemail(config) + subprocess.call(['osascript', exe]) + os.unlink(exe) + + +if __name__ == '__main__': + main() -- cgit v1.2.3