sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Creation of folder from Ruby on Mac

    Scheduled Pinned Locked Moved Developers' Forum
    31 Posts 5 Posters 1.1k Views 5 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • fredo6F Offline
      fredo6
      last edited by

      Dan,

      Yes, I meant the ENV key / values

      By the way it would be good this is documented in the API docs

      Fredo

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        @fredo6 said:

        By the way it would be good this is documented in the API docs

        It does not belong there as it is standard Ruby.

        Let's just get the API docs up to date with true API edits first, then worry about extras.

        (Sending you a PM.)

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          If you want to create a folder that you know you can write into without security problems then use the User's Temp folder.
          It has a different Key between PC and MAC.
          But it's relatively easy to find that.

          	IS_MAC=(RUBY_PLATFORM.downcase =~ /darwin/) != nil
          	###
          	home=File.expand_path(ENV["HOME"] || ENV["HOMEPATH"] || ENV["HOMEDRIVE"])
          	desktop=File.join(home, "Desktop")
          	###
          	[	ENV['TEMP'], 
          		ENV['TMP'], 
          		ENV['TMPDIR'], ### SHOULD be found by here, but in case have some fall backs !
          		ENV['USERPROFILE'], 
          		'C;/Temp', 
          		'/tmp', 
          		home, 
          		desktop, 
          		Dir.pwd
          	].each{|d|
          		if d && File.directory?(d) && File.writable?(d)
          			temp= File.expand_path(d)
          			break
          		end
              }
          	###
          	tempdir	= File.join(temp, 'Fredo6')
          	begin ### this allows for false File.exist? failure with non-ASCII
          		Dir.mkdir(tempdir)
          	rescue
          		###
          	end
          

          You now have a reference 'tempdir' - your own subfolder within the user's temp folder.
          On PC:
          C:/Users/Username/AppData/Local/Temp/Fredo6
          On MAC:
          /var/folders/rp/b9k42l5x7xngx_8tckgs0zdr0000gn/T/Fredo6
          This temporary folder will get emptied by some system clean ups.
          If you want a 'permanent' subfolder put in higher up in the user's temp folder tree:

          ###
          	permdir	= File.join(File.dirname(temp), 'Fredo6')
          	begin ### this allows for false File.exist? failure with non-ASCII
          		Dir.mkdir(permdir)
          	rescue
          		###
          	end
          

          You now have a reference 'permdir' - your own subfolder within the user's temp folder's container.
          On PC:
          C:/Users/Username/AppData/Local/Fredo6
          On MAC:
          /var/folders/rp/b9k42l5x7xngx_8tckgs0zdr0000gn/Fredo6

          If you want to find the default folder used by Sketchup to install Plugins from RBZ archives, then
          plugins = Sketchup.find_support_file('plugins')
          will return the full path on PC and MAC.
          To see if it's writable by the current user then use:
          File.writable?(plugins)
          Which returns true or false.
          To get its current mode use:
          File.stat(plugins).mode.to_s.split("")[-3..-1].join("").to_i
          e.g. 654

          = 777 would be good!
          File.chmod(0777, plugins) will try to reset the permissions IF the current user has sufficient security privileges...

          TIG

          1 Reply Last reply Reply Quote 0
          • jeff hammondJ Offline
            jeff hammond
            last edited by

            @tig said:

            If you want to create a folder that you know you can write into without security problems then use the User's Temp folder.

            i don't know if this has anything to do with the topic but just in case..
            2013 places the plugin folder in the user directory.

            plugins on mac are now at:

            (user) ~/ Library/ Application Support/ SketchUp 2013/ SketchUp/ Plugins

            Screen Shot 2013-05-26 at 9.07.26 AM.png

            dotdotdot

            1 Reply Last reply Reply Quote 0
            • TIGT Offline
              TIG Moderator
              last edited by

              Does Sketchup.find_support_file('plugins') in the Ruby Console now return the User's plugins folder ?
              It used to return the HD one...

              TIG

              1 Reply Last reply Reply Quote 0
              • jeff hammondJ Offline
                jeff hammond
                last edited by

                @tig said:

                Does Sketchup.find_support_file('plugins') in the Ruby Console now return the User's plugins folder ?
                It used to return the HD one...

                entering that into 2013 ruby console:

                > Sketchup.find_support_file('plugins')
                /Users/jeff/Library/Application Support/SketchUp 2013/SketchUp/plugins

                dotdotdot

                1 Reply Last reply Reply Quote 0
                • TIGT Offline
                  TIG Moderator
                  last edited by

                  So in v2013 it looks at the writable user's plugins folder, in v8 it used to look at the HD version, which is where it WAS recommended to install plugins BUT you could have limited access rights and that caused issued.
                  On a PC we are stuck with the main Plugins folder, which often needs its permissions fixing to allow it to be 'managed' by a user... 😒

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • fredo6F Offline
                    fredo6
                    last edited by

                    TIG, Dan, Jeff,

                    Thanks.
                    However what I want to be sure of is more precise.

                    Even if a folder is writable 'manually', it may not be possible to create a folder "from a script". This is what I am looking for.

                    Fredo

                    1 Reply Last reply Reply Quote 0
                    • D Offline
                      driven
                      last edited by

                      @dan rathbun said:

                      You could have a Mac user print out all the vars:
                      :idea:

                      By default [on my mac] SketchUp.app installs into /Applications, form there...
                      ENV.each {|k,v| puts("#{k} = '#{v}'") } returns

                      PATH = '/usr/bin;/bin;/usr/sbin;/sbin'
                      TMPDIR = '/var/folders/rp/b9k42l5x7xngx_8tckgs0zdr0000gn/T/'
                      SHELL = '/bin/bash'
                      HOME = '/Users/johns_iMac'
                      USER = 'johns_iMac'
                      LOGNAME = 'johns_iMac'
                      SSH_AUTH_SOCK = '/tmp/launch-wN3bJR/Listeners'
                      Apple_Ubiquity_Message = '/tmp/launch-blGWcE/Apple_Ubiquity_Message'
                      Apple_PubSub_Socket_Render = '/tmp/launch-a6GyAw/Render'
                      DISPLAY = '/tmp/launch-mAuKFs/org.macosforge.xquartz;0'
                      IG_ROOT = '/Applications/SketchUp 2013/SketchUp.app/Contents/Resources'
                      SHLVL = '4'
                      PWD = '/'
                      _ = '/usr/bin/open'
                      __CF_USER_TEXT_ENCODING = '0x1F5;0;0'
                      COMMAND_MODE = 'unix2003'
                      
                      

                      On other setups SketchUp.app can also be installed into ~/Applications, so I moved it to see the differences and form there...

                      PATH = '/usr/bin;/bin;/usr/sbin;/sbin'
                      TMPDIR = '/var/folders/rp/b9k42l5x7xngx_8tckgs0zdr0000gn/T/'
                      SHELL = '/bin/bash'
                      HOME = '/Users/johns_iMac'
                      USER = 'johns_iMac'
                      LOGNAME = 'johns_iMac'
                      SSH_AUTH_SOCK = '/tmp/launch-wN3bJR/Listeners'
                      Apple_Ubiquity_Message = '/tmp/launch-blGWcE/Apple_Ubiquity_Message'
                      Apple_PubSub_Socket_Render = '/tmp/launch-a6GyAw/Render'
                      DISPLAY = '/tmp/launch-mAuKFs/org.macosforge.xquartz;0'
                      IG_ROOT = '/Users/johns_iMac/Applications/SketchUp 2013/SketchUp.app/Contents/Resources'
                      SHLVL = '4'
                      PWD = '/'
                      _ = '/usr/bin/open'
                      __CF_USER_TEXT_ENCODING = '0x1F5;0;0'
                      COMMAND_MODE = 'unix2003'
                      
                      

                      IG_ROOT is the only change... but that may make a difference, especially with 'sandboxing' and 'gatekeeper', I'll dig some more.

                      learn from the mistakes of others, you may not live long enough to make them all yourself...

                      1 Reply Last reply Reply Quote 0
                      • D Offline
                        driven
                        last edited by

                        @fredo6 said:

                        However what I want to be sure of is more precise.

                        ok, I found a difference, v8 run from /Applications and v2013 run from ~/Applications

                        this script is in both 'Plugins' folders, [with appropriate version in name...]

                        FileTest.writable_real?('/tmp') && FileTest.writable_real?(ENV['TMPDIR'])
                            UI.messagebox("This code is for Mac only!")
                         begin
                          home = File.expand_path "~"
                          path = File.join( home, "/from_v8_plugins_file")
                          file = File.open(path, "w")
                          file.write(Time.now.to_s + "\n" +
                        "FileTest.writable_real?('/tmp') = " + (FileTest.writable_real?("/tmp")).to_s + "\n" +
                        "FileTest.writable_real?(ENV['TMPDIR']) = " + (FileTest.writable_real?(ENV['TMPDIR'])).to_s
                        ) 
                        rescue IOError => e
                          #some error occur, dir not writable etc.
                        ensure
                          file.close unless file == nil
                        end
                        

                        on start up v8 shows the dialog and writes the temp file to my user Directory. v2013 doesn't, I'll move it back to see if it's the ~/Application path that's causing it to not run...

                        learn from the mistakes of others, you may not live long enough to make them all yourself...

                        1 Reply Last reply Reply Quote 0
                        • fredo6F Offline
                          fredo6
                          last edited by

                          John,

                          Thanks.

                          Then the next thing to check if whether a Ruby script can create a directory under the /users/...hierarchy (pointed to by HOME).

                          If so, then, I'll store the parameters in this part, whether Sketchup is installed in /users or /applications.

                          Fredo

                          1 Reply Last reply Reply Quote 0
                          • D Offline
                            driven
                            last edited by

                            I think this is the crux of the issue,
                            SU 2013 writes nothing to the
                            > FileTest.writable_real?("/Library/Application Support/SketchUp 2013/SketchUp") false
                            plugins are either in the app bundle or written to
                            > FileTest.writable_real?(File.expand_path("~/Library/Application Support/SketchUp 2013/SketchUp")) true
                            it also appears that though loaded they don't 'auto run' on load from /Plugins.

                            This does appear to be different, from v8

                            the same tmp paths are still all accessible, but not immediately on load.

                            I can get it to auto run on load from "~/Library/Application Support/SketchUp 2013/SketchUp/Tools", but only because I have another script that writes that Directory for me, and SU auto loads/runs my startup files that are in it.

                            learn from the mistakes of others, you may not live long enough to make them all yourself...

                            1 Reply Last reply Reply Quote 0
                            • TIGT Offline
                              TIG Moderator
                              last edited by

                              Fredo

                              Of course a user should be able to make a new directory in one of the user's 'own' folders.
                              After all it would be pointless having permissions set to prevent a user writing things into their own folders. Conversely it's a good idea to protect system/program folders from indiscriminate changes by general users...

                              As I mentioned several posts back, the premade user's folders for making temporary files, OR subfolders within this to keep you files together, is the ../Local/Temp/ folder or the MAC ...xxx/T/ equivalent.
                              The location one level down from that can be used for the permanent subfolder+files from your tool as this isn't auto-purged during system cleanups... ../Local/ or ...xxx/

                              I don't see you concern, these folders are designed to do exactly what you intend, so permissions should always be OK...

                              TIG

                              1 Reply Last reply Reply Quote 0
                              • jeff hammondJ Offline
                                jeff hammond
                                last edited by

                                @driven said:

                                I think this is the crux of the issue,
                                SU 2013 writes nothing to the
                                > FileTest.writable_real?("/Library/Application Support/SketchUp 2013/SketchUp") false

                                maybe you didn't give it the proper path? because when i put this in the console:

                                FileTest.writable_real?("/Users/jeff/Library/Application Support/SketchUp 2013/SketchUp/plugins")

                                it returnstrue

                                that's the exact path i was given upon entering the line from TIG-- Sketchup.find_support_file('plugins')

                                dotdotdot

                                1 Reply Last reply Reply Quote 0
                                • D Offline
                                  driven
                                  last edited by

                                  @unknownuser said:

                                  maybe you didn't give it the proper path? because when i put this in the console:

                                  @ Jeff, both paths are there, one for the user path [shows true] and one is showing that the 'old' System/Library/path doesn't exist anymore, so it's the same as doing

                                  ls /Library/Application\ Support | grep "SketchUp"``
                                  which returns...
                                  %(#4000BF)[Google SketchUp 6
                                  Google SketchUp 7
                                  Google SketchUp 8
                                  SketchUp 5]

                                  where ls ~/Library/Application\ Support | grep "SketchUp" ``
                                  returns
                                  %(#008000)[Google SketchUp 6
                                  Google SketchUp 7
                                  Google SketchUp 8
                                  SketchUp 2013
                                  SketchUp 5]

                                  SketchUp 2013 is only in the User path, as you have shown...
                                  john

                                  learn from the mistakes of others, you may not live long enough to make them all yourself...

                                  1 Reply Last reply Reply Quote 0
                                  • D Offline
                                    driven
                                    last edited by

                                    @TIG

                                    I think it's something else that has changed, I can still write files anywhere I want, if triggered by a user action after SU has loaded.

                                    before, I could just run a script on load, so the simplest example, if saved as hello.rb
                                    UI.messagebox("Hello from Plugins")
                                    it would run as soon as SU loaded it from either Plugins folder, but it doesn't now.

                                    if I use load "/Users/johns_iMac/Library/Application Support/SketchUp 2013/SketchUp/Plugins/hello.rb" from 'Ruby Console' it runs.

                                    If your trying to write to temp 'on load' it won't work from 'Plugins' anymore. It's not the destination that's at fault, it simply won't run.
                                    john
                                    EDIT: found what had changed, but I'll leave this up anyway, I had v2013 Plugins turned 'off', from another script... DOH so it will run and write from the ~/Library/path to 'Plugins'

                                    learn from the mistakes of others, you may not live long enough to make them all yourself...

                                    1 Reply Last reply Reply Quote 0
                                    • jeff hammondJ Offline
                                      jeff hammond
                                      last edited by

                                      @driven said:

                                      @ Jeff, both paths are there, one for the user path [shows true] and one is showing that the 'old' System/Library/path doesn't exist anymore, so it's the same as doing

                                      SketchUp 2013 is only in the User path, as you have shown...
                                      john

                                      right.. i think you missed the point of my post though..

                                      you got FALSE whereas i got TRUE upon using FileTest.writable_real?
                                      and we were both aiming for the same folder (the 2013 folder)

                                      ie- i had /Users/jeff in front of my path and your path started at /Library.. the front end was truncated

                                      [edit].. i'll also point out that using the tilde instead of /users/jeff returns false

                                      > FileTest.writable_real?("~/Library/Application Support/SketchUp 2013/SketchUp/plugins") false

                                      dotdotdot

                                      1 Reply Last reply Reply Quote 0
                                      • D Offline
                                        driven
                                        last edited by

                                        @unknownuser said:

                                        [edit].. i'll also point out that using the tilde instead of /users/jeff returns false

                                        that's why I used

                                        FileTest.writable_real?(File.expand_path("~/Library/Application Support/SketchUp 2013/SketchUp"))
                                        
                                        

                                        which returns True, because the

                                        File.expand_path("~/Library/Application Support/SketchUp 2013/SketchUp")
                                        

                                        bit returns /Users/johns_iMac/Library/Application Support/SketchUp 2013/SketchUp, so when combined it gives the same result as hard coding, but will work for any user.

                                        The real point I was trying to make is that any 'Plugin' that's hardcoded to use "/Library/Application Support/SketchUp [n]/SketchUp/" will fail in v2013 because that 'Folder' is not created by v2013.

                                        So, given that the only pathout is from /Users/ why would there be any permission issues at all, unless something has been 'turned' off by 'System' or the user.

                                        Even with

                                        Sketchup.plugins_disabled?
                                        

                                        returning true

                                        FileTest.writable_real?(Sketchup.find_support_file('plugins' || 'Plugins' ))
                                        

                                        returns true

                                        no matter what I try, I can't get this to fail writing a new file in a new folder in 'Plugins'

                                        if saved in ~/Library/Application\ Support/SketchUp\ 2013/SketchUp/Plugins/from_v2013_plugins.rb

                                         begin
                                          home =  File.expand_path(File.dirname(__FILE__))
                                          test_dir = home + "/!Test_Folder"
                                          Dir.mkdir(test_dir) unless FileTest.exists?(test_dir)
                                          path = File.join( test_dir, "/from_v2013_plugins_file")
                                          file = File.open(path, "w")
                                          file.write(Time.now.to_s + "\n" +
                                        "FileTest.writable_real?('/tmp') = " + (FileTest.writable_real?("/tmp")).to_s + "\n" +
                                        "FileTest.writable_real?(ENV['TMPDIR']) = " + (FileTest.writable_real?(ENV['TMPDIR'])).to_s
                                        )
                                        rescue IOError => e
                                          #some error occur, dir not writable etc.
                                        ensure
                                          file.close unless file == nil
                                        end
                                        

                                        and it's text is
                                        %(#0000FF)[Sun May 26 23:08:59 +0100 2013
                                        FileTest.writable_real?('/tmp') = true
                                        FileTest.writable_real?(ENV['TMPDIR']) = true]
                                        so, all three paths are writable_real? from a ruby script from either /Users/johns_iMac/Applications/SketchUp 2013 or from /Applications/SketchUp 2013

                                        learn from the mistakes of others, you may not live long enough to make them all yourself...

                                        1 Reply Last reply Reply Quote 0
                                        • jeff hammondJ Offline
                                          jeff hammond
                                          last edited by

                                          yeah.. i don't know..

                                          the question by fredo is

                                          can you create a folder on mac via ruby or not?

                                          @fredo.. can you make a little script or snippet to test with?

                                          dotdotdot

                                          1 Reply Last reply Reply Quote 0
                                          • D Offline
                                            driven
                                            last edited by

                                            @unknownuser said:

                                            can you create a folder on mac via ruby or not?

                                            Yes, using the ruby snippet in my last post [ saved as a .rb file ], I can create both a Folder and a file from a ruby script in the Plugins folder.

                                            @Jeff it should work on yours as well.

                                            @Fredo, is there any pattern to what's failing?

                                            john

                                            learn from the mistakes of others, you may not live long enough to make them all yourself...

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement