<NME> lib.js
<BEF> @import 'actions/view-inventory.cocoascript'
@import 'actions/statistics-inventory.cocoascript'
@import 'actions/demo-inventory.cocoascript'
@import 'run-actions.cocoascript'


function ToolbarInventory () {}

// UIBar.init = function(context, command){
//     this.context = context;
//     this.extend(context);
//     this.pluginRoot = this.scriptPath
//             .stringByDeletingLastPathComponent()
//             .stringByDeletingLastPathComponent()
//             .stringByDeletingLastPathComponent();
//     // this.pluginSketch = this.pluginRoot + "/Contents/Sketch/library";
//     localPluginRoot = this.pluginRoot + "/Contents/Sketch/";
//
//     this.doc = context.document;
//     this.docData = this.doc.documentData();
//     this.page = this.doc.currentPage();
//     this.artboard = this.page.currentArtboard();
//     this.current = this.artboard || this.page;
//     // coscript.setShouldKeepAround(true);
//
//     if(command && command == "uibar"){
//         UIBar.createUIBar();
//         return false;
//     }
//     if(command){
//         switch(command){
//             case "top-guide":
//                 // this.topGuide();
//                 onSimulateKeynoteRun(this.context)
//                 break;
//             case "right-guide":
//                 // this.rightGuide();
//                 onGenerateStatesRun(this.context)
//                 break;
//             case "bottom-guide":
//                 // this.bottomGuide();
//                 onReplicateLayersRun(this.context)
//                 break;
//             case "left-guide":
//                 // this.leftGuide();
//                 var localPath = pluginPath + "/manifest.json"
//                 log("generating path" + localPath)
//                 var manifestText = readTextFromFile(localPath)
//                 log(manifestText)
//                 writeTextToFile(manifestText, pluginPath + "/manifest2.json")
//
//                 break;
//             case "v-center-guide":
//                 // this.vCenterGuide();
//                 onSelectProjectFolderRun(this.context)
//                 break;
//             case "h-center-guide":
//                 // this.hCenterGuide();
//                 onRemoveProjectFolderRun(this.context)
//                 break;
//             case "top-bottom-guides":
//                 // this.topBottomGuides();
//                 break;
//             case "right-left-guides":
//                 // this.rightLeftGuides();
//                 break;
//             case "remove-all-guides":
//                 // this.removeAllGuides();
//                 break;
//         }
//     }
//
// }
//
// UIBar.extend = function(options, target) {
//     var target = target || this;
//     for (var key in options) {
//         target[key] = options[key];
//     }
//     return target;
// }




// var CX = {
//     init: function(context, command){
//         this.context = context;
//         this.extend(context);
//         this.pluginRoot = this.scriptPath
//                 .stringByDeletingLastPathComponent()
//                 .stringByDeletingLastPathComponent()
//                 .stringByDeletingLastPathComponent();
//         // this.pluginSketch = this.pluginRoot + "/Contents/Sketch/library";
//         localPluginRoot = this.pluginRoot + "/Contents/Sketch/";
//
//         this.doc = context.document;
//         this.docData = this.doc.documentData();
//         this.page = this.doc.currentPage();
//         this.artboard = this.page.currentArtboard();
//         this.current = this.artboard || this.page;
//         coscript.setShouldKeepAround(true);
//
//         if(command && command == "uibar"){
//             this.UIBar();
//             return false;
//         }
//         if(command){
//             switch(command){
//                 case "top-guide":
//                     // this.topGuide();
//                     onSimulateKeynoteRun(this.context)
//                     break;
//                 case "right-guide":
//                     // this.rightGuide();
//                     onGenerateStatesRun(this.context)
//                     break;
//                 case "bottom-guide":
//                     // this.bottomGuide();
//                     onReplicateLayersRun(this.context)
//                     break;
//                 case "left-guide":
//                     // this.leftGuide();
//                     var localPath = pluginPath + "/manifest.json"
//                     log("generating path" + localPath)
//                     var manifestText = readTextFromFile(localPath)
//                     log(manifestText)
//                     writeTextToFile(manifestText, pluginPath + "/manifest2.json")
//
//                     break;
//                 case "v-center-guide":
//                     // this.vCenterGuide();
//                     onSelectProjectFolderRun(this.context)
//                     break;
//                 case "h-center-guide":
//                     // this.hCenterGuide();
//                     onRemoveProjectFolderRun(this.context)
//                     break;
//                 case "top-bottom-guides":
//                     // this.topBottomGuides();
//                     break;
//                 case "right-left-guides":
//                     // this.rightLeftGuides();
//                     break;
//                 case "remove-all-guides":
//                     // this.removeAllGuides();
//                     break;
//             }
//         }
//
//     },
//     extend: function( options, target ) {
//         var target = target || this;
//         for ( var key in options ){
//             target[key] = options[key];
//         }
//         return target;
//     }
// };


// CX.extend({
//     // getRect: function(layer){
//     //     var rect = layer.absoluteRect();
//     //     return {
//     //         x: Math.round(rect.x()),
//     //         y: Math.round(rect.y()),
//     //         width: Math.round(rect.width()),
//     //         height: Math.round(rect.height()),
//     //         maxX: Math.round(rect.x() + rect.width()),
//     //         maxY: Math.round(rect.y() + rect.height()),
//     //     };
//     // },
//     updateContext: function(){
//         currentDocument = NSDocumentController.sharedDocumentController().currentDocument();
//         this.context.document = NSDocumentController.sharedDocumentController().currentDocument();
//         // this.context.selection = this.context.document.selectedLayers();
//         return this.context;
//     },
// });
//
// CX.extend({
//     getDistance: function(targetRect, containerRect){
//         var containerRect = containerRect || this.getRect(this.current);
//         return {
//             top: (targetRect.y - containerRect.y),
//             right: ((targetRect.x - containerRect.x) + targetRect.width),
//             bottom: ((targetRect.y - containerRect.y) + targetRect.height),
//             left:(targetRect.x - containerRect.x),
//             vCenter:((targetRect.x - containerRect.x) + (targetRect.width / 2)),
//             hCenter:((targetRect.y - containerRect.y) + (targetRect.height / 2)),
//         };
//     },
//     message: function(message){
//         this.doc.showMessage(message);
//     }
// });


// CX.extend({
//     selectError: function(){
//         var self = this,
//             selection = this.selection,
//             inArtboard = false;
//
//         if( selection.count() <= 0 ){
//             this.message("Select an element.")
//             return false;
//         }
//         if(selection.count() == 2){
//             this.message("Can't select mutiple element.")
//             return false;
//         }
//         if(inArtboard || !this.artboard){
//             this.message("Element should be inside Artboard")
//             return false;
//         }
//         return true;
//     },
//     setCount: function(gain){
//         var self = this,
//             selection = this.selection,
//             layer = selection.firstObject();
//         var targetRect = this.getRect(layer),
//             containerRect = this.getRect(this.artboard),
//             eleCount = this.getDistance(targetRect, containerRect);
//         return eleCount;
//     }
// });


// CX.extend({
//     topGuide: function(){
//       // runSimulateKeynote(this.context)
//       onSimulateKeynoteRun(this.context)
//         // var self = this;
//         // if(!this.selectError()) return;
//         // this.artboard.verticalRulerData().addGuideWithValue(this.setCount().top);
//     },
//     rightGuide: function(){
//         onGenerateStatesRun(this.context)
//         // var self = this;
//         // if(!this.selectError()) return;
//         // this.artboard.horizontalRulerData().addGuideWithValue(this.setCount().right);
//     },
//     bottomGuide: function(){
//         onReplicateLayersRun(this.context)
//         // var self = this;
//         // if(!this.selectError()) return;
//         // this.artboard.verticalRulerData().addGuideWithValue(this.setCount().bottom);
//     },
//     leftGuide: function(){
//         var localPath = pluginPath + "/manifest.json"
//         log("generating path" + localPath)
//         var manifestText = readTextFromFile(localPath)
//         log(manifestText)
//         writeTextToFile(manifestText, pluginPath + "/manifest2.json")
//         // var self = this;
//         // if(!this.selectError()) return;
//         // this.artboard.horizontalRulerData().addGuideWithValue(this.setCount().left);
//     },
//     vCenterGuide: function() {
//         onSelectProjectFolderRun(this.context)
//         // log("URL")
//         // var url = [NSURL URLWithString:"https://bit.ly/2i1Usqq"]
//
//
//         // var self = this;
//         // if(!this.selectError()) return;
//         // this.artboard.horizontalRulerData().addGuideWithValue(this.setCount().vCenter);
//     },
//     hCenterGuide: function(){
//         onRemoveProjectFolderRun(this.context)
//         // var self = this;
//         // if(!this.selectError()) return;
//         // this.artboard.verticalRulerData().addGuideWithValue(this.setCount().hCenter);
//     },
//     topBottomGuides: function(){
//         // var self = this;
//         // if(!this.selectError()) return;
//         // this.artboard.verticalRulerData().addGuideWithValue(this.setCount().top);
//         // this.artboard.verticalRulerData().addGuideWithValue(this.setCount().bottom);
//     },
//     rightLeftGuides: function(){
//         // var self = this;
//         // if(!this.selectError()) return;
//         // this.artboard.horizontalRulerData().addGuideWithValue(this.setCount().right);
//         // this.artboard.horizontalRulerData().addGuideWithValue(this.setCount().left);
//     },
//     removeAllGuides: function(){
//         // log("Setting instead RemoveAllGuides")
//         // runSettings(this.context)
//
//         // StatisticsInventory.sendTestRequest()
//
//         // var url = [NSURL URLWithString:"https://goo.gl/nQYNuf/?ref=framer_inventory"];
//         // var downloadPhotoTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:nil];
//         // [downloadPhotoTask resume];
//         // StatisticsInventory.sendSimulateRequest()
//         // log("downloadPhotoTask?")
//
//         // var self = this;
//         // horizontalGuideCount = this.artboard.horizontalRulerData().numberOfGuides();
//         // verticalGuideCount = this.artboard.verticalRulerData().numberOfGuides();
//         // while (verticalGuideCount > 0) {
//         //     this.artboard.verticalRulerData().removeGuideAtIndex(0);
//         //     verticalGuideCount = this.artboard.verticalRulerData().numberOfGuides();
//         // }
//         // while (horizontalGuideCount > 0) {
//         //     this.artboard.horizontalRulerData().removeGuideAtIndex(0);
//         //     horizontalGuideCount = this.artboard.horizontalRulerData().numberOfGuides();
//         // }
//     }
// });


// CX.extend({
//     UIBar: function(){
//         log("CB1")
//         var self = this,
//             identifier = "com.tilllur.framer-inventory",
//             threadDictionary = NSThread.mainThread().threadDictionary(),
//             UIBar = threadDictionary[identifier];
//             log("CB2")
//         if(!UIBar){
//             log("CB3")
//             UIBar = NSPanel.alloc().init();
//             UIBar.setStyleMask(NSTitledWindowMask + NSFullSizeContentViewWindowMask);
//             UIBar.setBackgroundColor(NSColor.colorWithRed_green_blue_alpha(0.15, 0.15, 0.15, 1));
//             UIBar.setTitleVisibility(NSWindowTitleHidden);
//             UIBar.setTitlebarAppearsTransparent(true);
//             UIBar.setFrame_display(NSMakeRect(0, 0, 640, 50), false);
//             UIBar.setMovableByWindowBackground(true);
//             UIBar.setHasShadow(true);
//             UIBar.setLevel(NSFloatingWindowLevel);
//             var contentView = UIBar.contentView(),
//                 getImage = function(size, name){
//                     var isRetinaDisplay = (NSScreen.mainScreen().backingScaleFactor() > 1)? true: false;
//                         suffix = (isRetinaDisplay)? "@2x": "";
//                         imageURL = NSURL.fileURLWithPath(localPluginRoot + "/images" + "/uibar/" + name + suffix + ".png"),
//                         image = NSImage.alloc().initWithContentsOfURL(imageURL);
//                     return image
//                 },
//                 addButton = function(rect, name, callAction){
//                     var button = NSButton.alloc().initWithFrame(rect),
//                         image = getImage(rect.size, name);
//
//                     button.setImage(image);
//                     button.setBordered(false);
//                     button.sizeToFit();
//                     button.setButtonType(NSMomentaryChangeButton);
//                     button.setCOSJSTargetFunction(callAction);
//                     button.setAction("callAction:");
//                     return button;
//                 },
//                 addImage = function(rect, name){
//                     var view = NSImageView.alloc().initWithFrame(rect),
//                         image = getImage(rect.size, name);
//                     view.setImage(image);
//                     return view;
//                 },
//
//                 closeButton = addButton( NSMakeRect(20, 10, 30, 30), "close-control",
//                     function(sender){
//                         coscript.setShouldKeepAround(false);
//                         threadDictionary.removeObjectForKey(identifier);
//                         UIBar.close();
//                 }),
//                 topGuideB = addButton( NSMakeRect(100, 10, 30, 30), "top-guide",
//                     function(sender){
//                         self.updateContext();
//                         self.init(self.context, "top-guide");
//                 }),
//                 rightGuideB = addButton( NSMakeRect(150, 10,30,30), "right-guide",
//                     function(sender){
//                         self.updateContext();
//                         self.init(self.context, "right-guide");
//                 }),
//                 bottomGuideB = addButton( NSMakeRect(200, 10,30,30), "bottom-guide",
//                     function(sender){
//                         self.updateContext();
//                         self.init(self.context, "bottom-guide");
//                 }),
//                 leftGuideB = addButton( NSMakeRect(250, 10,30,30), "left-guide",
//                     function(sender){
//                         self.updateContext();
//                         self.init(self.context, "left-guide");
//                 }),
//                 vCenterGuideB = addButton( NSMakeRect(330, 10,30,30), "v-center-guide",
//                     function(sender){
//                         self.updateContext();
//                         self.init(self.context, "v-center-guide");
//                 }),
//                 hCenterGuideB = addButton( NSMakeRect(380, 10,30,30), "h-center-guide",
//                     function(sender){
//                         self.updateContext();
//                         self.init(self.context, "h-center-guide");
//                 }),
//                 topBottomGuides = addButton( NSMakeRect(460,10,30,30),"top-bottom-guides",
//                     function(sender){
//                         self.updateContext();
//                         self.init(self.context, "top-bottom-guides");
//                 }),
//                 rightLeftGuides = addButton ( NSMakeRect(510, 10, 30,30), "right-left-guides",
//                     function(sneder){
//                         self.updateContext();
//                         self.init(self.context, "right-left-guides");
//                 }),
//                 removeAllGuidesB = addButton( NSMakeRect(590, 10,30,30), "remove-all-guides",
//                     function(sender){
//                         self.updateContext();
//                         self.init(self.context, "remove-all-guides");
//                 }),
//                 separate1 = addImage( NSMakeRect(70, 10, 10, 30), "separate"),
//                 separate2 = addImage( NSMakeRect(300, 10, 10, 30), "separate"),
//                 separate3 = addImage( NSMakeRect(430, 10, 10, 30), "separate"),
//                 separate4 = addImage( NSMakeRect(560, 10, 10, 30), "separate");
//
//             contentView.addSubview(closeButton);
//             contentView.addSubview(separate1);
//             contentView.addSubview(topGuideB);
//             contentView.addSubview(rightGuideB);
//             contentView.addSubview(bottomGuideB);
//             contentView.addSubview(leftGuideB);
//             contentView.addSubview(separate2);
//             contentView.addSubview(vCenterGuideB);
//             contentView.addSubview(hCenterGuideB);
//             contentView.addSubview(separate3);
//             contentView.addSubview(topBottomGuides);
//             contentView.addSubview(rightLeftGuides);
//             contentView.addSubview(separate4);
//             contentView.addSubview(removeAllGuidesB);
//             threadDictionary[identifier] = UIBar;
//             UIBar.center();
//             UIBar.makeKeyAndOrderFront(nil);
//         }
//     }
// });


// ToolbarInventory.updateContext = function() {
//     currentDocument = NSDocumentController.sharedDocumentController().currentDocument();
//     this.context.document = NSDocumentController.sharedDocumentController().currentDocument();
//     return this.context;
// }

ToolbarInventory.updateContext = function() {
    methodStartTime = [NSDate date]

    var importViews = ViewInventory.createImportPanel()
    this.contentViewImport = importViews[0]
    this.pathLabel = importViews[1]
    this.selectionLabel = importViews[2]
    this.layersButton = importViews[3]
    this.statesButton = importViews[4]
    this.relativeButton = importViews[5]
    this.deviceAccessory = importViews[6]
    this.moreButtonImage = importViews[7]
    this.moreButton = importViews[8]
    this.emptySelectionView = importViews[9]
    this.expiredSelectionView = importViews[10]


    var sceneView = ViewInventory.createScenePanel()
    this.contentViewScene = sceneView[0]
    this.pathLabelScene = sceneView[1]
    this.selectionLabelScene = sceneView[2]
    this.deviceAccessoryScene = sceneView[3]
    this.sceneButton = sceneView[4]
    this.expiredSceneView = sceneView[5]

}




ToolbarInventory.prototype.returnPanel = function() {
    return this.panel
}


ToolbarInventory.setContentView = function() {
    var toolbar = ToolbarInventory.returnInstance()
    if (toolbar != nil) {
      var panel = toolbar.panel
      if (userDefaults.exportFramerFolder == "") {
        var view = toolbar.contentViewEmpty
        [panel setContentView:view]
      }
      else {
        if (panel.contentView() == toolbar.contentViewScene) {
          var view = toolbar.contentViewScene
          [panel setContentView:view]
        }
        else {
          var view = toolbar.contentViewImport
          [panel setContentView:view]
          SelectionInventory.asyncSelectionHandler(false)
        }
        ToolbarInventory.updateAccessoryControls();
      }
      ToolbarInventory.updatePathLabelStringValue()
    }
}

ToolbarInventory.changeContentView = function() {
    if (userDefaults.exportFramerFolder == "") { return }

    var toolbar = ToolbarInventory.returnInstance()
    if (toolbar != nil) {
      var panel = toolbar.panel

      if (panel.contentView() == toolbar.contentViewScene) {
        var view = toolbar.contentViewImport
        [panel setContentView:view]
      }
      else {
        var view = toolbar.contentViewScene
        [panel setContentView:view]
      }
      SelectionInventory.asyncSelectionHandler(false)
    }
}


ToolbarInventory.toolbarMode = function() {
      var toolbar = ToolbarInventory.returnInstance()
      if (toolbar != nil) {
          var panel = toolbar.panel
          if (panel.contentView() == toolbar.contentViewScene) { return Status.toolbarModeScene() }
          else if (panel.contentView() == toolbar.contentViewImport) { return Status.toolbarModeImport() }
          else { return Status.toolbarModeEmpty() }
      }
}


ToolbarInventory.removeInstance = function() {
    var threadIdentifier = "com.tilllur.framer-inventory"
    var threadDictionary = NSThread.mainThread().threadDictionary()
    threadDictionary.removeObjectForKey(threadIdentifier);
}

ToolbarInventory.returnInstance = function() {
    var threadIdentifier = "com.tilllur.framer-inventory"
    var threadDictionary = NSThread.mainThread().threadDictionary()
    return threadDictionary[threadIdentifier]
}

ToolbarInventory.updateClassInstance = function(toolbar) {
    if (toolbar != nil) { log(toolbar.returnPanel()) }
    var threadIdentifier = "com.tilllur.framer-inventory"
    var threadDictionary = NSThread.mainThread().threadDictionary()
                ToolbarInventory.updatePathLabel()
        })

        var relativeButtonType = (userDefaults.myRelativeGroup == 0) ? "relative/artboard" : "relative/group"
        topBottomGuides = ToolbarInventory.addButton( NSMakeRect(226, 6, 58, 26), relativeButtonType,
            function(sender){
                ToolbarInventory.updateContext();
                userDefaults.myRelativeGroup = (userDefaults.myRelativeGroup == 0) ? 1 : 0
                saveDefaults(userDefaults)
                ToolbarInventory.changeRelativeButton()

        })
        var rightLeftGuides = ToolbarInventory.addButton ( NSMakeRect(292, 6, 26, 26), "density",
            function(sneder){
                ToolbarInventory.updateContext();
                // ToolbarInventory.nextLayerState(false)
        })
        var removeAllGuidesB = ToolbarInventory.addButton( NSMakeRect(326, 6, 104, 26), "device",
            function(sender){
                ToolbarInventory.updateContext();
        })

        // var separate1 = ToolbarInventory.addImage( NSMakeRect(70, 10, 10, 30), "separate")
        // var separate2 = ToolbarInventory.addImage( NSMakeRect(300, 10, 10, 30), "separate")

      if (adoptID == 7) {
        ViewInventory.adoptMoreNone(toolbar.moreButtonImage, toolbar.moreButton)
        ViewInventory.adoptEmptySelectionNone(toolbar.emptySelectionView)
        ViewInventory.adoptExpiredSelection(toolbar.expiredSelectionView)
        ViewInventory.adoptButtonsNone(toolbar.layersButton, toolbar.statesButton, toolbar.images)
      }
      else {
        ViewInventory.adoptExpiredSelectionNone(toolbar.expiredSelectionView)
      }
  }
}

ToolbarInventory.updateSceneButtons = function(adoptID) {
    var toolbar = ToolbarInventory.returnInstance()
    if (toolbar != nil) {
        if (adoptID == 5) { ViewInventory.adoptSceneButtons(toolbar.sceneButton, toolbar.images) }
        else if (adoptID == 6) { ViewInventory.adoptSceneButtonsSelected(toolbar.sceneButton, toolbar.images) }

        if (adoptID == 7) {
          // log("adoptation")
          ViewInventory.adoptExpiredScene(toolbar.expiredSceneView)
          ToolbarInventory.updateSceneLabelStringValue("")
        }
        contentView.addSubview(vCenterGuideB);
        contentView.addSubview(hCenterGuideB);
        // contentView.addSubview(separate3);
        contentView.addSubview(topBottomGuides);
        contentView.addSubview(rightLeftGuides);
        // contentView.addSubview(separate4);
        contentView.addSubview(removeAllGuidesB);

        contentView.addSubview(pathLabel);
        contentView.addSubview(leftGuideB);
      currentSelection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers().layers();
      scaleInstance = new ScaleInventory()
    }
}









ToolbarInventory.createUIBar = function() {

    StatisticsInventory.setStatus(Status.waitingID())

    coscript.setShouldKeepAround(true);
    var toolbar = ToolbarInventory.returnInstance()

    if(!toolbar){

        var UIBar = NSPanel.alloc().init();
        UIBar.setStyleMask(NSTitledWindowMask + NSFullSizeContentViewWindowMask);
        UIBar.setBackgroundColor(ViewInventory.colorWindow())
        UIBar.setTitleVisibility(NSWindowTitleHidden);
        UIBar.setTitlebarAppearsTransparent(true);
        UIBar.setFrame_display(NSMakeRect(0, 0, ViewInventory.sizeWindowWidth(), ViewInventory.sizeWindowHeight()), false);
        UIBar.setMovableByWindowBackground(true);
        UIBar.setHasShadow(true);
        UIBar.setLevel(NSFloatingWindowLevel);


        toolbar = new ToolbarInventory(UIBar);
        ToolbarInventory.updateClassInstance(toolbar)
        ToolbarInventory.setContentView()

        UIBar.center()
        UIBar.makeKeyAndOrderFront(nil)

        ViewInventory.adoptButtonsNone(toolbar.layersButton, toolbar.statesButton, toolbar.images)
        SelectionInventory.asyncSelectionHandler(false)
    }
}



ToolbarInventory.changeRelativeButton = function() {
    if (topBottomGuides != nil) {
      log("SWitch")

<MSG> Added NSPopUpButtons to UI

– Added some magic to choose from UIBar
– created a temporary action to select device

TODO: remove temporary action

<DFF> @@ -6,443 +6,6 @@ var defaultPathLabel = "Please, select Framer folder"
 
 function ToolbarInventory () {}
 
-// UIBar.init = function(context, command){
-//     this.context = context;
-//     this.extend(context);
-//     this.pluginRoot = this.scriptPath
-//             .stringByDeletingLastPathComponent()
-//             .stringByDeletingLastPathComponent()
-//             .stringByDeletingLastPathComponent();
-//     // this.pluginSketch = this.pluginRoot + "/Contents/Sketch/library";
-//     localPluginRoot = this.pluginRoot + "/Contents/Sketch/";
-//
-//     this.doc = context.document;
-//     this.docData = this.doc.documentData();
-//     this.page = this.doc.currentPage();
-//     this.artboard = this.page.currentArtboard();
-//     this.current = this.artboard || this.page;
-//     // coscript.setShouldKeepAround(true);
-//
-//     if(command && command == "uibar"){
-//         UIBar.createUIBar();
-//         return false;
-//     }
-//     if(command){
-//         switch(command){
-//             case "top-guide":
-//                 // this.topGuide();
-//                 onSimulateKeynoteRun(this.context)
-//                 break;
-//             case "right-guide":
-//                 // this.rightGuide();
-//                 onGenerateStatesRun(this.context)
-//                 break;
-//             case "bottom-guide":
-//                 // this.bottomGuide();
-//                 onReplicateLayersRun(this.context)
-//                 break;
-//             case "left-guide":
-//                 // this.leftGuide();
-//                 var localPath = pluginPath + "/manifest.json"
-//                 log("generating path" + localPath)
-//                 var manifestText = readTextFromFile(localPath)
-//                 log(manifestText)
-//                 writeTextToFile(manifestText, pluginPath + "/manifest2.json")
-//
-//                 break;
-//             case "v-center-guide":
-//                 // this.vCenterGuide();
-//                 onSelectProjectFolderRun(this.context)
-//                 break;
-//             case "h-center-guide":
-//                 // this.hCenterGuide();
-//                 onRemoveProjectFolderRun(this.context)
-//                 break;
-//             case "top-bottom-guides":
-//                 // this.topBottomGuides();
-//                 break;
-//             case "right-left-guides":
-//                 // this.rightLeftGuides();
-//                 break;
-//             case "remove-all-guides":
-//                 // this.removeAllGuides();
-//                 break;
-//         }
-//     }
-//
-// }
-//
-// UIBar.extend = function(options, target) {
-//     var target = target || this;
-//     for (var key in options) {
-//         target[key] = options[key];
-//     }
-//     return target;
-// }
-
-
-
-
-// var CX = {
-//     init: function(context, command){
-//         this.context = context;
-//         this.extend(context);
-//         this.pluginRoot = this.scriptPath
-//                 .stringByDeletingLastPathComponent()
-//                 .stringByDeletingLastPathComponent()
-//                 .stringByDeletingLastPathComponent();
-//         // this.pluginSketch = this.pluginRoot + "/Contents/Sketch/library";
-//         localPluginRoot = this.pluginRoot + "/Contents/Sketch/";
-//
-//         this.doc = context.document;
-//         this.docData = this.doc.documentData();
-//         this.page = this.doc.currentPage();
-//         this.artboard = this.page.currentArtboard();
-//         this.current = this.artboard || this.page;
-//         coscript.setShouldKeepAround(true);
-//
-//         if(command && command == "uibar"){
-//             this.UIBar();
-//             return false;
-//         }
-//         if(command){
-//             switch(command){
-//                 case "top-guide":
-//                     // this.topGuide();
-//                     onSimulateKeynoteRun(this.context)
-//                     break;
-//                 case "right-guide":
-//                     // this.rightGuide();
-//                     onGenerateStatesRun(this.context)
-//                     break;
-//                 case "bottom-guide":
-//                     // this.bottomGuide();
-//                     onReplicateLayersRun(this.context)
-//                     break;
-//                 case "left-guide":
-//                     // this.leftGuide();
-//                     var localPath = pluginPath + "/manifest.json"
-//                     log("generating path" + localPath)
-//                     var manifestText = readTextFromFile(localPath)
-//                     log(manifestText)
-//                     writeTextToFile(manifestText, pluginPath + "/manifest2.json")
-//
-//                     break;
-//                 case "v-center-guide":
-//                     // this.vCenterGuide();
-//                     onSelectProjectFolderRun(this.context)
-//                     break;
-//                 case "h-center-guide":
-//                     // this.hCenterGuide();
-//                     onRemoveProjectFolderRun(this.context)
-//                     break;
-//                 case "top-bottom-guides":
-//                     // this.topBottomGuides();
-//                     break;
-//                 case "right-left-guides":
-//                     // this.rightLeftGuides();
-//                     break;
-//                 case "remove-all-guides":
-//                     // this.removeAllGuides();
-//                     break;
-//             }
-//         }
-//
-//     },
-//     extend: function( options, target ) {
-//         var target = target || this;
-//         for ( var key in options ){
-//             target[key] = options[key];
-//         }
-//         return target;
-//     }
-// };
-
-
-// CX.extend({
-//     // getRect: function(layer){
-//     //     var rect = layer.absoluteRect();
-//     //     return {
-//     //         x: Math.round(rect.x()),
-//     //         y: Math.round(rect.y()),
-//     //         width: Math.round(rect.width()),
-//     //         height: Math.round(rect.height()),
-//     //         maxX: Math.round(rect.x() + rect.width()),
-//     //         maxY: Math.round(rect.y() + rect.height()),
-//     //     };
-//     // },
-//     updateContext: function(){
-//         currentDocument = NSDocumentController.sharedDocumentController().currentDocument();
-//         this.context.document = NSDocumentController.sharedDocumentController().currentDocument();
-//         // this.context.selection = this.context.document.selectedLayers();
-//         return this.context;
-//     },
-// });
-//
-// CX.extend({
-//     getDistance: function(targetRect, containerRect){
-//         var containerRect = containerRect || this.getRect(this.current);
-//         return {
-//             top: (targetRect.y - containerRect.y),
-//             right: ((targetRect.x - containerRect.x) + targetRect.width),
-//             bottom: ((targetRect.y - containerRect.y) + targetRect.height),
-//             left:(targetRect.x - containerRect.x),
-//             vCenter:((targetRect.x - containerRect.x) + (targetRect.width / 2)),
-//             hCenter:((targetRect.y - containerRect.y) + (targetRect.height / 2)),
-//         };
-//     },
-//     message: function(message){
-//         this.doc.showMessage(message);
-//     }
-// });
-
-
-// CX.extend({
-//     selectError: function(){
-//         var self = this,
-//             selection = this.selection,
-//             inArtboard = false;
-//
-//         if( selection.count() <= 0 ){
-//             this.message("Select an element.")
-//             return false;
-//         }
-//         if(selection.count() == 2){
-//             this.message("Can't select mutiple element.")
-//             return false;
-//         }
-//         if(inArtboard || !this.artboard){
-//             this.message("Element should be inside Artboard")
-//             return false;
-//         }
-//         return true;
-//     },
-//     setCount: function(gain){
-//         var self = this,
-//             selection = this.selection,
-//             layer = selection.firstObject();
-//         var targetRect = this.getRect(layer),
-//             containerRect = this.getRect(this.artboard),
-//             eleCount = this.getDistance(targetRect, containerRect);
-//         return eleCount;
-//     }
-// });
-
-
-// CX.extend({
-//     topGuide: function(){
-//       // runSimulateKeynote(this.context)
-//       onSimulateKeynoteRun(this.context)
-//         // var self = this;
-//         // if(!this.selectError()) return;
-//         // this.artboard.verticalRulerData().addGuideWithValue(this.setCount().top);
-//     },
-//     rightGuide: function(){
-//         onGenerateStatesRun(this.context)
-//         // var self = this;
-//         // if(!this.selectError()) return;
-//         // this.artboard.horizontalRulerData().addGuideWithValue(this.setCount().right);
-//     },
-//     bottomGuide: function(){
-//         onReplicateLayersRun(this.context)
-//         // var self = this;
-//         // if(!this.selectError()) return;
-//         // this.artboard.verticalRulerData().addGuideWithValue(this.setCount().bottom);
-//     },
-//     leftGuide: function(){
-//         var localPath = pluginPath + "/manifest.json"
-//         log("generating path" + localPath)
-//         var manifestText = readTextFromFile(localPath)
-//         log(manifestText)
-//         writeTextToFile(manifestText, pluginPath + "/manifest2.json")
-//         // var self = this;
-//         // if(!this.selectError()) return;
-//         // this.artboard.horizontalRulerData().addGuideWithValue(this.setCount().left);
-//     },
-//     vCenterGuide: function() {
-//         onSelectProjectFolderRun(this.context)
-//         // log("URL")
-//         // var url = [NSURL URLWithString:"https://bit.ly/2i1Usqq"]
-//
-//
-//         // var self = this;
-//         // if(!this.selectError()) return;
-//         // this.artboard.horizontalRulerData().addGuideWithValue(this.setCount().vCenter);
-//     },
-//     hCenterGuide: function(){
-//         onRemoveProjectFolderRun(this.context)
-//         // var self = this;
-//         // if(!this.selectError()) return;
-//         // this.artboard.verticalRulerData().addGuideWithValue(this.setCount().hCenter);
-//     },
-//     topBottomGuides: function(){
-//         // var self = this;
-//         // if(!this.selectError()) return;
-//         // this.artboard.verticalRulerData().addGuideWithValue(this.setCount().top);
-//         // this.artboard.verticalRulerData().addGuideWithValue(this.setCount().bottom);
-//     },
-//     rightLeftGuides: function(){
-//         // var self = this;
-//         // if(!this.selectError()) return;
-//         // this.artboard.horizontalRulerData().addGuideWithValue(this.setCount().right);
-//         // this.artboard.horizontalRulerData().addGuideWithValue(this.setCount().left);
-//     },
-//     removeAllGuides: function(){
-//         // log("Setting instead RemoveAllGuides")
-//         // runSettings(this.context)
-//
-//         // StatisticsInventory.sendTestRequest()
-//
-//         // var url = [NSURL URLWithString:"https://goo.gl/nQYNuf/?ref=framer_inventory"];
-//         // var downloadPhotoTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:nil];
-//         // [downloadPhotoTask resume];
-//         // StatisticsInventory.sendSimulateRequest()
-//         // log("downloadPhotoTask?")
-//
-//         // var self = this;
-//         // horizontalGuideCount = this.artboard.horizontalRulerData().numberOfGuides();
-//         // verticalGuideCount = this.artboard.verticalRulerData().numberOfGuides();
-//         // while (verticalGuideCount > 0) {
-//         //     this.artboard.verticalRulerData().removeGuideAtIndex(0);
-//         //     verticalGuideCount = this.artboard.verticalRulerData().numberOfGuides();
-//         // }
-//         // while (horizontalGuideCount > 0) {
-//         //     this.artboard.horizontalRulerData().removeGuideAtIndex(0);
-//         //     horizontalGuideCount = this.artboard.horizontalRulerData().numberOfGuides();
-//         // }
-//     }
-// });
-
-
-// CX.extend({
-//     UIBar: function(){
-//         log("CB1")
-//         var self = this,
-//             identifier = "com.tilllur.framer-inventory",
-//             threadDictionary = NSThread.mainThread().threadDictionary(),
-//             UIBar = threadDictionary[identifier];
-//             log("CB2")
-//         if(!UIBar){
-//             log("CB3")
-//             UIBar = NSPanel.alloc().init();
-//             UIBar.setStyleMask(NSTitledWindowMask + NSFullSizeContentViewWindowMask);
-//             UIBar.setBackgroundColor(NSColor.colorWithRed_green_blue_alpha(0.15, 0.15, 0.15, 1));
-//             UIBar.setTitleVisibility(NSWindowTitleHidden);
-//             UIBar.setTitlebarAppearsTransparent(true);
-//             UIBar.setFrame_display(NSMakeRect(0, 0, 640, 50), false);
-//             UIBar.setMovableByWindowBackground(true);
-//             UIBar.setHasShadow(true);
-//             UIBar.setLevel(NSFloatingWindowLevel);
-//             var contentView = UIBar.contentView(),
-//                 getImage = function(size, name){
-//                     var isRetinaDisplay = (NSScreen.mainScreen().backingScaleFactor() > 1)? true: false;
-//                         suffix = (isRetinaDisplay)? "@2x": "";
-//                         imageURL = NSURL.fileURLWithPath(localPluginRoot + "/images" + "/uibar/" + name + suffix + ".png"),
-//                         image = NSImage.alloc().initWithContentsOfURL(imageURL);
-//                     return image
-//                 },
-//                 addButton = function(rect, name, callAction){
-//                     var button = NSButton.alloc().initWithFrame(rect),
-//                         image = getImage(rect.size, name);
-//
-//                     button.setImage(image);
-//                     button.setBordered(false);
-//                     button.sizeToFit();
-//                     button.setButtonType(NSMomentaryChangeButton);
-//                     button.setCOSJSTargetFunction(callAction);
-//                     button.setAction("callAction:");
-//                     return button;
-//                 },
-//                 addImage = function(rect, name){
-//                     var view = NSImageView.alloc().initWithFrame(rect),
-//                         image = getImage(rect.size, name);
-//                     view.setImage(image);
-//                     return view;
-//                 },
-//
-//                 closeButton = addButton( NSMakeRect(20, 10, 30, 30), "close-control",
-//                     function(sender){
-//                         coscript.setShouldKeepAround(false);
-//                         threadDictionary.removeObjectForKey(identifier);
-//                         UIBar.close();
-//                 }),
-//                 topGuideB = addButton( NSMakeRect(100, 10, 30, 30), "top-guide",
-//                     function(sender){
-//                         self.updateContext();
-//                         self.init(self.context, "top-guide");
-//                 }),
-//                 rightGuideB = addButton( NSMakeRect(150, 10,30,30), "right-guide",
-//                     function(sender){
-//                         self.updateContext();
-//                         self.init(self.context, "right-guide");
-//                 }),
-//                 bottomGuideB = addButton( NSMakeRect(200, 10,30,30), "bottom-guide",
-//                     function(sender){
-//                         self.updateContext();
-//                         self.init(self.context, "bottom-guide");
-//                 }),
-//                 leftGuideB = addButton( NSMakeRect(250, 10,30,30), "left-guide",
-//                     function(sender){
-//                         self.updateContext();
-//                         self.init(self.context, "left-guide");
-//                 }),
-//                 vCenterGuideB = addButton( NSMakeRect(330, 10,30,30), "v-center-guide",
-//                     function(sender){
-//                         self.updateContext();
-//                         self.init(self.context, "v-center-guide");
-//                 }),
-//                 hCenterGuideB = addButton( NSMakeRect(380, 10,30,30), "h-center-guide",
-//                     function(sender){
-//                         self.updateContext();
-//                         self.init(self.context, "h-center-guide");
-//                 }),
-//                 topBottomGuides = addButton( NSMakeRect(460,10,30,30),"top-bottom-guides",
-//                     function(sender){
-//                         self.updateContext();
-//                         self.init(self.context, "top-bottom-guides");
-//                 }),
-//                 rightLeftGuides = addButton ( NSMakeRect(510, 10, 30,30), "right-left-guides",
-//                     function(sneder){
-//                         self.updateContext();
-//                         self.init(self.context, "right-left-guides");
-//                 }),
-//                 removeAllGuidesB = addButton( NSMakeRect(590, 10,30,30), "remove-all-guides",
-//                     function(sender){
-//                         self.updateContext();
-//                         self.init(self.context, "remove-all-guides");
-//                 }),
-//                 separate1 = addImage( NSMakeRect(70, 10, 10, 30), "separate"),
-//                 separate2 = addImage( NSMakeRect(300, 10, 10, 30), "separate"),
-//                 separate3 = addImage( NSMakeRect(430, 10, 10, 30), "separate"),
-//                 separate4 = addImage( NSMakeRect(560, 10, 10, 30), "separate");
-//
-//             contentView.addSubview(closeButton);
-//             contentView.addSubview(separate1);
-//             contentView.addSubview(topGuideB);
-//             contentView.addSubview(rightGuideB);
-//             contentView.addSubview(bottomGuideB);
-//             contentView.addSubview(leftGuideB);
-//             contentView.addSubview(separate2);
-//             contentView.addSubview(vCenterGuideB);
-//             contentView.addSubview(hCenterGuideB);
-//             contentView.addSubview(separate3);
-//             contentView.addSubview(topBottomGuides);
-//             contentView.addSubview(rightLeftGuides);
-//             contentView.addSubview(separate4);
-//             contentView.addSubview(removeAllGuidesB);
-//             threadDictionary[identifier] = UIBar;
-//             UIBar.center();
-//             UIBar.makeKeyAndOrderFront(nil);
-//         }
-//     }
-// });
-
-
-// ToolbarInventory.updateContext = function() {
-//     currentDocument = NSDocumentController.sharedDocumentController().currentDocument();
-//     this.context.document = NSDocumentController.sharedDocumentController().currentDocument();
-//     return this.context;
-// }
 
 ToolbarInventory.updateContext = function() {
     methodStartTime = [NSDate date]
@@ -553,24 +116,76 @@ ToolbarInventory.createUIBar = function() {
                 ToolbarInventory.updatePathLabel()
         })
 
-        var relativeButtonType = (userDefaults.myRelativeGroup == 0) ? "relative/artboard" : "relative/group"
-        topBottomGuides = ToolbarInventory.addButton( NSMakeRect(226, 6, 58, 26), relativeButtonType,
-            function(sender){
-                ToolbarInventory.updateContext();
-                userDefaults.myRelativeGroup = (userDefaults.myRelativeGroup == 0) ? 1 : 0
-                saveDefaults(userDefaults)
-                ToolbarInventory.changeRelativeButton()
+        // var relativeButtonType = (userDefaults.myRelativeGroup == 0) ? "relative/artboard" : "relative/group"
+        // topBottomGuides = ToolbarInventory.addButton( NSMakeRect(226, 6, 58, 26), relativeButtonType,
+        //     function(sender){
+        //         ToolbarInventory.updateContext();
+        //         userDefaults.myRelativeGroup = (userDefaults.myRelativeGroup == 0) ? 1 : 0
+        //         saveDefaults(userDefaults)
+        //         ToolbarInventory.changeRelativeButton()
+        //
+        // })
+        // var rightLeftGuides = ToolbarInventory.addButton ( NSMakeRect(292, 6, 26, 26), "density",
+        //     function(sneder){
+        //         ToolbarInventory.updateContext();
+        //         // ToolbarInventory.nextLayerState(false)
+        // })
+
+
+        // NSShadowlessSquareBezelStyle
+        var accessoryFontSize = 10
+
+        var relativeAccessory = NSPopUpButton.alloc().initWithFrame(NSMakeRect(340-64-80, 6, 80, 28))
+        [[relativeAccessory cell] setBezelStyle:NSTexturedRoundedBezelStyle];
+        // [relativeAccessory setBordered:false];
+        // [[relativeAccessory cell] setArrowPosition:NSPopUpNoArrow];
+        var densityValues = ["Artboard", "Group"];
+        relativeAccessory.addItemsWithTitles(densityValues)
+      	relativeAccessory.selectItemAtIndex(userDefaults.myRelativeGroup)
+      	[relativeAccessory setFont:[NSFont boldSystemFontOfSize:accessoryFontSize]];
+
+        relativeAccessory.setCOSJSTargetFunction(function(sender){
+            userDefaults.myRelativeGroup = relativeAccessory.indexOfSelectedItem()
+            saveDefaults(userDefaults)
+            log(userDefaults.myRelativeGroup)
+      	})
+
+        var densityAccessory = NSPopUpButton.alloc().initWithFrame(NSMakeRect(340-64, 6, 64, 28))
+        [[densityAccessory cell] setBezelStyle:NSTexturedRoundedBezelStyle];
+        // [densityAccessory setBordered:false];
+        // [[densityAccessory cell] setArrowPosition:NSPopUpNoArrow];
+        var densityValues = ["Pixels", "Points"];
+        densityAccessory.addItemsWithTitles(densityValues)
+      	densityAccessory.selectItemAtIndex(userDefaults.myRetinaEnabled)
+      	[densityAccessory setFont:[NSFont boldSystemFontOfSize:accessoryFontSize]];
+
+        densityAccessory.setCOSJSTargetFunction(function(sender){
+      		  userDefaults.myRetinaEnabled = densityAccessory.indexOfSelectedItem()
+            saveDefaults(userDefaults)
+            log(userDefaults.myRetinaEnabled)
+      	})
+
+
+        var deviceAccessory = NSPopUpButton.alloc().initWithFrame(NSMakeRect(340, 6, 90, 28))
+        [[deviceAccessory cell] setBezelStyle:NSTexturedRoundedBezelStyle];
+        // [deviceAccessory setBordered:false];
+        // [[deviceAccessory cell] setArrowPosition:NSPopUpNoArrow];
+        var devices = [["Canvas", "Canvas @2x"], ["iPhone 5S", "iPhone 7", "iPhone 7 Plus"], ["Nexus 5P", "Nexus 6X"]]
+        for (var i = 0; i < devices.length; i++) {
+            deviceAccessory.addItemsWithTitles(devices[i])
+            if (i != devices.length - 1) { [[deviceAccessory menu] addItem:[NSMenuItem separatorItem]] }
+        }
+      	deviceAccessory.selectItemAtIndex(0)
+      	[deviceAccessory setFont:[NSFont boldSystemFontOfSize:accessoryFontSize]];
+
+        deviceAccessory.setCOSJSTargetFunction(function(sender){
+      		  log("REALLY")
+            log(deviceAccessory.indexOfSelectedItem())
+      	})
+
+
+
 
-        })
-        var rightLeftGuides = ToolbarInventory.addButton ( NSMakeRect(292, 6, 26, 26), "density",
-            function(sneder){
-                ToolbarInventory.updateContext();
-                // ToolbarInventory.nextLayerState(false)
-        })
-        var removeAllGuidesB = ToolbarInventory.addButton( NSMakeRect(326, 6, 104, 26), "device",
-            function(sender){
-                ToolbarInventory.updateContext();
-        })
 
         // var separate1 = ToolbarInventory.addImage( NSMakeRect(70, 10, 10, 30), "separate")
         // var separate2 = ToolbarInventory.addImage( NSMakeRect(300, 10, 10, 30), "separate")
@@ -601,10 +216,12 @@ ToolbarInventory.createUIBar = function() {
         contentView.addSubview(vCenterGuideB);
         contentView.addSubview(hCenterGuideB);
         // contentView.addSubview(separate3);
-        contentView.addSubview(topBottomGuides);
-        contentView.addSubview(rightLeftGuides);
+        // contentView.addSubview(topBottomGuides);
+        // contentView.addSubview(rightLeftGuides);
         // contentView.addSubview(separate4);
-        contentView.addSubview(removeAllGuidesB);
+        contentView.addSubview(deviceAccessory);
+        contentView.addSubview(densityAccessory);
+        contentView.addSubview(relativeAccessory);
 
         contentView.addSubview(pathLabel);
         contentView.addSubview(leftGuideB);
@@ -678,6 +295,8 @@ ToolbarInventory.createUIBar = function() {
 
 
 
+
+
 ToolbarInventory.changeRelativeButton = function() {
     if (topBottomGuides != nil) {
       log("SWitch")

Added NSPopUpButtons to UI