public static void SetAppMenuItems(CommandManager manager, CommandEntrySet entrySet)
        {
            HIMenuItem mnu = HIToolbox.GetMenuItem((uint)CarbonCommandID.Hide);

            bool firstTime = appMenu == IntPtr.Zero;

            appMenu = mnu.MenuRef;

            var existingitems = GetMenuCommandIDs(appMenu).ToList();

            for (int i = existingitems.Count - 1; i >= 0; i--)
            {
                var item = new HIMenuItem(appMenu, (ushort)(i + 1));
                //if first time, keep all the existing items except QuitAndCloseAllWindows
                if (firstTime && existingitems [i] != (uint)CarbonCommandID.QuitAndCloseAllWindows)
                {
                    //mark them so we can recognise and keep them in future
                    //HIToolbox.SetMenuItemReferenceConstant (item, uint.MaxValue);
                    continue;
                }

                // if it's not the first time, keep the original items we marked
                if (!firstTime && HIToolbox.GetMenuItemReferenceConstant(item) == UInt32.MaxValue)
                {
                    continue;
                }

                //remove everything else
                HIToolbox.DeleteMenuItem(item);
                existingitems.RemoveAt(i);
            }

            // Iterate backwards over the entries. For each one, try to find a matching built-in item. If successful,
            // get all the items from that point to the last item that was not yet inserted, and insert them
            // at that point.
            var entries         = entrySet.ToList();
            int uninsertedCount = entries.Count;

            for (int i = entries.Count - 1; i >= 0; i--)
            {
                var entry = entries [i];
                if (entry.CommandId == null || entry.CommandId == Command.Separator)
                {
                    continue;
                }
                var id = GetMacID(entry.CommandId);

                //find an existing item
                int match = existingitems.IndexOf(id);

                if (match >= 0)
                {
                    //if there are uninserted items *after* this one, insert them at this point
                    if ((uninsertedCount - i) > 0)
                    {
                        var items          = entries.Skip(i + 1).Take(uninsertedCount - i - 2);
                        var insertionPoint = new HIMenuItem(appMenu, (ushort)(match + 1));
                        AddMenuItems(insertionPoint, items);
                    }
                    uninsertedCount = i;
                }
            }
            // Finally, insert any remaining items at the beginning/top of the menu.
            if (uninsertedCount > 0)
            {
                AddMenuItems(new HIMenuItem(appMenu, 0), entries.Take(uninsertedCount));
            }
        }