views:

244

answers:

1

I can create a hyperlink to some url in an NSTextView using the "Link Panel". Or I can add a link manually using the NSLinkAttributeName attribute of NSAttributedString.

I don't want to make a hyperlink to some external url though, I want to be able to create a hyperlink to some text within the NSTextView. Do you know how in Pages you can set some text as a Bookmark, and then you can make a hyperlink to that bookmark? Any ideas or examples of how to go about that?

+1  A: 

I haven't done this in a while so take this with a grain of salt but it goes something like:

  1. You need to define a protocol for you app to handle URLs. It will look something like "myApplicationName://aPath"
  2. In the apps Info.plistfile add the protocol under CFBundleURLTypes key.
  3. Write code for the app to respond to openURL from NSWorkspace. This means the app will have to understand how to convert a URL style path to a specific location in one of its documents.

I think the best method for handling the URL path is to assign a UUID to each document and then a numeric scheme to the paragraph and sentences. The URL ends up looking like: myApplicationName://UUID/paragraphNumber/sentenceNumber/wordNumber. Alternatively you can insert hidden text to define an anchor and just search for that anchor.

TechZen
That's interesting TechZen. Thanks. I can see with the UUID idea how I could make this work between documents, not just within a document. My program is a core data based program so I just need the NSTextView to display the appropriate database entry (no NSWorkspace stuff). I think ultimately the hidden anchor in the document is the best idea because the paragraphNumber/sentenceNumber/wordNumber idea would get messed up as the text of the documents change. Any ideas how to insert a hidden anchor into an NSAttributedString? I haven't looked into that yet.
regulus6633
Try this http://developer.apple.com/mac/library/qa/qa2006/qa1487.html
TechZen
Just insert a custom attribute into the NSAttributedString. You don't need to use the built-in attributes, you can define your own.
Rob Keniger
Thanks guys. I'm on my way. I set up a custom url scheme. It seems that I need to register for the kAEGetURL applescript event as shown here in steps 1 thru 3 in order to receive the url in my app: http://stackoverflow.com/questions/49510/how-do-you-set-your-cocoa-application-as-the-default-web-browser. Now today I'll work on implementing a bookmarking scheme to setup the anchors in nsttributedstring. Then I'll write the parsing code to handle the passed url and select bookmarks as appropriate. This is pretty cool!
regulus6633