Ahoy. I was sad to see that the old sketchplugins.com forum was gone. Glad to be here. Hoping someone can help.
I have a plugin that updates placed images after they’ve been changed. An automated “Replace Image…” if you will. A recent Sketch update broke the old Obj-C methods I used so I’m trying to get it working again. I think I’m on the right track, but what I’ve come up with still doesn’t work.
The script doesn’t throw an error, nor does the image appear to change visually. It looks like nothing happened. But if you save the document and re-open it, the image is seemingly gone. The layer is still there, it has the correct dimensions, you can move it via keyboard, mouse, or inspector. It’s just totally transparent.
I’ve pasted below reduced version of what I have so far. To use it, first place any image as a layer, select that layer, then run the following script, changing the imagePath
variable to a different image file on your computer.
const sketch = require("sketch");
function updateBitmapLayer(layer,imagePath) {
const Image = require('sketch/dom').Image;
const newImage = new Image({image: imagePath});
const newImageData = newImage.image;
layer.image = newImageData;
}
let selectedLayer = context.selection.firstObject();
let imagePath = "/path/to/image.png";
updateBitmapLayer(selectedLayer,imagePath);
Thanks in advance.