Professional Web Applications Themes

Navigation Services - Sample Code? - Mac Programming

Can anyone point me to some sample code for using Navigation Services to create a new file, save text to it, and save the file to disk? The sample code that comes with the Navigation Services SDK is outdated and uses "not recommended" methods. I'm attempting to create a file using Nav Serv., but when I call FSpCreate() on the returned FSSpec I get an error -38 ("file is already on the disk"), and if I ignore that and subsequently call FSpOpenDF() I get an error -37 (file not found). Umm... what? I think the problem is that when I ...

  1. #1

    Default Navigation Services - Sample Code?

    Can anyone point me to some sample code for using Navigation Services to
    create a new file, save text to it, and save the file to disk?

    The sample code that comes with the Navigation Services SDK is outdated
    and uses "not recommended" methods.

    I'm attempting to create a file using Nav Serv., but when I call
    FSpCreate() on the returned FSSpec I get an error -38 ("file is already
    on the disk"), and if I ignore that and subsequently call FSpOpenDF() I
    get an error -37 (file not found). Umm... what?

    I think the problem is that when I tell it to create a file called
    "MyFile.txt" in the save dialog, the file name isn't returned by the
    dialog, but rather it ruturns the name of the folder in which I wish to
    create the file.

    Any thoughts or working sample code?

    Thanks,

    CT
    Charles Guest

  2. #2

    Default Re: Navigation Services - Sample Code?

    On Wed, 12 Nov 2003, Charles Thomas wrote:
     
    The FSSpec/FSRef you get is indeed for the containing folder.

    I basically do this:

    NavGetDefaultDialogCreationOptions(&newOptions);

    newOptions.saveFileName=defaultName;
    newOptions.optionFlags&=!kNavSelectDefaultLocation ;
    newOptions.optionFlags|=kNavNoTypePopup;
    if(noErr==NavCreatePutFileDialog(&newOptions,kMyTy pe,kMyCreator,NULL,NULL,&navDialog))
    {
    if(noErr==NavDialogRun(navDialog))
    {
    if(NavDialogGetUserAction(navDialog)==kNavUserActi onSaveAs)
    {

    if(noErr==NavDialogGetReply(navDialog,&reply))
    {

    AEKeyword key;
    DescType type;
    Size s;


    if(noErr==AEGetNthPtr(&reply.selection,1,typeFSRef ,&key,&type,&myFSRef,sizeof(FSRef),&s))
    {
    /*my fsref is now an fsref for the
    folder the user saved in, and the filename is in reply.saveFileName*/
    }
    NavDisposeReply(&reply);
    }
    }
    }
    NavDialogDispose(navDialog);
    }
     

    Frederick Guest

  3. #3

    Default Re: Navigation Services - Sample Code?

    In article
    <srcf.societies.cam.ac.uk>,
    Frederick Cheung <DUH.ucam.org> wrote:
     

    So assuming you want to create a file, you then create a new FSSpec with
    your filename and the volume number from the folder FSSpec?

    CT
    Charles Guest

  4. #4

    Default Re: Navigation Services - Sample Code?

    On Wed, 12 Nov 2003, Charles Thomas wrote:
     
    >
    > So assuming you want to create a file, you then create a new FSSpec with
    > your filename and the volume number from the folder FSSpec?
    >[/ref]
    And with the folder FSSpec's dirID as your parID.

    Fred

    Frederick Guest

  5. #5

    Default Re: Navigation Services - Sample Code?

    In article
    <srcf.societies.cam.ac.uk>,
    Frederick Cheung <DUH.ucam.org> wrote:
     


    I'm with you so far, Fred. I guess I'm a little unclear about how to
    take this FSSpec, which points to a directory, and use it in a call to
    FSpCreate() to make the new file.

    At first I thought I'd use FSMakeFSSpec(), but that doesn't seem to be
    the right answer.

    CT
    Charles Guest

  6. #6

    Default Re: Navigation Services - Sample Code?

    In article <doit.wisc.edu>,
    Charles Thomas <facstaff.wisc.edu> wrote:
     

    Never mind.

    I just saw that FSMakeFSSpec() will return fnfErr when you're creating a
    new file, and that this error can just be ignored. I just had to change
    my error handling to ignore fnfErr.

    Now all I need is a way to turn the parID of the directory FSSpec into
    the dirID needed by FSMakeFSSpec() and I'm good to go.

    It's making the file correctly now, just in the parent of the directory
    I select in the file dialog.

    I'm on the right track.

    CT
    Charles Guest

  7. #7

    Default Re: Navigation Services - Sample Code?

    On Wed, 12 Nov 2003, Charles Thomas wrote:
     
    >
    >
    > I'm with you so far, Fred. I guess I'm a little unclear about how to
    > take this FSSpec, which points to a directory, and use it in a call to
    > FSpCreate() to make the new file.>
    > At first I thought I'd use FSMakeFSSpec(), but that doesn't seem to be
    > the right answer.[/ref]

    I would convert the name that is given as a CFStringRef to an array of
    UniChars (using CFStringGetCharacters), then do something like

    FSCreateFileUnicode(&myFSRef,nameLength,name,kFSCa tInfoNone,NULL,&fileRef,&fileSpec);
    Where myFSRef is the FSRef to the directory, name is an array of UniChars
    of length nameLength. On exit, (if sucessful), fileRef is an FSRef
    referencing the newly create file (ditto fileSpec except that it's a
    FSSpec).

    Fred

    Frederick Guest

  8. #8

    Default Re: Navigation Services - Sample Code?

    On Wed, 12 Nov 2003, Charles Thomas wrote:
     
    >
    > Never mind.
    >
    > I just saw that FSMakeFSSpec() will return fnfErr when you're creating a
    > new file, and that this error can just be ignored. I just had to change
    > my error handling to ignore fnfErr.
    >
    > Now all I need is a way to turn the parID of the directory FSSpec into
    > the dirID needed by FSMakeFSSpec() and I'm good to go.[/ref]

    MoreFiles (Apple sample code) has code that will give you the dirID for a
    directory specified
    by an FSSpec

    Fred

    Frederick Guest

  9. #9

    Default Re: Navigation Services - Sample Code?

    In article
    <srcf.societies.cam.ac.uk>,
    Frederick Cheung <DUH.ucam.org> wrote:
     

    I've found code in MoreFiles.c that will give you a dirID of a FILE
    given a parID (CreateFileIDRef()), but it doesn't work with a directory.

    This all seems remarkably stupid to me, thus I have to believe that I'm
    doing something terribly wrong.

    When you create a dialog to ask the user for the name of a file and a
    directory to put it in, why on EARTH wouldn't the FSSpec you get
    returned just BE the FSSpec for the file the user specified? It used to
    be that way Pre-Carbon.

    To return an FSSpec for the directory which only contains a parID and
    not a dirID seems SO illogical that I can't believe that Apple would set
    it up that way.

    It simply cannot be so difficult to ask the user the name of a file they
    want to create and where they want it saved with a file dialog, then
    create the file. This is something people do ALL the time, and I can't
    believe they're digging through outdated and obscure code in MoreFiles.c
    to figure out how to do it.

    Whenever things seem this overly complex I'm suspious that I'm going
    about things in the totally wrong way.

    CT
    Charles Guest

  10. #10

    Default Re: Navigation Services - Sample Code?

    On Wed, 12 Nov 2003, Charles Thomas wrote:
     
    >
    > I've found code in MoreFiles.c that will give you a dirID of a FILE
    > given a parID (CreateFileIDRef()), but it doesn't work with a directory.
    >[/ref]
    You want FSpGetDirectoryID.
    Call that on the fsspec for the parent folder, then build an fsspec with
    the same volume ref num, the name of your choice, and as parID the dirID
    you have just found.
     
    Because we now work with FSRefs, and you cannot have an FSRef for an
    inexistant object, ie you need the parent folder/name combo
     
    You don't need all that stuff if you are using FSRefs.
     

    Frederick Guest

  11. #11

    Default Re: Navigation Services - Sample Code?

    In article
    <srcf.societies.cam.ac.uk>,
    Frederick Cheung <DUH.ucam.org> wrote:
     

    Works like a charm. Many thanks, once again.
     

    Can you recommend some docs for working with FSRefs? All the
    doentation I've seen so far deals with FSSpec files.

    If there is indeed an easier way to deal with files, I'm all for it.

    CT
    Charles Guest

  12. #12

    Default Re: Navigation Services - Sample Code?

    On Thu, 13 Nov 2003, Charles Thomas wrote:
     
    >
    > Works like a charm. Many thanks, once again.

    >
    > Can you recommend some docs for working with FSRefs? All the
    > doentation I've seen so far deals with FSSpec files.[/ref]

    I don't think there's much besides the api reference, but I've found it
    quite straightforward.
    If you're moving existing FSSpec based code, you may find this technote
    useful:
    http://developer.apple.com/technotes/tn2002/tn2078.html
     
    The FSRef APIs also allow you to deal with long/unicode file names, and
    manipulate files bigger than 2 gigs.

    Fred

    Frederick Guest

Similar Threads

  1. Code Sample
    By y c a r t in forum Macromedia Flex General Discussion
    Replies: 3
    Last Post: September 8th, 08:07 PM
  2. ethernet sample code
    By Steve Modica in forum Mac Networking
    Replies: 0
    Last Post: September 9th, 04:35 PM
  3. Code review of cross platform code sample
    By Otto in forum UNIX Programming
    Replies: 5
    Last Post: September 7th, 02:06 PM
  4. Impersonate code sample?
    By Chris Jackson in forum ASP.NET Security
    Replies: 2
    Last Post: August 4th, 08:03 PM
  5. DB Sample Code
    By Frank Py in forum ASP Database
    Replies: 3
    Last Post: July 2nd, 02:28 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139