Jack Jansen | d390325 | 1996-01-29 15:45:59 +0000 | [diff] [blame] | 1 | """nsremote - Control Netscape from python. |
| 2 | |
| 3 | Interface modelled after unix-interface done |
| 4 | by hassan@cs.stanford.edu. |
| 5 | |
| 6 | Jack Jansen, CWI, January 1996. |
| 7 | """ |
| 8 | # |
| 9 | # Note: this module currently uses the funny SpyGlass AppleEvents, since |
| 10 | # these seem to be the only way to get the info from Netscape. It would |
| 11 | # be nicer to use the more "object oriented" standard OSA stuff, when it |
| 12 | # is implemented in Netscape. |
| 13 | # |
| 14 | import addpack |
| 15 | import sys |
| 16 | |
| 17 | addpack.addpack('Tools') |
| 18 | addpack.addpack('bgen') |
| 19 | addpack.addpack('ae') |
| 20 | |
| 21 | import aetools |
| 22 | import Standard_Suite |
| 23 | import WWW_Suite |
| 24 | import MacOS |
| 25 | |
| 26 | class Netscape(aetools.TalkTo, Standard_Suite.Standard_Suite, WWW_Suite.WWW_Suite): |
| 27 | pass |
| 28 | |
| 29 | SIGNATURE='MOSS' |
| 30 | |
| 31 | Error = 'nsremote.Error' |
| 32 | |
| 33 | _talker = None |
| 34 | |
| 35 | def _init(): |
| 36 | global _talker |
| 37 | if _talker == None: |
| 38 | _talker = Netscape(SIGNATURE) |
| 39 | |
| 40 | def list(dpyinfo=""): |
| 41 | _init() |
| 42 | list = _talker.list_windows() |
| 43 | return map(lambda x: (x, 'version unknown'), list) |
| 44 | |
| 45 | def geturl(windowid=0, dpyinfo=""): |
| 46 | _init() |
| 47 | if windowid == 0: |
| 48 | ids = _talker.list_windows() |
| 49 | if not ids: |
| 50 | raise Error, 'No netscape windows open' |
| 51 | windowid = ids[0] |
| 52 | info = _talker.get_window_info(windowid) |
| 53 | return info |
| 54 | |
| 55 | def openurl(url, windowid=0, dpyinfo=""): |
| 56 | _init() |
| 57 | if windowid == 0: |
| 58 | _talker.OpenURL(url) |
| 59 | else: |
| 60 | _talker.OpenURL(url, toWindow=windowid) |
| 61 | |
| 62 | def _test(): |
| 63 | """Test program: Open www.python.org in all windows, then revert""" |
| 64 | import sys |
| 65 | windows_and_versions = list() |
| 66 | windows_and_urls = map(lambda x: (x[0], geturl(x[0])[0]), windows_and_versions) |
| 67 | for id, version in windows_and_versions: |
| 68 | openurl('http://www.python.org/', windowid=id) |
| 69 | print 'Type return to revert to old contents-' |
| 70 | sys.stdin.readline() |
| 71 | for id, url in windows_and_urls: |
| 72 | openurl(url, id) |
| 73 | |
| 74 | if __name__ == '__main__': |
| 75 | _test() |
| 76 | |