sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    [Code] FAQ: Detect if plugin is running on the Mac vs PC ?

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 2 Posters 1.9k Views 2 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.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by Dan Rathbun

      FAQ: How can I detect if my plugin is running on the Mac vs. PC ?
      from: Frequently Asked Questions (FAQ)

      The answer (as of this posting date,) has a VERY POOR example. Do not use it.
      EDIT(31MAR2012): The FAQ page has at last been updated ! (Thanks Simone.)


      DO

      It is BEST to create Boolean constants for comparison:

      <span class="syntaxdefault">MAC </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> Object</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">RUBY_PLATFORM </span><span class="syntaxkeyword">=~</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">/(</span><span class="syntaxdefault">darwin</span><span class="syntaxkeyword">)/</span><span class="syntaxdefault">i </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> true </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> false </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> unless defined</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">MAC</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">WIN </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> not MAC </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> unless defined</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">WIN</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">OSX </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> MAC unless defined</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">OSX</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">PC </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> WIN unless defined</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">PC</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span>
      
      • You can put these outside your author module (namespace,) so they are global constants* Or within anyone of your namespaces (submodules and/or classes,) so they are local.

      Then you can just have an if statement like:

      <span class="syntaxdefault">if MAC<br />  dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">show_modal</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault">elsif WIN<br />  dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">show</span><span class="syntaxkeyword">()<br />else<br /></span><span class="syntaxdefault">  UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Unsupported Platform for UI;;WebDialog class.'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end</span>
      

      or:

      if WIN
        require('WIN32OLE')
      else
        puts("Win32OLE.so is a Windows only utility!", MB_OK)
      end
      

      DO NOT

      <span class="syntaxdefault">PLATFORM </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Object</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">RUBY_PLATFORM </span><span class="syntaxkeyword">=~</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">/</span><span class="syntaxdefault">mswin</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">windows </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">((</span><span class="syntaxdefault">Object</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">RUBY_PLATFORM </span><span class="syntaxkeyword">=~</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">/</span><span class="syntaxdefault">darwin</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">mac </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">other</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span>
      

      The example overwrites (reassigns,) the value of the standard (although deprecated,) Ruby constant PLATFORM. The example should use another constant name, perhaps PLAT_SYM.

      Note also that the example creates interned String values that will later be used for comparison.
      Ruby is extremely SLOW at String comparison.
      Observe:
      PLAT_SYM.to_s == "windows"
      Is much slower than testing a boolean constant. Reason is that Ruby must create a new String instance object, whenever it encounters a literal quoted text in your code. (In this example, Ruby must create TWO new String instance objects, one on each side of the == operator. They are abandoned, aka unreferenced, after the line is evaluated. Ruby garbage collection will clean them up eventually. Sooner if the statement was within a method.)

      It also makes no sense, to assign Symbol constants, and then convert them to a String during a boolean test. It is better (if you absolutly need to use a Symbol,) to do Symbol comparison, like:
      PLAT_SYM == :windows


      Edited and re-organized, 09 FEB 2012.


      [Comment added to bottom of FAQ webpage and Documentation Bug Report filed. ~ Dan]

      I'm not here much anymore.

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

        (31MAR2012): The FAQ page has at last been updated ! (Thanks Simone.)

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          @dan rathbun said:

          (31MAR2012): The FAQ page has at last been updated ! (Thanks Simone.)

          What was changed? The FAQ appear to use your "DO NOT" example... 😕

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

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

            @thomthom said:

            @dan rathbun said:

            (31MAR2012): The FAQ page has at last been updated ! (Thanks Simone.)

            What was changed? The FAQ appear to use your "DO NOT" example... 😕

            He changed the name of the constant from the Ruby PLATFORM to a custom CURRENT_PLATFORM (although he refused to give up the symbol type example,) he did add a (similar) boolean example as I gave it. (or close enough.)

            I guess it is the best I can hope for.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              I don't see the point at the Symbol pattern he used...

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

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

                Neither do I, as I had said in the OP.

                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