Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Files /
Chapter 3 - Standard File Package / Using the Standard File Package


Setting the Current Directory

The first time your application calls one of the Standard File Package routines, the default current directory (that is, the directory whose contents are listed in the dialog box) is determined by the way in which your application was launched.

At each subsequent call to one of the Standard File Package routines, the default current directory is simply the directory that was current when the user completed the previous dialog box. You can use the function GetSFCurDir defined in Listing 3-11 to determine the current directory.

Listing 3-11 Determining the current directory

FUNCTION GetSFCurDir: LongInt;
TYPE
   LongIntPtr = ^LongInt;
CONST
   CurDirStore = $398;
BEGIN
   GetSFCurDir := LongIntPtr(CurDirStore)^;
END;
You can use the GetSFCurVol function defined in Listing 3-12 to determine the
current volume.

Listing 3-12 Determining the current volume

FUNCTION GetSFCurVol: Integer;
TYPE
   IntPtr = ^Integer;
CONST
   SFSaveDisk = $214;
BEGIN
   GetSFCurVol := -IntPtr(SFSaveDisk)^;
END;
If necessary, you can change the default current directory and volume. For example,
when the user needs to select a dictionary file for a spell-checking application, the application might set the current directory to a directory containing document-specific dictionary files. This saves the user from having to navigate the directory hierarchy from the directory containing documents to that containing dictionary files. You can use the procedure SetSFCurDir defined in Listing 3-13 to set the current directory.

Listing 3-13 Setting the current directory

PROCEDURE SetSFCurDir (dirID: LongInt);
TYPE
   LongIntPtr = ^LongInt;
CONST
   CurDirStore = $398;
BEGIN
   LongIntPtr(CurDirStore)^ := dirID;
END;
You can use the procedure SetSFCurVol defined in Listing 3-14 to set the current volume.

Listing 3-14 Setting the current volume

PROCEDURE SetSFCurVol (vRefNum: Integer);
TYPE
   IntPtr = ^Integer;
CONST
   SFSaveDisk = $214;
BEGIN
   IntPtr(SFSaveDisk)^ := -vRefNum;
END;
Note
Most applications don't need to alter the default current directory
or volume.
If you are using the enhanced Standard File Package routines, you can set the current directory by filling in the fields of the file system specification in the reply record passed to CustomGetFile or CustomPutFile. You do this within your dialog hook function. Listing 3-15 defines a dialog hook function that makes the currently active System Folder the current directory.

Listing 3-15 Setting the current directory

FUNCTION MyDlgHook (item: Integer; theDialog: DialogPtr; myDataPtr: Ptr):
                   Integer;
VAR
   myReplyPtr:    StandardFileReplyPtr;
   foundVRefNum:  Integer;
   foundDirID:    LongInt;
   myErr:         OSErr;
BEGIN
   MyDlgHook := item;            {by default, return the item passed in}
   IF GetWRefCon(WindowPtr(theDialog)) <> LongInt(sfMainDialogRefCon) THEN
      Exit(MyDlgHook);           {this function is only for main dialog box}

   CASE item OF
      sfHookFirstCall:           {pseudo-item: first time function called}
         BEGIN
            myReplyPtr := StandardFileReplyPtr(myDataPtr);
            myErr := FindFolder(kOnSystemDisk, kSystemFolderType,
                            kDontCreateFolder, foundVRefNum, foundDirID);
            IF myErr = noErr THEN
               BEGIN
                  myReplyPtr^.sfFile.parID := foundDirID;
                  myReplyPtr^.sfFile.vRefNum := foundVRefNum;
                  MyDlgHook := sfHookChangeSelection;
               END;
         END;
      OTHERWISE
         ;                       {ignore all other items}
   END;
END;
This dialog hook function installs the System Folder's volume reference number and parent directory ID into the file system specification whose address is passed in the myDataPtr parameter. Because the dialog hook function returns the constant sfHookChangeSelection the first time it is called (that is, in response to the sfHookFirstCall pseudo-item), the Standard File Package sets the current directory to the indicated directory when the dialog box is displayed.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
2 JUL 1996