home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-07-04 | 6.0 KB | 236 lines | [TEXT/PJMM] |
- { This application demonstrates the use of the Alias Manager to create an alias to a file. }
- {}
- { Simply, we will allow the user to choose a file from standard get file and then will }
- { ask the user to name an alias to that file using standard put file. The alias file will then }
- { be created to the originally selected file. }
- {}
- { NOTE: Aliases made in this manner will be automatically updated by the Finder whenever }
- { the original file moves. }
- {}
- { NOTE: This is a quick & dirty demonstration…resources should be used for strings, etc. }
- { but are not being used due to time. }
- {}
- { by Joe Zobkiw 6/12/91 }
- {}
- { contact information: }
- { AOL: AFL Zobkiw }
- { CIS: 70712,515 }
- { Internet: 70712.515@compuserve.com }
- {}
-
- program AliasMaker;
- uses
- Types, OSUtils, Files, Aliases;
-
- const
- kSystem7 = $0700; { system 7.0 }
- kAliasNameExtension = ' alias'; { filename extension }
- kAliasResourceID = 0; { resource ID of ‘alis’ resource }
- kIsAliasFlag = 32768; { to set Finder flag }
-
- {____________________________________________________________}
- { This routine initializes the Macintosh Toolbox }
- {____________________________________________________________}
- procedure InitMacintosh;
- begin
- InitGraf(@thePort);
- InitFonts;
- InitWindows;
- InitMenus;
- TEInit;
- InitDialogs(nil);
- FlushEvents(EveryEvent, 0);
- InitCursor;
- end;
-
- {____________________________________________________________}
- { make sure we can run, we should really check Gestalt here }
- {____________________________________________________________}
- procedure CheckEnvironment;
- var
- env: SysEnvRec;
- err: OSerr;
- begin
- err := SysEnvirons(1, env);
- if (err <> noErr) then
- ExitToShell;
- if (env.systemVersion < kSystem7) then
- ExitToShell;
- end;
-
- {____________________________________________________________}
- { this function gets a file to alias }
- {____________________________________________________________}
- procedure GetFileToAlias (var reply: SFReply; var spec: FSSpec);
- var
- where: Point;
- typeList: SFTypeList;
- vRefNum: integer;
- parDirID: longint;
- procID: longint;
- err: OSerr;
- begin
- SetPt(where, 0, 0);
- SFGetFile(where, '', nil, -1, typeList, nil, reply);
-
- if reply.good then
- begin
- { get some working directory information for the original file }
- err := GetWDInfo(reply.vRefNum, vRefNum, parDirID, procID);
- if (err <> noErr) then
- ExitToShell;
-
- { fill in the spec record now }
- spec.vRefNum := vRefNum;
- spec.parID := parDirID;
- spec.name := reply.fName;
-
- end;
- end;
-
- {____________________________________________________________}
- { find out where to save the alias }
- {____________________________________________________________}
- procedure GetAliasInformation (fName: Str31; var reply: SFReply);
- var
- where: Point;
- typeList: SFTypeList;
- origName: Str63;
-
- begin
- SetPt(where, 0, 0);
- origName := Concat(fName, kAliasNameExtension);
- SFPutFile(where, 'Save the alias as:', origName, nil, reply);
- end;
-
- {____________________________________________________________}
- { create an alias file…this is the meat of this program! }
- {____________________________________________________________}
- function CreateAlias (originalReply: SFReply; aliasReply: SFReply; spec: FSSpec): OSErr;
- var
- err: OSErr;
- myAliasHandle: AliasHandle;
- aliasFileRefNum: integer;
- saveResFile: integer;
- fndrInfo: FInfo;
- vRefNum: integer;
- parDirID: longint;
- procID: longint;
-
- procedure Fail (err: OSErr);
- begin
- if (err <> noErr) then
- begin
- CreateAlias := err;
-
- UseResFile(saveResFile);
-
- if myAliasHandle <> nil then
- DisposHandle(Handle(myAliasHandle));
-
- Exit(CreateAlias);
- end;
- end;
-
- begin
- CreateAlias := noErr;
-
- { save our current file }
- saveResFile := CurResFile;
-
- { create the new alias }
- err := NewAlias(nil, spec, myAliasHandle);
- Fail(err);
-
- { check the handle for nil }
- if (myAliasHandle = nil) then
- Fail(memFullErr);
-
- { get some working directory information for the alias file }
- err := GetWDInfo(aliasReply.vRefNum, vRefNum, parDirID, procID);
- Fail(err);
-
- { create the alias file tech note 241}
- HCreateResFile(aliasReply.vRefNum, parDirID, aliasReply.fName);
- err := ResError;
- if (err = dupFNErr) then
- begin
- err := HDelete(aliasReply.vRefNum, parDirID, aliasReply.fName);
- Fail(err);
- HCreateResFile(aliasReply.vRefNum, parDirID, aliasReply.fName);
- end
- else
- begin
- Fail(err);
- end;
-
- { open the alias file }
- aliasFileRefNum := HOpenResFile(aliasReply.vRefNum, parDirID, aliasReply.fName, fsWrPerm);
- if (aliasFileRefNum = -1) then
- Fail(aliasFileRefNum);
-
- { use the alias resource file }
- UseResFile(aliasFileRefNum);
- Fail(ResError);
-
- { add a resource for the alias of type 'alis' }
- AddResource(Handle(myAliasHandle), rAliasType, kAliasResourceID, aliasReply.fName);
- Fail(ResError);
-
- { write it out }
- WriteResource(Handle(myAliasHandle));
- Fail(ResError);
-
- { add icons here also if you wish but you don’t have to }
- { ICN# -16496 }
- { ics# -16496 }
- { SICN -16496 }
-
- { close the new alias }
- CloseResFile(aliasFileRefNum);
- Fail(ResError);
-
- { we need the creator of the original file }
- err := HGetFInfo(originalReply.vRefNum, spec.parID, originalReply.fName, fndrInfo);
- Fail(err);
-
- { make sure we set it up as an alias }
- fndrInfo.fdFlags := kIsAliasFlag;
-
- { set the info of the new file }
- err := HSetFInfo(aliasReply.vRefNum, parDirID, aliasReply.fName, fndrInfo);
- Fail(err);
-
- { restore our file }
- UseResFile(saveResFile);
- Fail(ResError);
- end;
-
- {____________________________________________________________}
- { this is our main }
- {____________________________________________________________}
- var
-
- err: OSErr;
- originalReply: SFReply;
- aliasReply: SFReply;
- spec: FSSpec;
-
- begin
-
- InitMacintosh;
- CheckEnvironment;
-
- GetFileToAlias(originalReply, spec);
- if (not originalReply.good) then
- ExitToShell;
-
- GetAliasInformation(originalReply.fName, aliasReply);
- if (not aliasReply.good) then
- ExitToShell;
-
- err := CreateAlias(originalReply, aliasReply, spec);
- if (err <> noErr) then
- ExitToShell;
-
- end.