UI.openpanel returns no slashes

Hi community,
I’m new to ruby, so probably it’s something basic.

I’m trying to get a variable containing just a folder path via UI.openpanel.
There seems to be no option to select a folder, so I’m asking users to select any file in the folder, so that in the next step I can just remove filename from it (ugh).

imglocation = UI.openpanel("Choose any image from images folder", "/", "Image Files|*.jpg;*.png;||")

However, using this code returns the path stripped of slashes:
C:UsersMeDesktopimage.jpg

Could anyone give me a hint how to deal with it?

Perhaps you have not installed SketchUp properly ?
Close SketchUp.
When logged in as your normal User account…
Find its installer exe file’s icon - probably in your Downloads folder ?
Select it and right-click > context-menu > “Run as administrator”
When prompted, choose ‘Repair’.
Restart SketchUp and retest.

Installing any complex app, like SketchUp, in any other way causes weird and unexpected issues.
Do NOT double-click the installer’s exe icon to Run it.
Even if you have admin-powers you still need to install it properly !
Also do not install it whilst logged in with the separate ‘administrator’ account, and do NOT set the SketchUp.exe Properties to “always run as administrator” - this leads to new separate issues…

1 Like

Thanks @TIG, I found the bug.
It was in the beautiful Alexander Schreyer’s Ruby code editor, which I was using.
Running same code straight in Ruby Console works perfectly.
That’s a pity - I’ll report to him, and go looking for some other workflow…

FYI, for SketchUp 2015 and higher, the better method to ask the user for a path is …

imglocation = UI.select_directory(
  title: "Select Image Directory",
  directory: File.expand_path('~'), # User's HOME dir
  select_multiple: false
)

REF:

2 Likes

Perhaps Aerelius’ Ruby Console+ could be of interest? I use the native console myself (don’t really know why, just have got so used to it I guess) but this one looks and feels very much like a console, and probably doesn’t have that specific bug.

https://extensions.sketchup.com/en/content/ruby-console

1 Like

Thanks @DanRathbun, that looks way more elegant, I’ll give it a try.
Also, I see you can select multiple directories. Is it somehow possible to select multiple files as well?

Thanks @eneroth3, naturally, I continued working with Aerelius’ Ruby Console+ after this, - it’s quite good as well. Funny enough, that Console+ did return folder names, but with backslashes instead of forward ones, so I had to replace them with .gsub!. And after I did that to my code, it became usable on Schreyers’ Ruby code editor again!

That’s the Windows way. It is what is returned by the API method and not related to the console you use.

Maybe Schreyers console editor just doesn’t escape backslashes before printing them out.

1 Like

I guess you’re right about Schreyers’ Ruby code editor - it doesn’t escape before output,
because
UI.openpanel("Choose", "/", "*") removes the slashes,
but
UI.openpanel("Choose", "/", "*").gsub!(/\\/, '/') outputs a nice path.

The slashes are in the variable, though. This code shows the full path in an alert…

imglocation = UI.openpanel(“Choose any file from the folder”, “/”, “*”)
UI.messagebox imglocation

1 Like

Not currently with SketchUp’s UI.openpanel.

Once you have gotten the folder path via UI.select_directory, …
you can use core Ruby Dir class methods to get an array of files …

imglocation = UI.select_directory(
  title: "Select Image Directory",
  directory: File.expand_path('~'), # User's HOME dir
  select_multiple: false
)

if imglocation # test in case user cancelled folder pick
  choice = nil
  Dir.chdir(imglocation) {
    files = Dir["*.{jpg,jpeg,png}"]
    choice = UI.inputbox(
      ["Image"],
      [ files.first ],
      [ files.join('|') ],
      "Choose image ..."
    )
    if choice
      # do some task ...
    else
      # user cancelled inputbox
    end
  }
  # Previous dir is automatically reset after block, so
  # outside block, choice (if valid) needs it's path prefixed ...
  abs_img_path = File.join(imglocation,choice)
end

Reference:


You can get fancier if you’d like. You can display a checkbox list in a HtmlDialog window
(but need knowledge of HTML, JavaScript and CSS.)

1 Like

What a community! I appreciate this a lot.
I think I’ll dive into html dialogs anyway, for more options, and maybe I’ll find how to select multiple image files there.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.