[futurebasic] Re: [FB] Open Folder Alias

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

From: Robert Purves <listrp@...>
Date: Sun, 7 Aug 2011 18:54:51 +1200
Steve wrote:

> When my app is used in a multiuser environment, I want to place an alias of the shared database folder on the server and resolve the FSRef for that folder to use for my data files.
> By default, it looks in Users>Shared for a specific folder by name and if it exists, uses that folder.  If it doesn't it will prompt to locate the folder to use.
> How can I modify and use the example Alias Demo in the FB 5 Examples>Files folder to resolve an FSRef for a aliased folder  vs. a file?

The first time you run the demo below it prompts a folder choice and saves an alias of the chosen folder, in /Users/Shared/Steve's Data Folder Locator. Subsequent runs do not prompt. That must be close to your requirements.

Robert P.


'-------------
include "Util_FileDirectory.incl"
include "Tlbx Aliases.incl"


_aliasFileName$ = "Steve's Data Folder Locator"


local fn GetAliasFileParentRef( outRef as ^FSRef ) as OSStatus
'~'1
//  /Users/Shared
end fn = fn FD_SpecialDirectoryGetFSRef( _kOnAppropriateDisk, _kSharedUserDataFolderType, #outRef )


local fn RetrieveLocationFromAliasFile( outRef as ^FSRef ) as OSStatus
'~'1
dim as FSRef             parentRef, aliasRef
dim as Boolean           wasChanged
dim as AliasHandle       aliasH
dim as UInt32            fileSize
dim as OSStatus          err  

// Find the alias file's parent folder.
err = fn GetAliasFileParentRef( @parentRef )
if ( err ) then exit fn
// Find the alias file.
err = fn FD_PathGetFSRef( fn CFSTR( _aliasFileName$ ), @parentRef, @aliasRef )
if ( err ) then exit fn
// The alias file exists. Attempt to open it.
on error end : error = _noErr
open "I", 111, @aliasRef
err = error : error = _noErr
if ( err ) then exit fn
// Read file contents into a handle.
fileSize = lof( 111, 1 )
aliasH = fn NewHandle( fileSize )
read file #111, [aliasH], fileSize
// Resolve the file object to a FSRef.
err = fn FSResolveAlias( 0, aliasH, #outRef, @wasChanged )
DisposeHandle( (Handle)aliasH )
end fn = err


local fn StashLocationAsAliasFile( dataFolderRef as ^FSRef ) as OSStatus
'~'1
dim as FSRef             parentRef
dim as Handle            h
dim as OSStatus          err  

// Find the alias file's parent folder.
err = fn GetAliasFileParentRef( @parentRef ) 
if ( err ) then exit fn
err = fn FSNewAlias( 0, #dataFolderRef, @h )
if ( err ) then exit fn
// Attempt to open the alias file, erasing preexisting file contents.
def open "alisMACS"
on error end : error = _noErr
open "O", 111, _aliasFileName$, @parentRef
err = error : error = _noErr
long if ( err == _noErr )
// Write alias data to file.
write file #111, [h], fn GetHandleSize( h )
close 111
end if
DisposeHandle( h )
end fn = err



include "ConsoleWindow"

dim as OSStatus   err  
dim as FSRef      ref

err = fn RetrieveLocationFromAliasFile( @ref )
long if ( err )
long if ( files$( _FSRefFolder, "Locate Steve's Data folder", "", @ref ) )
err = fn StashLocationAsAliasFile( @ref )
long if ( err )
print "Could not save location"
xelse
print "Location saved OK"
end if
end if
xelse
print "Location found and resolved"
end if
'-------------