aboutsummaryrefslogtreecommitdiffstats
path: root/Troublegun.scptd/Contents/Resources/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'Troublegun.scptd/Contents/Resources/main.py')
-rwxr-xr-xTroublegun.scptd/Contents/Resources/main.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/Troublegun.scptd/Contents/Resources/main.py b/Troublegun.scptd/Contents/Resources/main.py
new file mode 100755
index 0000000..76cbbb0
--- /dev/null
+++ b/Troublegun.scptd/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()