When do you call attention on deck for officers?

On deck or outdoors, "Attention on Deck" is generally called by whoever sees the officer first if the assembly of personnel present isn't generally aware of the officers' approach, or if the officer appears suddenly from inside a building, when that officer is within saluting range or near saluting range of the nearest junior person present. If indoors or aboard ship, it's called when the officer enters the space if the protocol is required.

At a formal unit assembly, the command will generally be given by the Senior Enlisted present or Senior Officer - it depends on who's present at the time and what the unit regulations are.

For Quarterdecks, the Quarterdeck Watch (or Topside Watch on a Submarine) will call AoD when the officer comes into saluting range. For Flag Officers (including the CO), the watch will ring the ship's bell an announce their arrival over the ship's 1MC circuit (General Announcement).

In other words - outdoors and on deck, it's never a sure thing, but for most units it's when the officer is within saluting range of the nearest junior member to that officer. Indoors, it's easy, as it's always when the officer enters the room.

Each unit/command usually has its own instructions for Naval protocol - this includes Marine Corps Commands, as they are a part of the Navy and use Navy terminology as well as their own.