"Show in Folder" on Mac

Hi all!

I’m writing a method that opens a directory and select a specific file. Typically you can do this from clicking “Show in Folder” or “Open Containing Folder” inside various applications. I have got it to work on Windows but not on Mac yet. Anyone who knows a way to achieve this on mac?

An easy solution would be to simply use the phrase “Open Containing Folder” and then the file doesn’t necessarily have to be selected but it looks neater if it is :stuck_out_tongue: .

def self.show_in_folder(path)

  if Sketchup.platform == :platform_win
    system "explorer.exe /select,\"#{path.tr("/", "\\")}\""
  else
    # TODO: If possible select the file on Mac.
    UI.openURL File.dirname(path)
  end

  nil

end

to open a folder in Finder on a mac

    `open -R "#{File.dirname(path)}"`

“UI.openURL File.dirname(path)” already does that. The question is how to open the folder with the file selected.

drop the File.dirname and the actual file will be highlighted in Finder…

UI.openURL(path) will spawn a ‘choose application to open’ message, but open -R "#{path}" won’t…

john

1 Like

Thanks!

I don’t have access to any Mac myself so I cannoy try it but based on the documentation I found it seems to work.

This is the code I’ll be using:
system “open -R “#{path}””

system("open -R #{path}")

looks cleaner and also works…

john

Shouldn’t there be quoutes around thye path in case in contains a space?

your correct, I normally use back ticks or %x [so I can monitor/use the return] …

the alternate to escaping the quotes is to use inspect…

to me it just looks neater…

 system("open -R #{path.inspect}")

john

To continue with some related notes: I found that some paths didn’t work on Windows. It turned out to be a character encoding problem. The following code has worked for the tests I’ve made so far:

system(“explorer.exe /select,”#{path.tr("/", “\”).encode(“ISO-8859-1”)}"")

Are there any similar problems for Mac or does it simply properly support utf8 all the way?

Without claiming that there are never any issues, Macs have been UTF8 for some time so concerns about encoding usually happen only with texts imported from a non-UTF8 platform.

1 Like