[futurebasic] Re: [FB] Renaming a file using FSSpec

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

From: Robert Purves <listrp@...>
Date: Thu, 29 Dec 2011 19:18:46 +1300
Brian S wrote:

> why not go straight to CFURLRefs?


The demo below gives CFURLs a workout; it is more ambitious than the original request (uses a sheet dialog, and preserves the opened file's extension, if any).

Robert P.

'------------
include "ConsoleWindow"


local fn DoSomethingWithFile( openURL as CFURLRef )
'~'1
dim as CFStringRef         ext, path
dim as CFMutableStringRef  newPath
dim as CFURLRef            url1, url2, saveURL

print "File to be opened:"
fn ConsolePrintCFType( openURL )

print "Opening the file...
open "I", 1, @openURL
close 1

print "Pretending to process..."

ext = fn CFURLCopyPathExtension( openURL ) // NULL if no extension
url1 = fn CFURLCreateCopyDeletingPathExtension( _kCFAllocatorDefault, openURL )
path = fn CFURLGetString( url1 )
newPath = fn CFStringCreateMutableCopy( _kCFAllocatorDefault, 0, path )
CFStringAppend( newPath, @"(processed)" )
url2 = fn CFURLCreateWithString( _kCFAllocatorDefault, newPath, NULL )
if ( ext )
saveURL = fn CFURLCreateCopyAppendingPathExtension( _kCFAllocatorDefault, url2, ext )
CFRelease( ext )
else
saveURL = fn CFRetain( url2 )
end if

print "File to be saved:"
fn ConsolePrintCFType( saveURL )

print "Saving [empty file]..."
open "O", 1, @saveURL
close 1
print "Done"

CFRelease( url1 )
CFRelease( url2 )
CFRelease( newPath )
CFRelease( saveURL )
end fn


local fn ChooseFileHandler( reply as ^NavReplyRecord, userData as pointer )
'~'1
dim as CFURLRef ref

NavDialog_CopyItemCFURLRef( #reply, 1, @ref )
fn DoSomethingWithFile( ref )
CFRelease( ref )
end fn


local fn ChooseFileSheet
'~'1
NavDialog( _kNavDialogChooseFile +_kNavDialogSheet, "", "", @fn ChooseFileHandler, 0 ) 
end fn


local fn HandleMenu
'~'1
select switch menu( 1 )
case 1 : fn ChooseFileSheet
end select
end fn


menu 1, 0, 1, "File"
menu 1, 1, 1, "Open/O"
on menu fn HandleMenu
print "Choose Open from the File menu."
'------------