"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

Hi, I’m trying to learn how to make the code in Ruby to open a specific local folder from where I can select components to download ino my model (and also navigate to sub folders).

I tried the following code in the Ruby editor while the file was open but it’s not working. Are you able to advise on this or let me know if it’s a complicated thing to do?

# Default code, use or delete...
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection

path = C:\Users\Nick\Dropbox\Sketchup\Components Backup 220604\Components

def self.show_in_folder(path)

end

Thanks!

OFF-TOPIC

  1. In Ruby you must enclose strings in quotes. This is called delimiting. The quote characters are the delimiters.

  2. There are two kinds of strings. Single quoted ('text' or %q[text])
    and double quoted ("text" or %Q[text]). When using the % form, you can choose your own desired delimiters [ ], ( ), { } or some other character.

  3. Double quoted strings are interpolated and checked for embedded escape sequences.
    Escape sequences begin with the backslash character (\). Because of this, you cannot use backslash characters in doubled quoted path strings unless you escape them like "C:\\users\\BobsUncle"

  4. You should get in the habit of using network paths with forward slashes: "C:/users/BobsUncle"
    Most Ruby path string methods use and return path like this.

  5. SketchUp can have latency issues when dealing with network paths. It is recommended to copy files to the local disk and access then from there (if possible.)

See the Ruby primer on literal expressions: File: literals.rdoc [Ruby 2.7.2]

So …

module SomeAuthor::SomePlugin

  def self.get_component
    path = File.join(
      ENV["HOME"],
      "Dropbox/Sketchup/Components Backup 220604/Components"
    )
    filepath = self.show_in_folder(path)
    return false unless filepath
    comp_def = Sketchup.active_model.definitions.load(filepath)
  end

  def self.show_in_folder(path)
    chosen = UI.openpanel("Choose Component File", path, "SketchUp Models|*.skp||")
    # returns nil if dialog was cancelled
  end

  # ... other code ...

end

REF:

Note that openpanel ignores the path argument and instead follows the last used path for the app from the OS. The user must navigate to the desired folder!

Perhaps on Mac, but not on Windows. If a valid path is given the browser opens there.

But if a wildcard "*.skp" is given as the path argument and the third argument omitted, then the system will open the browser in the last successful path chosen for the “skp” filetype (regardless of application, ie, it’s from the user’s MRU history.) If there is no “Most Recently Used” path for the filetype, then the file browser will likely open in the user’s profile "Documents" folder.

Note that all of this is not part of the API contract. It’s just platform specific quirky behavior.

1 Like

I have got this to work successfully. in so much as the directory box appears when I click the icon on my custom toolbar.
However when I click on a file (which are all saved .skp components) they don’t open in my current model, instead a new SKP file is opened each time.
Is there a simple bit of code which makes the selected files open in the current model?

Depending on how you want it to happen, either model#import or DefinitionList#import might work.

1 Like