Set a background image for NSWindow using Cocoascript

580 Views Asked by At

I'm trying to create a window in this style:

enter image description here

I have the following code, but I just get a back background. I guess it can't find the image.

var win = [[NSWindow alloc] init]
[win setFrame:NSMakeRect(0, 0, 320, 200) display:false]
[win setBackgroundColor:[NSColor colorWithPatternImage:[NSImage imageNamed:@"background.png"]]];

I'm writing this for a Sketch App plugin so I have to use Cocoascript. My plugin structure looks like

myplugin.sketchplugin/
    Contents/
        Sketch/
            app.js
            manifest.json
        Resources/
            background.png

I'm neither sure where to put the background image or how to reference it. Would it have to be in manifest.json? Under which key?

1

There are 1 best solutions below

0
On

(I don't use Sketch so this is untested)

Here is an example how to load World.pdf that resides in the Resources folder.

It basically does this:

var plugin = context.plugin;// the plugin (MSPluginBundle)
var imageLayer = SketchLib.addImageLayer(group, {"name": "World", "url": [plugin urlForResourceNamed:"World.pdf"]})

So if [plugin urlForResourceNamed:"World.pdf"] does the job I suspect that

[plugin urlForResourceNamed:"background.png"] should also.