1 from PyObjCTools
import AppHelper
2 from Foundation
import *
8 class URLHandlerClass (NSScriptCommand
):
9 def performDefaultImplementation (self
):
10 urlString
= self
.directParameter ()
11 self
.openURL (urlString
)
14 def openURL (self
, urlString
):
15 urlString
= urllib
.unquote (urlString
)
16 m
= re
.match ("^textedit://([^:]*):?([0-9]*):?([0-9]*):?([0-9]*)$", urlString
)
18 NSLog ("URL doesn't match")
25 line
= string
.atoi (m
.group (2))
26 char
= string
.atoi (m
.group (3))
27 column
= string
.atoi (m
.group (4))
32 self
.jumpFile (path
, line
, char
)
34 def charCount (self
, str, line
, char
):
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
]))
44 def jumpFile (self
, path
, line
, char
):
45 NSLog ("Jumping to %s %d %d\n" % (path
, line
, char
))
47 controller
= NSDocumentController
.sharedDocumentController ()
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)
67 NSLog ("Unable to open file %s" % (url
))
68 textView
= doc
.textView
70 str = doc
.textView
.string ()
71 range.location
= self
.charCount (str, line
, char
)
72 range.location
= min (range.location
, len (str))
75 textView
.setSelectedRange_ (range)
76 textView
.scrollRangeToVisible_ (range)
79 if __name__
== "__main__":
80 AppHelper
.runEventLoop ()