I'm working on a plugin for Sketch that allows you to easily upload and iterate on Sketch designs and assets to JIRA. I'm using a WebView and drag-and-drop flow for uploading layers/artboards/slices to a selected JIRA issue:
Sketch handily converts dragged images into whatever your default export configuration is. However, I'd love to be able to detect if the user has mutliple export configurations and export all of them to the attached JIRA issue. To do this, I need to know which layer the user has dragged, rather than just the exported image file. Therein lies the problem.
When you drag a layer, Sketch writes the file to disk and puts a URL with the UTI string
public.file-url on the system drag pasteboard. Currently I'm reading the file URL and uploading the contents to JIRA. But that URL contains no information that could be used to determine which layer was dragged (the filename has some relationship to the layer name, but I can't safely use that since layer names aren't necessarily unique).
Sketch also puts an object with the UTI string
com.bohemiancoding.draggedLayer on the system drag pasteboard. You can use
MSPasteboardManager.readPasteboardLayersFromPasteboard to read the layer back, but that method seems to generate a new ID for the layer when reading it back from the pasteboard, and I haven't determined a way to determine the original ID of the dragged layer in order to look up it's export options. You can test this yourself by dragging a layer from Sketch to your desktop and running this code snippet a few times via 'Run Script...':
var pb = NSPasteboard.pasteboardWithName(NSDragPboard)
var pbLayers = MSPasteboardManager.readPasteboardLayersFromPasteboard_options(pb, nil)
On the first run, you'll see something like:
"<MSBitmapLayer: 0x7fbd97a32f30> Layer name@1x (450AEEC6-CCE3-405F-92E8-E84C9DAA022F)"
Then on the second try:
"<MSBitmapLayer: 0x7fbd97938f00> Layer name@1x (70A23A9D-38FB-453B-BF27-0A5F97430840)"
The layer ID in the round bracket (which appears to be a UUID) changes each invocation, despite the same dragged data being decoded each time.
Any ideas or workarounds for determining which layer(s) a user just dragged would be much appreciated!
Note that I have tried a somewhat hacky workaround of checking which layers the user has selected when the drag occurs, but it turns out that you can drag-and-drop items in MacOS without selecting them first.