Using a global array between files

71 Views Asked by At

I'm currently writing a Sketch Plugin and I'm trying to store data inside an global array. In the copy.sketchscript the data is generated, and in paste.sketchscript I'm trying to retrieve the array data. However, when I log the variable, it returns empty. What can I do to update the array data properly so that every function can access it?

Here's my code.

library/common.js

var verticalRulers = new Array; // global?

function copyVertical(context) {
    var doc = context.document

    var target = [[doc currentPage] currentArtboard] || [doc currentPage]
    var countVertical = [[target verticalRulerData] numberOfGuides]

    for(var i=0; i < countVertical; i++) {
        var thisRuler = [[target verticalRulerData] guideAtIndex:i]
        verticalRulers.push(thisRuler);
    }
}

function pasteVertical(context) {
    var doc = context.document
    var target = [[doc currentPage] currentArtboard] || [doc currentPage]

    for(i = 0; i < verticalRulers.length; i++) {
        var thisRuler = verticalRulers[i];

        [[target verticalRulerData] addGuideWithValue: thisRuler]
    }
}

copy.sketchscript

@import 'library/common.js'

function onRun(context) {
    verticalRulers = copyVertical(context);
    log(verticalRulers) // return right data from variable;
}

paste.sketchscript

@import 'library/common.js'

function onRun(context) {
    pasteVertical(context);
    log(verticalRulers); // returns an empty array
}
0

There are 0 best solutions below