I want to replace OpenFileRequester with NSOPenPanel. Any API gurus here

Code: Select all
void function (const char* pbstring)
{
NSString string = [NSString stringWithUTF8String: string];
}
It's easier to usePolo wrote:How would you convert a PB string to a NSString?
Code: Select all
void mOpenFileRequester()
{
NSString *title = "Requester title";
NSString *directory = "";
NSString *file = "";
NSString *extensions = "";
currentwin=[NSApp keyWindow];
if ( !currentwin ) [NSApp activateIgnoringOtherApps:YES];
NSOpenPanel *panel=[NSOpenPanel openPanel];
[panel setTitle:title];
if ( [panel runModalForDirectory:directory file:file types:extensions]==NSFileHandlingPanelOKButton )
{
NSString *fileselected=[[panel filename] UTF8String];
}
if ( currentwin ) [currentwin makeKeyWindow];
}
I'm not sure what your goal is.Wolfram wrote:can some give me a easy example of this code. I'm a beginner and have no experience with C.
PB help says:Wolfram wrote:can some give me a easy example of this code. I'm a beginner and have no experience with C.
So here a version with patterns, based on Polo's Objective-C code:Note: The 'Pattern$' and 'PatternPosition' parameters have no effect on Mac OSX (the requester always displays all files).
Code: Select all
Macro CreateCocoaObject(objectName,withMethod="alloc")
CocoaMessage(0,0,objectName+" "+withMethod)
EndMacro
#showHiddenFiles = 1
#allowMultipleSelection = 1 << 1
#returnAbsoluteNames = 1 << 2
#NSFileHandlingPanelOKButton = 1
#NSFileHandlingPanelCancelButton = 0
Procedure OpenFileRequester_(List out.s(), title.s, defaultdirectory.s="", pattern.s="", message.s="", flags=0)
ClearList(out())
NSApp = CreateCocoaObject("NSApplication")
If NSApp
currentwin = CocoaMessage(0,NSApp,"keyWindow")
If Not currentwin
CocoaMessage(0,NSApp,"activateIgnoringOtherApps:",#YES)
EndIf
panel = CreateCocoaObject("NSOpenPanel","openPanel")
If panel
;
; set title
;
CocoaMessage(0,panel,"setTitle:$",@title)
;
; set optional message text
;
If message
CocoaMessage(0,panel,"setMessage:$",@message)
EndIf
;
; set allowed patterns, separated by '|'
;
If pattern <> ""
i = 1
nextPattern.s = StringField(pattern,i,"|")
fileTypesArray = CocoaMessage(0,0,"NSArray arrayWithObject:$", @nextPattern)
Repeat
i + 1
nextPattern = StringField(pattern,i,"|")
If nextPattern
fileTypesArray = CocoaMessage(0,fileTypesArray, "arrayByAddingObject:$", @nextPattern)
EndIf
Until nextPattern = ""
CocoaMessage(0,panel,"setAllowedFileTypes:",fileTypesArray)
EndIf
;
; show hidden files?
;
If flags & #showHiddenFiles
CocoaMessage(0,panel,"setShowsHiddenFiles:",#YES)
EndIf
;
; allow multiple selection?
;
If flags & #allowMultipleSelection
CocoaMessage(0,panel,"setAllowsMultipleSelection:",#YES)
EndIf
;
; set default directory
;
url = CocoaMessage(0,0,"NSURL URLWithString:$",@defaultdirectory)
CocoaMessage(0,panel,"setDirectoryURL:",url)
;
; run
;
If CocoaMessage(0,panel,"runModal") = #NSFileHandlingPanelOKButton
;
; get results
;
results = CocoaMessage(0,panel,"URLs")
If results
count = CocoaMessage(0, results, "count")
If count
For i = 0 To count-1
NSURL = CocoaMessage(0,results,"objectAtIndex:",i)
If NSURL
If Not flags & #returnAbsoluteNames
;
; return file names
;
string = CocoaMessage(0,NSURL,"fileSystemRepresentation")
If string
fileName.s = PeekS(string,-1,#PB_UTF8)
If AddElement(out())
out() = fileName
EndIf
EndIf
Else
;
; return absolute names
;
string = CocoaMessage(0,NSURL,"absoluteString")
If string
utf8 = CocoaMessage(0,string,"UTF8String")
If utf8
fileName.s = PeekS(utf8,-1,#PB_UTF8)
If AddElement(out())
out() = fileName
EndIf
EndIf
EndIf
EndIf
EndIf
Next i
EndIf
EndIf
EndIf
EndIf
EndIf
If currentwin
CocoaMessage(0,currentwin,"makeKeyWindow")
EndIf
ProcedureReturn count
EndProcedure
NewList files.s()
num = OpenFileRequester_( files(),
"Please choose file(s) to load",
"/users/",
"pb|pbi|prefs|rtf|txt",
"Optional message:"+#CR$+"Please open a file for loading."+#CR$+"Second message line",
#allowMultipleSelection |
#showHiddenFiles )
Debug num
ForEach files()
Debug files()
Next
The fileSystemRepresentation method requires OS X 10.9 (Mavericks).Wolfram wrote:Object dose not respond to method "fileSystemRepresentation"
Does it work for you with flag '#returnAbsoluteNames'?Wolfram wrote:but I get an Error if I try to load a File
Object dose not respond to method "fileSystemRepresentation"
Code: Select all
#showHiddenFiles = 1 << 0
#allowMultipleSelection = 1 << 1
Procedure.s OpenFileRequester_(title.s, defaultdirectory.s = "", pattern.s = "", message.s = "", flags = 0)
Protected NSPanel, NSArray, NSString
NSPanel = CocoaMessage(0, 0, "NSOpenPanel openPanel")
NSArray = CocoaMessage(0, CocoaMessage(0, 0, "NSString stringWithString:$", @pattern), "componentsSeparatedByString:$", @"|")
CocoaMessage(0, NSPanel, "setTitle:$", @title)
CocoaMessage(0, NSPanel, "setDirectoryURL:", CocoaMessage(0, 0, "NSURL URLWithString:$", @defaultdirectory))
CocoaMessage(0, NSPanel, "setAllowedFileTypes:", NSArray)
CocoaMessage(0, NSPanel, "setMessage:$", @message)
If flags & #showHiddenFiles
CocoaMessage(0, NSPanel, "setShowsHiddenFiles:", #YES)
EndIf
If flags & #allowMultipleSelection
CocoaMessage(0, NSPanel, "setAllowsMultipleSelection:", #YES)
EndIf
If CocoaMessage(0, NSPanel, "runModal")
NSString = CocoaMessage(0, CocoaMessage(0, NSPanel, "URLs"), "componentsJoinedByString:$", @"|")
ProcedureReturn PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8)
Else
ProcedureReturn ""
EndIf
EndProcedure
Debug OpenFileRequester_("Please choose file(s) to load",
"/users/",
"pb|pbi|prefs|rtf|txt",
"Optional message:"+#CR$+"Please open a file for loading."+#CR$+"Second message line",
#allowMultipleSelection |
#showHiddenFiles )