Here’s a way to get the information you want:
[VarSet,layerName,[IGetTitle,"Tool:Layers:Layer Intensity"]]
[VarSet,layerPath,[StrMerge,"Tool:Layers:",#layerName]]
[VarSet,layerInfo,[IGetInfo,layerPath]]
//turn off recording
[ISet,Tool:Layers:Layer Intensity,0]
[ISet,Tool:Layers:Layer Intensity,1]
[If,([StrFind, "PolyPaint (Empty)",layerInfo]== -1),//we've got some polypaint
[IPress,Tool:Masking:Mask By Intensity]
[IPress,Tool:Masking:Inverse]
]
However note that there’s a drawback - this will only work if the layers have different names. If the layers have the same names then the ‘layerPath’ will be the first layer and so the ‘layerInfo’ will be for that, not the selected layer.