[futurebasic] Re: [FB] FSSpecs vs FSRefs

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : January 2008 : Group Archive : Group : All Groups

From: Joe Lertola <joefb@...>
Date: Sun, 27 Jan 2008 01:39:36 -0500
Wow, I learned a lot about using FSRefs today. Until now I only used  
FSRefs when example code that I was cribbing used them. Now I know how  
to:

use Files$ and Open with FSrefs
scan all files in a folder
get FSref of the documents folder
get FSref of the application's folder
get the file name from an FSref
get a CFURLRef for the applications folder
launch a web browser and open a url

Thanks Brian and Ken.

On Jan 26, 2008, at 3:02 PM, Brian Stevens wrote:

> "Let user select folder". This is just a matter of picking the FSRef  
> version of a call when call Navigation Services. If you are asking  
> about the corresponding FSRef support in files$ for _FSSpecFolder,  
> it doesn't exist yet (AFAIK). Somebody will have to write it or you  
> will have to cal Nav Services directly (which is what files$ does)

I decided to see if I could make a folder selection function with  
Navigation Services as you suggest. Here is what I came up with. It  
seems to work in both FB and FBtoC.

include "Tlbx Navigation.incl"
include "Tlbx MoreFilesX.incl"
_typeFSRef = _"fsrf"

'~';
local mode
'~FullNameFromFSRef$
local fn FullNameFromFSRef$( inRef as ^FSRef )
dim as str255 name
dim as HFSUniStr255 uniStr255
dim as CFStringRef cfStr
dim as boolean ignore
'~'<
name[0] = 0 // null string to return if error
long if ( fn FSGetCatalogInfo( #inRef, _kFSCatInfoNone, #0,  
@uniStr255, #0, #0 ) == _noErr )// get name as HFSUniStr255
cfStr = fn CFStringCreateWithCharacters( 0, uniStr255.unicode[0],  
uniStr255.length )// HFSUniStr255 -> CFString
long if ( cfStr )
ignore = fn CFStringGetPascalString( cfStr, @name, sizeof( name ),  
_kCFStringEncodingMacRoman )// CFString -> pascal string
CFRelease( cfStr ) // dump
end if
end if
end fn = name
'~'5
local fn chooseFolder( refPtr as ^FSRef ) as OSErr
dim as NavDialogCreationOptions myNavOptions
dim as NavDialogRef @myNavDialogue
dim as NavReplyRecord myNavResults
dim as OSStatus err
dim as long @myDummyClientData, ItemCount
dim as long theSize
dim as FSRef myRef
myDummyClientData = 0

err = fn NavGetDefaultDialogCreationOptions( @myNavOptions )
err = fn NavCreateChooseFolderDialog( @myNavOptions, 0, 0,  
@myDummyClientData, @myNavDialogue )
err = fn NavDialogRun ( myNavDialogue )
err = fn NavDialogGetReply ( myNavDialogue, @myNavResults )
long if myNavResults.validRecord <> _false'nav not canceled
err = fn AECountItems( myNavResults.selection, ItemCount )
long if ItemCount =1

err = fn AEGetNthPtr( @myNavResults.selection, ItemCount, _typeFSRef,  
#0, #0, @myRef, sizeof( FSRef ), #0 )

long if err = _noErr
blockmove @myRef, refPtr, sizeof( FSRef )
end  if

end if

xelse

end if

err = fn NavDisposeReply (myNavResults)
fn NavDialogDispose ( myNavDialogue )

end fn = err
'~'5
window 1
dim as OSStatus gErr
dim as str255 gName
dim as FSRef gRef

gErr = fn chooseFolder( @gRef )
long if gErr = _noErr
gName = fn FullNameFromFSRef$( @gRef )
print "Folder name is: " + gName
xelse
print "Error = " + str$( gErr )
end if

do
HandleEvents
until gFBQuit