In many apps, there’s a button that you can stuff in a fully-qualified path and provide a way to launch into File Explorer (Windows) or Finder (OS X). I bet there’s a simple way to do that–any ideas? Thank you!!!
Ken
In many apps, there’s a button that you can stuff in a fully-qualified path and provide a way to launch into File Explorer (Windows) or Finder (OS X). I bet there’s a simple way to do that–any ideas? Thank you!!!
Ken
Straight from the Xojo Developer’s Library
To reveal a File or Folder, and on most platforms, select the item:
(Place the method into a module for it to work)
Usage: FolderItem.Reveal
Sub Reveal(Extends f as FolderItem)
Dim shell1 as new Shell
Dim cmd as string
if f <> nil then
if f.Exists then
#IF TargetWin32 then
cmd = "explorer.exe /select,"+ chr(34)+ f.AbsolutePath+ chr(34)
shell1.execute(cmd)
#elseIf TargetMacOS then
Shell1.Mode = 1
dim OSXVersion as string
cmd = "sw_vers -productVersion"
shell1.execute(cmd)
OSXVersion = shell1.ReadAll
if OSXVersion.NthField(".",2).val >= 6 then
cmd = "open -R "+ f.ShellPath
else
cmd = "osascript -e 'Tell application ""Finder"" to reveal """ + f.AbsolutePath + """' -e 'Tell application ""Finder"" to activate'"
end if
shell1.execute(cmd)
#elseif TargetLinux
'NOTE: Nautilus supports this since 2011-04-08, but I didn't want to put an additional check
'So in case of doubt uncomment the second line and have gnome-open open just the directory.
cmd = "if [ $(which -s nautilus) -eq 1 ]; then nautilus "+f.URLPath+"; else gnome-open " + f.Parent.ShellPath+ chr(34)+"; fi"
'cmd = "gnome-open "+f.parent.ShellPath
shell1.execute(cmd)
#ENDIF
end if
end if
End Sub
Simplified way to reveal a folder:
//Let's reveal the desktop folder
Dim myFolder as FolderItem
myFolder = SpecialFolder.Desktop
myFolder.Launch
You can use an AppleScript. See http://apple.stackexchange.com/questions/26535/how-do-i-use-applescript-to-reveal-a-file-in-finder-from-its-posix-path . As far as I remember there were some problems with this on Yosemite. Which is the reason why I changed to a plugin solution. See https://www.monkeybreadsoftware.net/cocoa-nsworkspacembs-method2.shtml#1 .
OSX
Sub ShowFileInFinder(f as FolderItem)
declare function objc_getClass lib "libobjc.dylib" ( 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" ) )
if selectFile( workspace, f.NativePath, "") then
return
else
MsgBox "Error"
end if
End Sub
[quote=204853:@Howard Whitaker]In many apps, there’s a button that you can stuff in a fully-qualified path and provide a way to launch into File Explorer (Windows) or Finder (OS X). I bet there’s a simple way to do that–any ideas? Thank you!!!
Ken[/quote]
FolderItem.Launch
http://documentation.xojo.com/index.php/FolderItem.Launch
Cross platform :
dim f as folderitem = GetFolderItem(TextField1.Text, FolderItem.PathTypeShell)
If f <> nil and f.Exists then
if f.Directory then
f.launch
else
f.parent.launch
end if
end if
If the path points to a folder, it is opened in a new window. If it points to a file, it opens the containing folder.
Code is like life. The less complicated, the less worries. Keep it simple
but it does not select the file.
I do not read the OP as a GetOpenFolderItem. Instead, it seems to me Howard wants to input a path and have it open in a window. And he clearly said he wanted OS X AND Windows. I did nothing else.
[quote=204907:@Michel Bujardet]FolderItem.Launch
http://documentation.xojo.com/index.php/FolderItem.Launch[/quote]
Yes, sometimes it’s good to read the Xojo Documentation
Thanks, guys! Great feedback.
Michael’s elegant solution works greeeeaaattttttt! I tried it on OS X and Windows.
just for future reference; anything that controls the “Finder” or “System Events” via Apple Script will be barred from the App Store.
I use this method. Just pass a FolderItem.
It highlights the file in Finder. Cross platform and it should be sandbox friendly.
[code]Sub mShowFile(f as FolderItem)
#If TargetWin32 Then
Declare Function ShellExecuteW Lib “shell32”(hwnd As Integer, lpOperation As WString, lpFile As WString, lpParameters As WString, lpDirectory As Integer, nShowCmnd As Integer) As Integer
Dim parm As String
Dim error As Integer
parm = “/select, “”” + f.AbsolutePath + “”""
error = ShellExecuteW(Window(0).WinHWND, “Open”, “explorer”, param, 0, 1)
#ElseIf TargetMacOS Then
Declare Function selectFile Lib “AppKit” selector “selectFile:inFileViewerRootedAtPath:”(obj As Ptr, fPath As CFStringRef, rootFullPath As CFStringRef) As Boolean
Declare Function objc_getClass Lib “libobjc.dylib”(name As CString) As Ptr
Declare Function sharedWorkspace Lib “AppKit” selector “sharedWorkspace”(obj As Ptr) As Ptr
Dim swrkspace As Ptr = sharedWorkspace(objc_getClass(“NSWorkspace”))
Call selectFile(swrkspace, f.NativePath, “”)
#ElseIf TargetLinux
Dim sh As New Shell
Dim launchers() As String = Array(“xdg-open”, “gnome-open”, “exo-open”, “kde-open”)
For Each launcher As String In launchers
sh.Execute("which " + launcher)
If Left(sh.Result, 1) = “/” Then
sh.Execute(launcher + " " + f.Parent.ShellPath)
Exit
End If
Next
#EndIf
End Sub
[/code]
Thanks for that Marco - works great on Mac!