pluginssketch-3sketchappcocoascript

Sketch Plugin Develop : Is there an API for Sketch to unlock the size of MSBitmapLayer?


The unlock size button in Sketh App

I can click this button to unlock the size of a layer in Sketch App. And then I can change the width/height ratio of the image. But I want my Sketch plugin to do this job instead of using my hand to click the button. Is there an API for Sketch to unlock the size of MSBitmapLayer?

I've tried "[layer setIsLocked:false]", but it's not about the size lock.

Thank you very much.


Solution

  • The particular function you are looking for is setConstrainProportions()

    Here is an example for you, It might help you in understanding it

    function toggleConstrainProportions(context) {
      var doc = context.document
      var selection = context.selection
    
      // Toggles the currently selected item(s) 'Constrain Proportions'setting
    
      for (var i = 0; i < [selection count]; i++) {
        var s = [selection objectAtIndex: i]
        s.frame().setConstrainProportions(!s.constrainProportions())
      }
    }
    

    Hope this is helpful :)