Yes this could be done with a macro.
You'd need to home the printer in some way first so that you can safely and accurately position the nozzle where you need it.
You'd need the sensor for touching the nozzle to be working in the firmware to begin with.
Then you'd need to position the nozzle, ensure the probe type for the sensor is selected, touch it, and then switch over to the IR sensor and use G30 S-3 to measure the trigger height and apply it
https://duet3d.dozuki.com/Wiki/Gcode#Section_G30_Single_Z_Probe
And then M500 P31 to save the G31 trigger height to the config-override file.
But if you already have a working touch sensor, do you really also need an IR sensor?