Revert "Fix percent-escape URI issue (hopefully)."
[lilypad-macos.git] / URLHandlerClass.py
blobd58568004a896c7f5ee1563073052c80031e1cd4
1 from PyObjCTools import AppHelper
2 from Foundation import *
3 from AppKit import *
4 import re
5 import string
6 import urllib
8 class URLHandlerClass (NSScriptCommand):
9 def performDefaultImplementation (self):
10 urlString = self.directParameter ()
11 self.openURL (urlString)
12 return None
14 def openURL (self, urlString):
15 urlString = urllib.unquote (urlString)
16 m = re.match ("^textedit://([^:]*):?([0-9]*):?([0-9]*):?([0-9]*)$", urlString)
17 if m == None:
18 NSLog ("URL doesn't match")
19 return None
21 line = 1
22 char = 1
23 column = 1
24 try:
25 line = string.atoi (m.group (2))
26 char = string.atoi (m.group (3))
27 column = string.atoi (m.group (4))
28 except ValueError:
29 pass
31 path = m.group (1)
32 self.jumpFile (path, line, char)
34 def charCount (self, str, line, char):
35 line -= 1
36 char -= 1
38 lines = string.split (str, '\n')
39 lineChars = sum (map (lambda x: len (x) + 1, lines[:line]))
40 if line < len (lines):
41 lineChars += min (char, len (lines[line]))
42 return lineChars + 1
44 def jumpFile (self, path, line, char):
45 NSLog ("Jumping to %s %d %d\n" % (path, line, char))
47 controller = NSDocumentController.sharedDocumentController ()
49 # FIXME:
51 # When URIs use percent escaping for accented characters,
52 # a point-and-click action *opens* the GUI just fine, but
53 # here we need to handle the URI/filename/path correctly.
55 # A "file URL" on Mac OS X looks like this:
56 # file://localhost/path/to/file.ly
58 # It seems that file URLs of this form cannot be opened:
59 # file://localhost/path/to/sometext%C3%AA.ly
61 # The same file URL without percent escaping is opened fine???
62 # file://localhost/path/to/sometextĂȘ.ly
64 url = NSURL.fileURLWithPath_ (path)
65 (doc, error) = controller.openDocumentWithContentsOfURL_display_error_ (url, True, None)
66 if not error == None:
67 NSLog ("Unable to open file %s" % (url))
68 textView = doc.textView
69 range = NSRange ()
70 str = doc.textView.string ()
71 range.location = self.charCount (str, line, char)
72 range.location = min (range.location, len (str))
73 range.length = 1
75 textView.setSelectedRange_ (range)
76 textView.scrollRangeToVisible_ (range)
77 return None
79 if __name__ == "__main__":
80 AppHelper.runEventLoop ()