<!DOCTYPE html>
<html>

  <head>
  
    <script type="text/javascript">
      
      const bars = safari.extension.bars;
      
      function performCommand(event)
      {
        if (event.command == 'searchmarks.button.toggle-bar')
        {
          toggleSearchmarksBar();
        }
      }
      
      function validateCommand(event)
      {
      }
    
      function toggleSearchmarksBar()
      {
        var found = false;
        
        for (var i = 0; i < bars.length; ++i)
        {
          var bar = bars[i];
          
          if (bar.browserWindow === safari.application.activeBrowserWindow && bar.identifier === "searchmarks.bar")
          {
            if (bar.visible)
            {
              bar.hide();
            }
            else
            {
              bar.show();
              bar.contentWindow.SearchmarksUI.focusSearchField();
            }
              
            found = true;
          }
        }
        
        if (!found)
        {
          alert("Error: Searchmarks bar not found. Try toggling the Searchmarks bar via the View menu. Sorry, I'm working on it :(");
          throw "Searchmarks bar not found";
        }
      }
      
      safari.application.addEventListener("command", performCommand, false);
      safari.application.addEventListener("validate", validateCommand, false);
    
    </script>
  
  </head>

  <body>
  </body>

</html>
<!DOCTYPE html>
<html>

  <head>
  
    <script type="text/javascript">
      
      const bars = safari.extension.bars;
      
      function performCommand(event)
      {
        if (event.command == 'searchmarks.button.toggle-bar')
        {
          toggleSearchmarksBar();
        }
      }
      
      function validateCommand(event)
      {
      }
    
      function toggleSearchmarksBar()
      {
        var found = false;
        
        for (var i = 0; i < bars.length; ++i)
        {
          var bar = bars[i];
          
          if (bar.browserWindow === safari.application.activeBrowserWindow && bar.identifier === "searchmarks.bar")
          {
            if (bar.visible)
            {
              bar.hide();
            }
            else
            {
              bar.show();
              bar.contentWindow.SearchmarksUI.focusSearchField();
            }
              
            found = true;
          }
        }
        
        if (!found)
        {
          throw "Searchmarks bar not found";
        }
      }
      
      safari.application.addEventListener("command", performCommand, false);
      safari.application.addEventListener("validate", validateCommand, false);
    
    </script>
  
  </head>

  <body>
  </body>

</html>
Remove alert dialog when error encountered trying to toggle bar
Remove alert dialog when error encountered trying to toggle bar

It's obvious that it's not working, so it doesn't need an annoying alert.
