0

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
}
Enzio
  • 377
  • 4
  • 15
  • I'm not familiar with any of the technologies, and I couldn't easily find documentation on what the heck `@import` does, so... how exactly does `@import 'library/common.js'` work? Does it just insert the contents of that script at the top? If so, you're re-creating the array each time, so of course it's going to be empty. – Anthony Grist Jul 03 '15 at 08:34
  • @AnthonyGrist You might be right... http://bohemiancoding.com/sketch/support/developer/02-common-tasks/04.html Any other idea how I can achieve what I want? – Enzio Jul 03 '15 at 09:14

0 Answers0