I would argue that this approach is inefficient. It is better to include this in a script, and add the script to your menu.
The following function should work.
Function HidePCBDesignators(Board : IPCB_Board) : Boolean;
Var
PCBIterator : IPCB_BoardIterator;
PCBComponent : IPCB_Component;
Begin
PCBIterator := Board.BoardIterator_Create;
PCBIterator.AddFilter_ObjectSet(MkSet(eComponentObject));
PCBIterator.AddFilter_IPCB_LayerSet(LayerSet.AllLayers);
PCBIterator.AddFilter_Method(eProcessAll);
PCBComponent := PCBIterator.FirstPCBObject;
While (PCBComponent <> Nil) Do
Begin
PCBServer.SendMessageToRobots( PCBComponent.I_ObjectAddress, c_Broadcast,
PCBM_BeginModify , c_NoEventData); // Alert undo system to a change
PCBComponent.NameOn := FALSE; // Hide the PCB designator
PCBServer.SendMessageToRobots(
PCBComponent.I_ObjectAddress,c_Broadcast,PCBM_EndModify ,c_NoEventData);
PCBComponent := PCBIterator.NextPCBObject;
End;
Board.BoardIterator_Destroy(PCBIterator);
End;
This link will explain how to add any script as a button on the menu:
https://techdocs.altium.com/display/SCRT/Running+Scripts+in+Altium+Designer
Another method is the global method, by using the DXP Preferences -> PCB Editor -> Defaults -> Component and activate the checkbox under Designator -> Hide.