comparison my-enso-commands.py @ 7:41bc59775872

wow, haven't committed changes in a long time.
author Atul Varma <varmaa@toolness.com>
date Sat, 27 Mar 2010 12:19:32 -0700
parents c41c4896c97e
children 4285e37bea69
comparison
equal deleted inserted replaced
6:c41c4896c97e 7:41bc59775872
56 56
57 def _get_clipboard_img_datafile(): 57 def _get_clipboard_img_datafile():
58 import AppKit 58 import AppKit
59 59
60 pb = AppKit.NSPasteboard.generalPasteboard() 60 pb = AppKit.NSPasteboard.generalPasteboard()
61 typestr = pb.availableTypeFromArray_([AppKit.NSTIFFPboardType,
62 AppKit.NSPICTPboardType])
61 tiffbytes = None 63 tiffbytes = None
62 if pb.availableTypeFromArray_([AppKit.NSTIFFPboardType]): 64 if typestr == AppKit.NSTIFFPboardType:
63 tiffbytes = pb.dataForType_(AppKit.NSTIFFPboardType).bytes() 65 tiffbytes = pb.dataForType_(AppKit.NSTIFFPboardType).bytes()
64 elif pb.availableTypeFromArray_([AppKit.NSPICTPboardType]): 66 elif typestr == AppKit.NSPICTPboardType:
65 img = AppKit.NSImage.alloc().initWithPasteboard_(pb) 67 img = AppKit.NSImage.alloc().initWithPasteboard_(pb)
66 tiffbytes = img.TIFFRepresentation().bytes() 68 tiffbytes = img.TIFFRepresentation().bytes()
67 69
68 if tiffbytes: 70 if tiffbytes:
69 return StringIO(str(tiffbytes)) 71 return StringIO(str(tiffbytes))
72 def image_command(func): 74 def image_command(func):
73 def wrapper(ensoapi): 75 def wrapper(ensoapi):
74 import Image 76 import Image
75 77
76 datafile = _get_clipboard_img_datafile() 78 datafile = _get_clipboard_img_datafile()
79
77 if datafile: 80 if datafile:
78 img = Image.open(datafile) 81 img = Image.open(datafile)
79 return func(ensoapi, img) 82 return func(ensoapi, img)
80 else: 83 else:
81 files = ensoapi.get_selection().get("files", []) 84 files = ensoapi.get_selection().get("files", [])
351 354
352 query = urllib.quote( query.encode("utf-8") ) 355 query = urllib.quote( query.encode("utf-8") )
353 356
354 webbrowser.open( self._url_template % {"query" : query} ) 357 webbrowser.open( self._url_template % {"query" : query} )
355 358
356 cmd_wiki = WebSearchCmd("http://en.wikipedia.org/wiki/%(query)s") 359 cmd_wiki = WebSearchCmd("http://en.wikipedia.org/wiki/Special:Search?search=%(query)s")
357 cmd_wowhead = WebSearchCmd("http://www.wowhead.com/?search=%(query)s") 360 cmd_wowhead = WebSearchCmd("http://www.wowhead.com/?search=%(query)s")
358 cmd_amaz = WebSearchCmd("http://www.amazon.com/exec/obidos/search-handle-url/index%%3Dblended%%26field-keywords%%3D%(query)s%%26store-name%%3Dall-product-search") 361 cmd_amaz = WebSearchCmd("http://www.amazon.com/exec/obidos/search-handle-url/index%%3Dblended%%26field-keywords%%3D%(query)s%%26store-name%%3Dall-product-search")
359 cmd_yp = WebSearchCmd("http://local.google.com/local?sc=1&q=%(query)s&near=2418+washington+94115&btnG=Google+Search") 362 cmd_yp = WebSearchCmd("http://local.google.com/local?sc=1&q=%(query)s&near=2418+washington+94115&btnG=Google+Search")
360 cmd_imdb = WebSearchCmd("http://www.imdb.com/find?s=all&q=%(query)s&x=0&y=0") 363 cmd_imdb = WebSearchCmd("http://www.imdb.com/find?s=all&q=%(query)s&x=0&y=0")
361 cmd_mdc = WebSearchCmd("http://www.google.com/search?hl=en&q=%(query)s+site%%3Adeveloper.mozilla.org&btnG=Google+Search") 364 cmd_mdc = WebSearchCmd("http://www.google.com/search?hl=en&q=%(query)s+site%%3Adeveloper.mozilla.org&btnG=Google+Search")
634 ensoapi.display_message("An error occurred.") 637 ensoapi.display_message("An error occurred.")
635 638
636 os.remove(source_name) 639 os.remove(source_name)
637 os.remove(output_name) 640 os.remove(output_name)
638 641
642 def cmd_wow_firefox(ensoapi):
643 popen = subprocess.Popen(["/Users/varmaa/bin/wow-firefox"])
644
645 def cmd_work_firefox(ensoapi):
646 popen = subprocess.Popen(["/Users/varmaa/bin/work-firefox"])
647
648 def cmd_mozpics_firefox(ensoapi):
649 popen = subprocess.Popen(["/Users/varmaa/bin/mozpics-firefox"])
650
651 def cmd_firefox(ensoapi):
652 popen = subprocess.Popen(["/Users/varmaa/bin/personal-firefox"])
653
639 def cmd_nosetest(ensoapi): 654 def cmd_nosetest(ensoapi):
640 """ 655 """
641 runs nosetests on the selected region of python code. 656 runs nosetests on the selected region of python code.
642 """ 657 """
643 658
682 raise AssertionError( "nosetests didn't return anything!" ) 697 raise AssertionError( "nosetests didn't return anything!" )
683 698
684 os.remove(source_name) 699 os.remove(source_name)
685 os.remove(output_name) 700 os.remove(output_name)
686 701
702 def cmd_markdown_to_html(ensoapi):
703 """
704 Converts the plaintext markdown selection to plaintext
705 HTML source.
706 """
707
708 import markdown2
709 text = ensoapi.get_selection().get("text", "")
710 html = markdown2.markdown(text)
711 ensoapi.set_selection({"text": html})
712
713 def cmd_unquote(ensoapi):
714 """
715 Removes a '<' at the beginning of every line.
716 """
717
718 text = ensoapi.get_selection().get("text", "")
719 lines = ["%s\n" % line[2:]
720 for line in text.splitlines()
721 if line.startswith("> ")]
722 ensoapi.set_selection({"text": ''.join(lines)})
723
724 def cmd_quote(ensoapi):
725 """
726 Puts a '>' at the beginning of every line.
727 """
728
729 text = ensoapi.get_selection().get("text", "")
730 lines = ["> %s\n" % line
731 for line in text.splitlines()]
732 ensoapi.set_selection({"text": ''.join(lines)})
733
734 def cmd_markdown(ensoapi):
735 """
736 Converts the plaintext markdown selection to rich text.
737 """
738
739 import markdown2
740 text = ensoapi.get_selection().get("text", "")
741 html = markdown2.markdown(text)
742 ensoapi.set_selection({"html": html,
743 "text": text})
744
687 def cmd_insert_html(ensoapi): 745 def cmd_insert_html(ensoapi):
688 ensoapi.set_selection( {"html":"<b>hi</b> <p>there</p>"} ) 746 ensoapi.set_selection( {"html":"<b>hi</b> <p>there</p>"} )
689 747
690 def _runAppleScript( script ): 748 def _runAppleScript( script ):
691 params = [ "osascript", "-e", script ] 749 params = [ "osascript", "-e", script ]
785 843
786 def cmd_html_template(ensoapi): 844 def cmd_html_template(ensoapi):
787 ensoapi.set_selection({"text" : HTML_TEMPLATE}) 845 ensoapi.set_selection({"text" : HTML_TEMPLATE})
788 846
789 HTML_TEMPLATE = """\ 847 HTML_TEMPLATE = """\
790 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 848 <html>
791 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
792 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
793 <head> 849 <head>
794 <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 850 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
795 <link rel="stylesheet" type="text/css" media="all" 851 <link rel="stylesheet" type="text/css" media="all"
796 href="" /> 852 href="" />
797 <title></title> 853 <title></title>
798 </head> 854 </head>
799 <body> 855 <body>
800 856
801 </body> 857 </body>
802 <script type="text/javascript" src=""></script> 858 <script src=""></script>
803 </html> 859 </html>
804 """ 860 """