NSOpenPanel howto

Please share your functions/code-snips

NSOpenPanel howto

Postby udo.killermann » Sat Jun 02, 2012 9:26 am

Hello Board,

Today the explanation is part of the source code. Give it a try and hopefully you understand how to work with NSOpenPanel afterwards:
Code: Select all

' This information is brought to you by U. Killermann
'
' NSOpenPanel is a built in object of Objective basic, which means it exists as a global object
' exactly once. You cannot instantiate it multiple times.
' It's properties can be set before you invoke its Run() method and read after the user has made
' his selection.
' NSOpenPanel shows the standard file open dialog of OSX.
'
' Additionally there exist two functions that come handy for handling the results of NSOpenPanel.
' These are FileName(), FilePath() and FileExtension(). Suppose you have a file which has the full
' POSIX path "/Users/Udo/Documents/empty.rtf". FileName() returns "empty.rtf" and FilePath() returns
' "/User/Udo/Documents". Please be aware that NSOpenPanel.FileName() returns the full POSIX
' path and not (as one might expect) only the filename. So you have to process it with the
' former functions to split it up into filename and path. To get the file extension
' - which would be "rtf" - you use the function FileExtension().
'
' NSOpenPanel.Directory can be used to specify the initial directory which is used as the
' starting point for directory traversal. It will not return the directory of the selected
' file. Again you need to handle this situation by using the above mentioned functions.
'
' BTW: instead of using the .FileName() function to get the POSIX name you can use
' .URL() to get a valid URL to access the chosen file.
'
' This subroutine takes a string and assumes this to be the POSIX filename of a file
' it uses the global functions to get the information.
'
Sub DisectFileName(myFileName As String)
  Alert("Full POSIX name:", myFileName)
  Alert("File name:", FileName(myFileName))
  Alert("File path:", FilePath(myFileName))
  Alert("File extension:", FileExtension(myFileName))
End Sub

Event AwakeFromNib()

  Dim FileArray As Array
  Dim Counter As Integer
  Dim POSIXName As String
 

' ~ is a POSIX short hand notation for the user's home directory
  NSOpenPanel.Directory = "~/Downloads"
 
' we allow the user only to select one file in this iteration 
  NSOpenPanel.AllowsMultipleSelection = No
 
' show the Panel!
  NSOpenPanel.Run()
 
' if the user presses [Cancel] the FileName property is an empty string ("")
' you need to check for this in your own code and handle the situation properly

  If (Compare(NSOpenPanel.FileName(), "") = 0) Then
     Alert("User canceled selection!")
 End If
 
' the user was restricted to select no more than one file so we don't need to
' process FileNames() but are going with FileName()
'
    DisectFileName(NSOpenPanel.FileName())

' no we allow the user to select several files in this iteration 
  NSOpenPanel.AllowsMultipleSelection = Yes
 
' show the Panel!
  NSOpenPanel.Run()
 
' if the user presses [Cancel] the FileName property is an empty string ("")
' you need to check for this in your own code and handle the situation properly

  If (Compare(NSOpenPanel.FileName(), "") = 0) Then
     Alert("User canceled selection!")
 End If

' now we need to process the array FileNames() cause there probably is more than one file
' selected by the user

  FileArray = NSOpenPanel.FileNames()
 
' to analyze the results we can use the array methods

  Alert("You selected the following # of files:",FileArray.Count())

' don't forget: arrays start at 0 and end at Count() - 1 - they are zero based!

  For Counter = 0 To FileArray.Count() - 1
    ' POSIXName=String(FileArray.Object(Counter))
    DisectFileName(FileArray.Object(Counter))
  Next
 
' instead of for...next we can use the foreach loop as well - but this is reserved
' for the next Objective Basic Release, it still has to be implemented
   
End Event


Enjoy your day
Udo
--------------
MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
udo.killermann
 
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany

Return to Code examples

Who is online

Users browsing this forum: No registered users and 1 guest