Change in File Link Behavior in NSTextView Under Leopard

  • I have a Cocoa application ( compiled under XCode 2.5 to work under
    both Tiger and Leopard) that lets users create links to files on disk.

    Under Tiger, clicking one of these links acts just like double-
    clicking the file icon in the Finder--it opens the file in its default
    application.

    Under Leopard, clicking a file link just shows the file in the Finder.

    I thought at first that Leopard might have added a System Preference
    item to control this, but haven't found anything.

    So, has the behavior of file links been changed in Leopard, or am I
    seeing a flaw in my approach that just slipped through in Tiger? The
    code involved is pretty simple.

    My routine gets an NSURL from from the open panel like this:

    NSString *pathName = [[oPanel filenames] objectAtIndex:0];

    NSURL *theURL =[NSURL fileURLWithPath:pathName];

    The resulting pathName will look something like this:

    file://localhost/Users/mikewright/Pictures/fourcards.jpg.

    Then, I add theURL to a piece of text in an NSTextView,
    freeformTextView:

    NSDictionary *attributes = [NSDictionary dictionaryWithObject:theURL
    forKey:NSLinkAttributeName];

    [[freeformTextView textStorage] addAttributes:(NSDictionary
    *)attributes range:(NSRange)selectionRange];

    Since I have an NSWindowController that contains freeformTextView, I
    decided to  implement -textView:clickedOnLink:atIndex: and, within
    that, call LSOpenCFURLRef to launch the URL in the link.

    That works great under both Tiger and Leopard.

    However...I also have an NSTableView that can contain links. In this
    case, the link is stored as a plain text as a standard URL, and is
    only activated when the cell containing it is clicked.

    I have an NSObject, EditorTableController, set as the delegate for the
    NSTableView.

    In EditorTableController , there is a routine that activates the link
    in a clicked cell. There I call:

    NSTextView *tableTextView = (NSTextView *)[[editorWindowController
    window] firstResponder];

    So, in that routine, I decided to add:

    [tableTextView setDelegate:self];

    I also added -textView:clickedOnLink:atIndex: to
    EditorTableController, just as I did for my window controller.

    After that, clicking file links caused the files to open just like the
    ones in freeformTextView.

    Great!

    There's just one little glitch--now when I make changes in a cell and
    then select a different cell, those changes evaporate.

    I've read and re-read the NSTableView documentation, but I haven't
    been able to find out how to keep that from happening.

    I've also tried saving the tableTextView delegate before the
    setDelegate:self call, and then restoring it in -
    tableView:shouldEditTableColumn:row: and several other places, but
    nothing seems to have any effect on this last problem.

    The best solution would be if there were some way to tell Leopard to
    behave like Tiger when a file link is clicked.

    If that can't be done, is there some solution to the NSTableView
    problem that I have missed?

    Thanks,
    Mike Wright
    http://www.idata3.com/
    http://www.raccoonbend.com/
  • On Jan 12, 2008, at 11:20 AM, Mike Wright wrote:

    > Under Tiger, clicking one of these links acts just like double-
    > clicking the file icon in the Finder--it opens the file in its
    > default application.
    >
    > Under Leopard, clicking a file link just shows the file in the Finder.
    >
    > I thought at first that Leopard might have added a System Preference
    > item to control this, but haven't found anything.
    >
    > So, has the behavior of file links been changed in Leopard, or am I
    > seeing a flaw in my approach that just slipped through in Tiger? The
    > code involved is pretty simple.

    This change was made for security reasons.  As you note, the text
    view's delegate can override this behavior.

    In the case of a table view, the text view you are dealing with is the
    field editor.  This is shared by all of the controls in a window, and
    its state is normally reset whenever the editing focus changes.
    Ordinarily the control would be set up as the field editor's delegate,
    so the natural thing to try would be to implement -
    textView:clickedOnLink:atIndex: in a custom NSTableView subclass.

    Douglas Davidson
  • On Jan 14, 2008, at 1:43 PM, Douglas Davidson wrote:

    > On Jan 12, 2008, at 11:20 AM, Mike Wright wrote:
    >
    >> Under Tiger, clicking one of these links acts just like double-
    >> clicking the file icon in the Finder--it opens the file in its
    >> default application.
    >>
    >> Under Leopard, clicking a file link just shows the file in the
    >> Finder.
    >>
    >> I thought at first that Leopard might have added a System
    >> Preference item to control this, but haven't found anything.
    >>
    >> So, has the behavior of file links been changed in Leopard, or am I
    >> seeing a flaw in my approach that just slipped through in Tiger?
    >> The code involved is pretty simple.
    >
    > This change was made for security reasons.  As you note, the text
    > view's delegate can override this behavior.
    >
    > In the case of a table view, the text view you are dealing with is
    > the field editor.  This is shared by all of the controls in a
    > window, and its state is normally reset whenever the editing focus
    > changes.  Ordinarily the control would be set up as the field
    > editor's delegate, so the natural thing to try would be to implement
    > -textView:clickedOnLink:atIndex: in a custom NSTableView subclass.
    >
    > Douglas Davidson

    Thanks. Worked like a charm.

    Regards,
    Mike Wright
    http://www.idata3.com/
    http://www.raccoonbend.com/
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today