<NME> selection-changed-action.cocoascript
<BEF> // var onSelectionChanged = function(context) {
//
//     action = context.actionContext;
//
//     document = action.document;
//     selection = action.newSelection;
//
//     count = selection.count();
//     if (count == 0) {
//         document.hideMessage();
//     } else {
//         log("here")
//         log(selectionLabel)
//
//         if (count == 1) {
//             message = "1 layer selected."
//         } else {
//             message = count + " layers selected."
//         }
//         document.showMessage(message);
//     }
//
// };

@import 'actions/action-inventory.cocoascript'
@import 'actions/view-inventory.cocoascript'
@import 'lib.js'

// var testThreadIdentifier = "com.tilllur.framer-inventory.testing"
// function TestObject (name) {
//   this.name = name
//   this.panel = NSPanel.alloc().init();
// }

var onSelectionChanged = function(context) {

function SelectionInventory(type) {
    this.type = type
    this.artboards = NSMutableArray.new()
    this.layers = NSMutableArray.new()
}

function SelectionInventory(type, artboards) {
    this.type = type
    this.artboards = NSMutableArray.new()
    this.layers = NSMutableArray.new()
    // First argument is a delay in seconds.
    coscript.scheduleWithInterval_jsFunction(0.4,function() {
        var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
        ToolbarInventory.updateSelectionLabelStringValue(selection.count() + "")

        // if (testObject != nil) {
        //     // var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
    if (artboards != nil) { this.artboards = artboards}
    if (layers != nil) { this.layers = layers}
}





var onSelectionChanged = function(context) {
  var asyncing = true
  coscript.setShouldKeepAround(true);
  coscript.scheduleWithInterval_jsFunction(0.05, SelectionInventory.asyncSelectionHandler(asyncing));
};


SelectionInventory.asyncSelectionHandler = function(asyncing) {
    var localTime = [NSDate date]
    currentDocument = NSDocumentController.sharedDocumentController().currentDocument()

    if (currentDocument == nil) {
      if (asyncing) { coscript.setShouldKeepAround(false); }
      return
    }

    var localSelection = currentDocument.selectedLayers().layers();
    var selectionInventoryInstance = SelectionInventory.analyseActionType(localSelection)
    // log(selectionInventoryInstance)
    // var currentSelectionType = selectionInventoryInstance.type
    // var selectionTitle = ""
    // if (currentSelectionType == 0) { selectionTitle = "Select Layers" }
    // else if (currentSelectionType == 1) { selectionTitle = "Layer / All States" }
    // else if (currentSelectionType == 2) { selectionTitle = "Selected States" }
    // else if (currentSelectionType == 3) { selectionTitle = "Layers / All States for Layers" }
    // else if (currentSelectionType == 4) { selectionTitle = "Selected States for Layers" }
    // log("a: " + [[NSDate date] timeIntervalSinceDate:localTime])

    var localMode = ToolbarInventory.toolbarMode()

    if (localMode == Status.toolbarModeImport()) {
      ToolbarInventory.updateImportButtons(selectionInventoryInstance.type)
      if (selectionInventoryInstance.type == 7) { ToolbarInventory.updateSceneButtons(selectionInventoryInstance.type) }

      var localLayers = selectionInventoryInstance.layers
      if (selectionInventoryInstance.type == 2) { ToolbarInventory.updateSelectionLabelStringValue(localLayers.firstObject().name()) }
      // else if (localLayers.count() == 2) { ToolbarInventory.updateSelectionLabelStringValue(localLayers.firstObject().name() + ", " + localLayers.objectAtIndex(1).name()) }
      else if (localLayers.count() == 1) { ToolbarInventory.updateSelectionLabelStringValue(localLayers.firstObject().name()) }
      else if (localLayers.count() > 0) { ToolbarInventory.updateSelectionLabelStringValue(localLayers.count() + " layers") }
      else { ToolbarInventory.updateSelectionLabelStringValue(ViewInventory.placeHolderImport()) }
    }
    else if (localMode == Status.toolbarModeScene()) {
        // log("Found correct action: " + selectionInventoryInstance.type)
        ToolbarInventory.updateSceneButtons(selectionInventoryInstance.type)
    }


    // StatisticsInventory.setStatus(2)
    // log("b: " + [[NSDate date] timeIntervalSinceDate:localTime])
    if (asyncing) { coscript.setShouldKeepAround(false); }
}

// SelectionInventory.selectionChangedHandler = function() {
//
// }


// 0 – Select layers
// 1 – layer / all states
// 2 – selected states
// 3 – layers / all states for layers
// 4 – selected states for layers
// 5 – Scene
// 6 – Scene for selected artboards
// 7 – Expired

SelectionInventory.analyseActionType = function(selection) {
    if (!DemoInventory.isRegistered() && DemoInventory.actionsLeft() <= 0) { return new SelectionInventory(7) }

    if (selection == nil) {
        if (showLog) { log("Warning. Selection is nil") }
        return new SelectionInventory(0)
    }

    var localLayers = NSMutableArray.new()
    var localArtboards = NSMutableArray.new()
    var hasParentArtboard = false

    for (var i = 0; i < selection.count(); i++) {
    //   log(UIBar)
		// }
};

<MSG> Added action type detection

– now we check current selection on the fly and can suggest a proper
action

TODO: now we shown change the way to returnArtboardsScope

<DFF> @@ -1,36 +1,7 @@
-// var onSelectionChanged = function(context) {
-//
-//     action = context.actionContext;
-//
-//     document = action.document;
-//     selection = action.newSelection;
-//
-//     count = selection.count();
-//     if (count == 0) {
-//         document.hideMessage();
-//     } else {
-//         log("here")
-//         log(selectionLabel)
-//
-//         if (count == 1) {
-//             message = "1 layer selected."
-//         } else {
-//             message = count + " layers selected."
-//         }
-//         document.showMessage(message);
-//     }
-//
-// };
-
 @import 'actions/action-inventory.cocoascript'
 @import 'actions/view-inventory.cocoascript'
 @import 'lib.js'
 
-// var testThreadIdentifier = "com.tilllur.framer-inventory.testing"
-// function TestObject (name) {
-//   this.name = name
-//   this.panel = NSPanel.alloc().init();
-// }
 
 var onSelectionChanged = function(context) {
 
@@ -47,7 +18,9 @@ var onSelectionChanged = function(context) {
     // First argument is a delay in seconds.
     coscript.scheduleWithInterval_jsFunction(0.4,function() {
         var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
-        ToolbarInventory.updateSelectionLabelStringValue(selection.count() + "")
+
+
+        ToolbarInventory.updateSelectionLabelStringValue(analyseActionType(selection) + "")
 
         // if (testObject != nil) {
         //     // var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
@@ -141,3 +114,66 @@ var onSelectionChanged = function(context) {
     //   log(UIBar)
 		// }
 };
+
+
+// 0 – Select layers
+// 1 – layer / all states
+// 2 – selected states
+// 3 – layers / all states for layers
+// 4 – selected states for layers
+
+var analyseActionType = function(selection) {
+    var localLayers = NSMutableArray.new()
+    var localArtboards = NSMutableArray.new()
+
+    for (var i = 0; i < selection.count(); i++) {
+        var currentLayerClassName = [[selection objectAtIndex:i] className]
+        if (TypeInventory.isArtboardType(currentLayerClassName)) { localArtboards.addObject([selection objectAtIndex:i]) }
+        else { localLayers.addObject([selection objectAtIndex:i]) }
+    }
+
+    if (localLayers.count() < 1) { return 0 }
+
+    var isSameName = true
+    if (localLayers.count() > 0) {
+        var currentLayerName = [[localLayers firstObject] name]
+        for (var i = 1; i < localLayers.count(); i++) {
+            var currentLayer = [localLayers objectAtIndex:i]
+            if (![currentLayerName isEqualToString:[currentLayer name]]) {
+                isSameName = false
+                break;
+            }
+        }
+    }
+
+    var additionalArtboards = NSMutableArray.new()
+    for (var i = 0; i < localLayers.count(); i++) {
+        var currentLayer = [localLayers objectAtIndex:i]
+        var currentLayerArtboard = TypeInventory.findParentArtboard(currentLayer)
+        if (TypeInventory.isArtboardType([currentLayerArtboard className])) { additionalArtboards.addObject(currentLayerArtboard) }
+    }
+
+    for (var i = 0; i < additionalArtboards.count(); i++) {
+        var shouldAddCurrentArtboard = true
+        var currentArtboard = [additionalArtboards objectAtIndex:i]
+
+        for (var j = 0; j < localArtboards.count(); j++) {
+            var comparableArtboard = [localArtboards objectAtIndex:j]
+            if ([[comparableArtboard name] isEqualToString:[currentArtboard name]] &&
+                [comparableArtboard hash] == [currentArtboard hash]) {
+                    shouldAddCurrentArtboard = false
+                    break;
+                }
+        }
+
+        if (shouldAddCurrentArtboard) { localArtboards.addObject(currentArtboard) }
+    }
+
+    log("L: " + localLayers.count() + ", on A:" + localArtboards.count() + ". Same?" + isSameName)
+
+    if (localArtboards.count() == 1) { return isSameName ? 1 : 3 }
+    else if (localArtboards.count() > 1) { return isSameName ? 2 : 4 }
+    else { return 0 }
+
+    return 0
+}

Added action type detection