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

    Auto-running a Mac '.command' File from Sketchup?

    Scheduled Pinned Locked Moved Developers' Forum
    15 Posts 4 Posters 3.3k Views 4 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.
    • TIGT Online
      TIG Moderator
      last edited by

      Unfortunately I have little recent experience of Macs, and currently no access to one.

      I have made a 'cross-platform' Sketchup tool that needs to auto-run a '.cmd' file [PC] or a '.command' file [?] for a Mac, to open another application with arguments etc.
      On a PC it's relatively easy you get Sketchup/Ruby to write the '.cmd' file and then use UI::openURL(path_to_cmd) to auto-run it - on a PC the '.cmd' suffix [or '.bat'] makes it run like a binary .exe file when 'opened' OR its icon is double-clicked.
      On a Mac it's also easy enough to write a '.command' file which is treated in much the same way and runs when its icon is double-clicked [I have that working in tests]...

      Now for the rub... although I change the Mac's '.command' file's properties to be 'executable' [with chmod 0777] when I use UI::openURL(path_to_command) it doesn't auto-run the script like it does if I double-click its icon - what am I missing in the '.command' file's code - any Mac users have any clues on how I might structure the file to auto-execute just like on a PC ? It can't be impossible ??? πŸ˜•

      PS: I know this is Ruby related but hope to catch the wider audience of Mac expertise this way.... πŸ˜„

      TIG

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        I found on Mac you need to use a URL as the argument to openURL:

        UI.openURL("file:///filesystem/path/to/file")

        (relative paths didn't work for opening the Plugins folder)

        UI.openURL(Sketchup.find_support_file("plugins")) <- failed UI.openURL("file://"+Sketchup.find_support_file("plugins")) <- worked

        Hi

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

          Thanks. I'll try it... πŸ˜’

          BUT I want to execute a Mac .command file - like a .cmd on PC... will that work ???

          TIG

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

            See this:
            Run a Ruby script (.rbw) from a dock shortcut in Mac OS X (Leopard)? Options

            and this:
            File permissions w/Dir.mkdir Options

            and adding a "shebang"
            Re: A complete beginners question

            I'd think on OSX, you'd get the string for the shebang line from Ruby via:
            shebang = "\#!#{ENV['SHELL']}"

            I'm not here much anymore.

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

              Thanks Dan, I have already added a #!/bin/bash line to the start of the Mac .command script so it knows it's a 'bourne_again_shell' script.
              I have made the file executable using ruby chmod 0777 and I want it to run when opened from Sketchup by UI.openURL(path_to_command_file)...
              I am trying out Jim's suggestion and some others I got separately... I'll report back.
              It's a pain when you don't have the OS to test and Mac users with Sketchup and the 3rd party application I want to run wit arguments are not so common... πŸ˜’

              TIG

              1 Reply Last reply Reply Quote 0
              • J Offline
                Jim
                last edited by

                How to do pass arguments using openURL?

                edit: Oh I see, you don't. You are running a script which then runs the app with arguments.

                Isn't using system or ` ``` more appropriate?

                Hi

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

                  @jim said:

                  Isn't using system or ` ``` more appropriate?

                  using system by itself (on WIN) causes SU's ruby thread to stop until the system call returns. ie SU app will go into ghost-mode.
                  Does this happen on Mac? (SU stops while the system call is processed.)

                  you can subthread it:
                  result = nil t = Thread.new { result = system(cmd, args) }

                  I'm not here much anymore.

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

                    Jim, your adding file:/// idea works for Mac. 😍
                    I can now execute a suitably worded 'cmd' file on a PC OR '.command' file on a Mac (chmod 0777) using UI.openURL(path) - this let's me open an application and give it arguments [paths inside "" on PC and escaped "" on Mac...]

                    Thanks for the advice - I new it'd be something simple πŸ˜’

                    TIG

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

                      So what's the diff between chmod 0777 and 0755 ??

                      I'm not here much anymore.

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

                        @dan rathbun said:

                        So what's the diff between chmod 0777 and 0755 ??

                        Not a lot really... in the way it will be used eventually - there are three levels of 'user' [owner/group/others], with Read/Write/eXecute for each set by 1/2/4 for each, added together for multi-layered options: so for ALL users to be able to RWX its 777, of course even 444 would make it executable by all***, but not readable or writable by any - with 777 anyone can RWX the script even if they don't have some mysterious 'membership' or 'rights' that I've missed: ***it doesn't matter too much as the .command file should be auto-erased after running anyway, but during beta-tests it's retained, so I can read its contents to see if it's getting made as expected; so then on the next run it needs to be 'Writable' by any users, so that's at least a 666 ? so it seems to me that 777 is best as it means absolutely anyone can Read/Write/Execute the script after it's been made [and kept temporarily]... Your suggested 755 would be Writable and Executable by all, but only Readable by its owner, which seems an odd choice since others might want to read it too ?

                        Ownership and permissions of Unix files are a minefield so I just set mine to the widest possible field of view...

                        TIG

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

                          @tig said:

                          @dan rathbun said:

                          So what's the diff between chmod 0777 and 0755 ??

                          Your suggested 0755 would be Writable and Executable by all, but only Readable by its owner, which seems an odd choice since others might want to read it too ?

                          Are the fields backwards? (like big endian?)
                          The way I'd read 0755 is:
                          Executable by owner, group and all
                          Readable by owner, group and all
                          Writable by only the third significant value (depending on which is the Least significant value.)

                          @tig said:

                          Ownership and permissions of Unix files are a minefield so I just set mine to the widest possible field of view...

                          0777 makes sense.. of course.

                          I'm not here much anymore.

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

                            The RWX order of setting is Owner/Group/Others so 755 is RWX for the file's owner and -WX for the group and also for 'others'.
                            Whereas 710 would be RWX for the file's owner, for the group [-R-] and no permissions at all for 'others' [---].
                            Then 744 would be a logical setting for making an executable file [--X] so that all others users can run but they can't alter, or even read - its owner has all permission though [RWX]. It it were owned by root [superuser] then it would be well protected.

                            Anyway, as you agreed, 777 covers all eventualities for a .command file that will be temporary in the final tool, but might be retained during testing and read/written/executed by anyone then...

                            TIG

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

                              @dan rathbun said:

                              you can subthread it:
                              result = nil t = Thread.new { result = system(cmd, args) }

                              Dan this works brilliantly with applescript on a mac....

                              I've been going around in circles trying to run an applescript that needs to interact with SU, after being launched by SU.

                              I new I needed a separate thread, I just didn't to look for such a simple solution. cheers.

                              Something else I found on the 'journey' was to include the shebang, save it as plane txt NO extension, and set chmod ug+wr+x in /usr/local/bin/bht_su

                              Now it can the be called from terminal with just bht_su or SU with your
                              result = nil t = Thread.new { result = system("bht_su") }
                              SU returns
                              #<Thread:0x5664bc60 sleep>
                              so the question is, should I kill that thread, or is sleep enough...

                              t.kill appears to work?
                              #<Thread:0x5663b360 dead>

                              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

                                @dan rathbun said:

                                If you want your "thing" to work on older SU versions, the API's thread-like UI.start_timer() block method may be a better choice.

                                I just ran my demo script on v5 and the thread running the shell works fine, funnily though...

                                The applescript that resizes the windows, for the demo, also runs it's own thread, it launches v8 and resizes it's windows, while, at the same time, v5 is recieving a stream of new messages.
                                I don't need to do the both at once in use and the Applescript could be Targeted better if needs be.

                                @unknownuser said:

                                If YOU get comfy with Threads on Mac (under Sketchup,) a tutorial may help others out.

                                Everything I've found and tested works so far. The difference's between Thread.new, Thread.fork, Thread.start
                                are a bit confusing as sometime they behave identically, and at other times only .new will work. When I work out why I'll report back...
                                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
                                • Dan RathbunD Offline
                                  Dan Rathbun
                                  last edited by

                                  Well I am NOT an expert on threads.

                                  Doc: http://www.ruby-doc.org/core-1.8.6/Thread.html

                                  What we DO know is that they were CRAP on PC with Ruby v1.8.0

                                  They are better, in v1.8.6-p287, likely even better in v1.8.7 branch.

                                  But they are still Green threads, not native threads (at least on PC.)
                                  I'm not sure if Apple tweeked the Ruby editions they install on Macs, and what 'kind' of threads you get, on Apple platforms.

                                  If you want your "thing" to work on older SU versions, the API's thread-like UI.start_timer() block method may be a better choice.

                                  Otherwise... if the rdocs are too techno.. check out all the books on Ruby. Maybe tutorials on the web.

                                  We've avoided them on PC because they were too problematic (... likely because PC Sketchup was running Ruby v1.8.0 for so many versions.)
                                  I never (on my machine,) ran Sketchup with the obsolete initial release of v1.8.0; always something >= v1.8.6-p111
                                  ~
                                  If YOU get comfy with Threads on Mac (under Sketchup,) a tutorial may help others out.

                                  I'm not here much anymore.

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

                                  Advertisement