Skip to content

Instantly share code, notes, and snippets.

@matthewfl
Last active May 25, 2021 21:17
Show Gist options
  • Save matthewfl/bc37c8ac1434720d278806fbd19317fe to your computer and use it in GitHub Desktop.
Save matthewfl/bc37c8ac1434720d278806fbd19317fe to your computer and use it in GitHub Desktop.
Get a notification when latexmk fails
#!/usr/bin/env python3
import subprocess
import sys
def main():
proc = subprocess.Popen(['latexmk', '-pvc'] + sys.argv[1:],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=0,
universal_newlines=False)
out = ''
while True:
li = proc.stdout.read(1)
if not li:
if proc.poll() is not None:
sys.exit(1)
continue
li = li.decode('utf-8', 'ignore')
out += li
out = out[-2000:]
if out.endswith('\n? '):
# then we have failed, so log an error
out = '\n'.join(out.split('\n')[-10:-1])
print('send notification')
subprocess.Popen(['notify-send', '-t', '10000', '-u', 'critical',
'Latex compile failed', out])
# make it continue to compile
if sys.argv[0].endswith('r'):
proc.stdin.write(b'R\n')
else:
proc.stdin.write(b'X\n')
sys.stdout.write(li)
if __name__ == '__main__':
main()
@se4u
Copy link

se4u commented Mar 9, 2018

On OSX use the following instead of notify-send

osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'

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