[futurebasic] Re: [FB] Making an alias

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2011 : Group Archive : Group : All Groups

From: Robert Covington <artlythere@...>
Date: Sun, 2 Oct 2011 03:14:50 -0400
I am wondering...how does one save multiple aliases in a single file when there is not (apparently) an ID as there was with the resource based aliases of yore?

My old alias code is all busted up in 10.6.8 and I don't know how to repair it.

If I write something from FSNewAlias to the same file, how can I give ID's and count them? (for OpenRecent, etc)

Or is there an Open Recent widget/api these days?

rc

On Feb 12, 2010, at 4:28 PM, Brian S wrote:

> 
> 
>> On Feb 12, 2010, at 8:29 AM, Jay Reeve wrote:
>> 
>>> I can't find any examples showing how I might do that. Is there one?
> 
> Here is a demo. Code could be cleaned up but it has the basics.
> 
> 
> '-----------
> include "Util_FileDirectory.incl"
> include "Tlbx Aliases.incl"
> 
> toolbox fn FSNewAlias( const FSRef * fromFile, const FSRef * target, Handle * inAlias ) = OSErr
> 
> _aliasFile$ = "aliasFile"
> _dataFile$ = "dataFile"
> 
> #if ndef _DEFINEDINCARBON
> begin record AliasRecord
> dim userType as OSType// appl stored type like creator type
> dim aliasSize as unsigned short // alias record size in bytes, for appl usage
> end record
> #endif
> 
> 
> local fn GetApplicationFolderRef( ref as ^FSRef )
> '~'1
> end fn = fn FD_ApplicationDirectoryGetFSRef( #ref )
> 
> 
> local fn GetUserDocumentsFolderRef( ref as ^FSRef )
> '~'1
> end fn = fn FD_SpecialDirectoryGetFSRef( _kUserDomain, _kDocumentsFolderType, #ref )
> 
> 
> 
> // Write data file to application folder and return the FSRef to the data file
> local fn WriteDataFile as FSRef
> '~'1
> dim as FSRef             appFolderRef, dataFileRef
> dim as OSStatus          err
> dim as Str255            s     : s = "abc test data"
> 
> err = fn GetApplicationFolderRef ( @appFolderRef )
> open "O", 1, _dataFile$, @appFolderRef
> print #1, s
> close 1
> err = fn FD_PathGetFSRef( fn CFSTR( _dataFile$ ), @appFolderRef, @dataFileRef )
> 
> end fn = dataFileRef
> 
> // Create an alias to data file and write alias to ~user/Documents folder
> local fn CreateAliasAndSave( dataFileRef as ^FSRef )
> '~'1
> dim as FSRef             userDocumentFolderRef
> dim as handle            h
> 
> long if ( fn FSNewAlias( #0, #dataFileRef, @h ) == _noErr )
> long if ( fn GetUserDocumentsFolderRef( @userDocumentFolderRef ) == _noErr )
> open "O",1,_aliasFile$, @userDocumentFolderRef
> write file #1,[ h ], fn GetHandleSize( h )
> close 1
> DisposeHandle( h )
> end if
> end if
> end fn
> 
> local fn Retrieve
> '~'1
> dim as FSRef             dataFileRef, userDocumentFolderRef, aliasFileRef
> dim as ^^AliasRecord     aliasH
> dim as UInt32            fileSize
> dim as Boolean           wasChanged
> dim as OSStatus          err
> dim as Str255            s
> 
> // Retrieve the alias from ~user/Documents folder
> long if ( fn GetUserDocumentsFolderRef( @userDocumentFolderRef ) == _noErr )
> err = fn FD_PathGetFSRef( fn CFSTR( _aliasFile$ ), @userDocumentFolderRef, @aliasFileRef )
> 
> // Open the aliasFile, create handle size of alias data, read alias data into the handle
> open "I",1,@aliasFileRef
> fileSize = lof( 1,1 )
> aliasH = fn NewHandle( fileSize )
> read file #1,[ aliasH ], fileSize
> close 1
> 
> // Use the handle with FSResolveAlias to retrieve the FSRef of the data file
> err = fn FSResolveAlias( #0, aliasH, @dataFileRef, @wasChanged )
> fn DisposeH ( aliasH )
> 
> // Use the FSRef provided by FSResolveAlias to open the file and print contents
> open "I",2,@dataFileRef
> input #2, s
> close 2
> print s
> end if
> end fn
> 
> // main
> dim as FSRef     myDataFile
> 
> 
> myDataFile = fn WriteDataFile
> fn CreateAliasAndSave( @myDataFile )
> fn Retrieve
> 
> RunApplicationEventLoop()
> '--------------------------
> 
> 
> Brian S
> 
> --
> To unsubscribe, send ANY message to: futurebasic-unsubscribe@...
>