Opening a file folder in finder

I use this code in my app to show where is located a sqlite database.

MessageBox("DB located at: "+ App.DBM.DBFilePath)
Var f As New FolderItem(App.DBM.DBFilePath)

How can I open a window in the Finder to show the file store in f?


Simply add f.Open (true)

1 Like

No, that opens the folderitem itself but not a Finder window. These days AppleScripts on Finder are discouraged. Instead NSWorkspace has a method to show the Finder window. There are declares available or use the MBS plugin.


Thanks @Beatrix_Willius I just found that in MBS Doc

 // Open Folder in Finder
 Var a As FolderItem = LaunchServicesOpenMBS(DB.DatabaseFile.Parent)

As @Valdemar_De_SOUSA said this code also works fine

// Open Folder in Finder


Here’s a hint for y’all, this is how to do it in a different language.

NSWorkspace.shared.activateFileViewerSelecting( [ url ] )

I really dislike the naming of these functions though…

System.GotoURL f.Parent.URLPath

where f is the folderitem of the file you want to display the folder

It does make sense, it activates the Finder and opens windows containing the URLs, and then highlights the URLs and makes sure that the customer can see them. [ url ] is an array, so you can pass in multiple URLs.

There’s also a declare you can use. I put this inside a method and pass the folderitem showFolder to the method.

If showFolder <> Nil And showFolder.Exists Then
  Declare Function objc_getClass Lib "Cocoa" ( name As CString ) As ptr
  Declare Function sharedWorkspace Lib "AppKit" Selector "sharedWorkspace" ( obj As ptr ) As ptr
  Declare Function selectFile Lib "AppKit" Selector "selectFile:inFileViewerRootedAtPath:" ( obj As ptr, fPath As CFStringRef, rootFullPath As CFStringRef ) As Boolean
  Dim workspace As ptr = sharedWorkspace( objc_getClass( "NSWorkspace" ) )
  Call selectFile( workspace, showFolder.Nativepath, "")
End If

If you have MBS, this is what I use (x-platform):

Public Sub RevealFile(f as FolderItem)

  If f is nil or not f.Exists then Return
  #If TargetMacOS Then
    dim b as boolean = NSWorkspaceMBS.selectFile (f)
  #Elseif TargetWindows
    dim TheFolder as new FolderItem
    TheFolder = f.Parent
    dim e as integer = WinOpenFolderAndSelectItemsMBS(TheFolder, array(f))
End Sub


It “can” make sense; as would other terminology do, possibly better.
First thing is to call this an URL, which had been primarily used for web until then. I can’t get accustomed to that for file system objects.
Then the capitalisation. Why on earth is the first letter lowercase, unlike the remaining words?

All I know about the use of URL for files, is its documented as being available from Mac OS X 10.0, and I would guess further with its history at NeXT.

There are published guidelines on naming conventions when using this language.

Follow case conventions. Names of types and protocols are UpperCamelCase. Everything else is lowerCamelCase.

Indeed. The kind of things that seemed more strange at the time of the switch from Mac OS 9 to Mac OS X and we’ve become “accustomed” since then, yet it still can be seen as “illogical”.

Yes, I know, thanks. Yet, I still find this ugly.