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 | # |
Jack Jansen | d390325 | 1996-01-29 15:45:59 +0000 | [diff] [blame] | 14 | import sys |
| 15 | |
Jack Jansen | d390325 | 1996-01-29 15:45:59 +0000 | [diff] [blame] | 16 | import aetools |
Jack Jansen | 742ca03 | 2000-08-20 20:06:51 +0000 | [diff] [blame] | 17 | import Netscape |
Jack Jansen | d390325 | 1996-01-29 15:45:59 +0000 | [diff] [blame] | 18 | import MacOS |
| 19 | |
Jack Jansen | d390325 | 1996-01-29 15:45:59 +0000 | [diff] [blame] | 20 | Error = 'nsremote.Error' |
| 21 | |
| 22 | _talker = None |
| 23 | |
| 24 | def _init(): |
| 25 | global _talker |
| 26 | if _talker == None: |
Jack Jansen | 742ca03 | 2000-08-20 20:06:51 +0000 | [diff] [blame] | 27 | _talker = Netscape.Netscape() |
Jack Jansen | d390325 | 1996-01-29 15:45:59 +0000 | [diff] [blame] | 28 | |
| 29 | def list(dpyinfo=""): |
| 30 | _init() |
| 31 | list = _talker.list_windows() |
| 32 | return map(lambda x: (x, 'version unknown'), list) |
| 33 | |
| 34 | def geturl(windowid=0, dpyinfo=""): |
| 35 | _init() |
| 36 | if windowid == 0: |
| 37 | ids = _talker.list_windows() |
| 38 | if not ids: |
| 39 | raise Error, 'No netscape windows open' |
| 40 | windowid = ids[0] |
| 41 | info = _talker.get_window_info(windowid) |
| 42 | return info |
| 43 | |
| 44 | def openurl(url, windowid=0, dpyinfo=""): |
| 45 | _init() |
| 46 | if windowid == 0: |
| 47 | _talker.OpenURL(url) |
| 48 | else: |
| 49 | _talker.OpenURL(url, toWindow=windowid) |
| 50 | |
| 51 | def _test(): |
| 52 | """Test program: Open www.python.org in all windows, then revert""" |
| 53 | import sys |
| 54 | windows_and_versions = list() |
| 55 | windows_and_urls = map(lambda x: (x[0], geturl(x[0])[0]), windows_and_versions) |
| 56 | for id, version in windows_and_versions: |
| 57 | openurl('http://www.python.org/', windowid=id) |
| 58 | print 'Type return to revert to old contents-' |
| 59 | sys.stdin.readline() |
| 60 | for id, url in windows_and_urls: |
| 61 | openurl(url, id) |
| 62 | |
| 63 | if __name__ == '__main__': |
| 64 | _test() |
| 65 | |