You can add two fields to component from where you want to copy the path :
<compPath
jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/text"
granite:class="actual-component-path"
text="${requestPathInfo.suffix}"/>
<button
jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/button"
granite:class="copy-component-path"
text="Copy component path"/>
And then can write small js to copy it on click of button
Sample js
$(".copy-component-path").click(function(){
var button = $(this);
var compPath = button.parent().find(".actual-component-path").text(); navigator.clipboard.writeText(compPath);
})
And if I may suggest an alternative approach, you can write a sling model in your second component and can easily find the first component's path which you want to include in that sling model using a simple query or any other option that you find suitable.
Then call that sling model in your HTL to get the component path and include in data-sly-resource. Will save you all the hassle of going through copy/past etc.
Hope this helps.