How to Hide All Windows of an App Other Than the Front One

This is very hard, at least for me. I have spent many hours on this, but have learned a lot.

Here is a general purpose script that should work well with all apps.
I have tested it with:
• Finder
• Apple Mail
• Outlook 2011
• Evernote
• Chrome
• Safari

It is NOT perfect. There could be issues with Finder if you have actual Finder windows with the same name.

If you have any ideas on how to improve, or find issues, please post.

Minimize All App Windows Except Frontmost

(*
===============================================================================
  Minimize All App Windows Except Frontmost
===============================================================================

VER:   1.0    LAST UPDATE:   2016-04-09

PURPOSE:
  • ============================================================================
  Minimize All App Windows Except Frontmost

AUTHOR:    JMichaelTX
          Find any bugs/issues or have suggestions for improvement?
          Contact me via PM or at blog.jmichaeltx.com/contact/

REQUIRED:
  1.  Mac OS X Yosemite 10.10.5+
  2.  Mac Applications -- NONE are required, but this has been tested with:
        • Finder
        • Apple Mail
        • Outlook 2011
        • Evernote
        • Chrome
        • Safari
        
  3.  INTERNAL FUNCTIONS:
      • removeDups(pList)

REF:  The following were used in some way in the writing of this script.
  1.  
===============================================================================
   
*)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "System Events"
  
  --- GET FRONTMOST APP ---
  --    CAUTION:
  --      1. Some commands do NOT work when using tell application with 
  --        a variable instead of explicit text
  --      2. Different Apps have different commands to minimize a window
  --      3. Thus, I had to use System Events with a UI keyboard command
  --    If you know of a better way, please let me know
  
  set frontAppName to item 1 of (get name of processes whose frontmost is true)
  
  tell process frontAppName
    
    set numWin to count of windows
    
    --- GET LIST OF WINDOW NAMES ---
    --    CAUTION:
    --      1. Some apps, like the Finder, can have different windows with the same name
    --      2. Some apps, like the Finder, report duplicate windows in error
    
    set winTitleList to title of every window
    set winTitleList to my removeDups(winTitleList) -- needed for Finder windows
    
    --- Remove 1st Window (the current Frontmost) from List ---
    set winTitleList to (items 2 thru -1 of winTitleList)
    
    repeat with windowTitle in winTitleList
      -- Select/Activate Each Window to Make Frontmost --
      click menu item windowTitle of menu 1 of menu bar item "Window" of menu bar 1
      delay 0.1
      
      -- Minimize the Window --
      keystroke "m" using command down -- this is same as CMD-M
    end repeat
    
  end tell
end tell


--~~~~~~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~

on removeDups(pList)
  local newList
  set newList to {}
  repeat with iItem from 1 to count pList
    set itemStr to item iItem of pList
    if itemStr is not in newList then set end of newList to itemStr
  end repeat
  return newList
end removeDups