<NME> selection-changed-action.cocoascript
<BEF> @import 'actions/action-inventory.cocoascript'
@import 'actions/view-inventory.cocoascript'
@import 'lib.js'


var onSelectionChanged = function(context) {

    // coscript.setShouldKeepAround(true);
    // coscript.scheduleWithInterval_jsFunction(0.2,function() {
    //     var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
    //     ToolbarInventory.updateSelectionLabelStringValue(selection.count() + "")
    //     coscript.setShouldKeepAround(false); // Tearing down the session.
    // }


    coscript.setShouldKeepAround(true); // Setting session as a long running.

    // First argument is a delay in seconds.
    coscript.scheduleWithInterval_jsFunction(0.4,function() {
        var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();


        ToolbarInventory.updateSelectionLabelStringValue(analyseActionType(selection) + "")

        // if (testObject != nil) {
        //     // var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
        //     // var selection = context.actionContext.newSelection
        //     // log(selection)
        //
        //     var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
        //     // log(selectionCorrect)
        //
        //     if (selection.count() > 0) {
        //         var currentName = "Artboards: "
        //
        //         var counterA = 0
        //         var counterL = 0
        //
        //         for (var i = 0; i < selection.count(); i++) {
        //               if (TypeInventory.isArtboardType([[selection objectAtIndex:i] className])) { counterA++ }
        //             // currentName += selection[i].name() + ", "
        //         }
        //         testObject.updateSelectionLabel(currentName + counterA)
        //     }
        //     else {
        //         testObject.updateSelectionLabel("Select Layers")
        //     }
        //
        // }

        coscript.setShouldKeepAround(false); // Tearing down the session.
    });






    //
    // var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
    // var tempCount = [selection count]
    // if (tempCount < 2) {
    //     log("Handler")
    //
    //     var threadDictionary = NSThread.mainThread().threadDictionary()
    //     log(threadDictionary)
    //     testObject = threadDictionary[testThreadIdentifier]
    //
    //     if (testObject == nil) {
    //       log("Object is NULL")
    //
    //       threadDictionary[testThreadIdentifier] = new TestObject("example");
    //       log(threadDictionary)
    //
    //       threadDictionary = NSThread.mainThread().threadDictionary()
    //
    //       log("Object should be changed. Let's check it")
    //       log(threadDictionary)
    //       var changedObject = threadDictionary[testThreadIdentifier]
    //       log(changedObject.panel)
    //     }
    //     else {
    //       log(threadDictionary[testThreadIdentifier])
    //       var changedObject = threadDictionary[testThreadIdentifier]
    //       log(changedObject.panel)
    //     }
    // }
    // else {
    //   log("gonna Clean. Base is")
    //
    //   var threadDictionary = NSThread.mainThread().threadDictionary()
    //   log(threadDictionary[testThreadIdentifier])
    //   threadDictionary.removeObjectForKey(testThreadIdentifier);
    //   log("works?")
    //   log(NSThread.mainThread().threadDictionary())
    //
    // }





    // log(NSThread.mainThread().threadDictionary())
		// log(UIBar)
		// if (UIBar == nil) {
		// 	log("NO")
    //
    //
    //
    //
    //   threadDictionary = NSThread.mainThread().threadDictionary()
    //   UIBar = threadDictionary[testThreadIdentifier]
    //
    //   log(UIBar)
		// }
};


// 0 – Select layers

    if (currentDocument == nil) {
// 3 – layers / all states for layers
// 4 – selected states for layers

var analyseActionType = function(selection) {
    var localLayers = NSMutableArray.new()
    var localArtboards = NSMutableArray.new()

    // 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])
        else { localLayers.addObject([selection objectAtIndex:i]) }
    }

    if (localLayers.count() < 1) { return 0 }

    var isSameName = true
    if (localLayers.count() > 0) {
      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


    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
}
    var hasParentArtboard = false

    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 (!hasParentArtboard) {
                var localParent = TypeInventory.findParentArtboard([selection objectAtIndex:i])
                if (localParent != nil && TypeInventory.isArtboardType(localParent.className())) { hasParentArtboard = true }
            }
        }
    }
    // for Keynote only
    if (StatisticsInventory.getStatus() == Status.simulateID() || (ToolbarInventory.toolbarMode() == Status.toolbarModeScene() && (StatisticsInventory.getStatus() != Status.generateID() || StatisticsInventory.getStatus() != Status.replicateID()))) {
        if (localArtboards.count() < 1) { return new SelectionInventory(5, currentDocument.currentPage().artboards()) }
        return new SelectionInventory(6, localArtboards)
    }

    if (localLayers.count() < 1 || !hasParentArtboard) { return new SelectionInventory(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)

    var type = -1
    if (localArtboards.count() == 1) { type = isSameName ? 1 : 3 }
    else if (localArtboards.count() > 1) { type = isSameName ? 2 : 4 }
    else { type = 0 }

    return new SelectionInventory(type, localArtboards, localLayers)
}

<MSG> Added support for ActionTypes

– Removed Smart Action to introduce advanced Action Types
– returnArtboardsScope now return only suitable artboards

TODO: correct artboards sequence is required

<DFF> @@ -3,117 +3,42 @@
 @import 'lib.js'
 
 
-var onSelectionChanged = function(context) {
+function SelectionInventory(type) {
+    this.type = type
+    this.artboards = NSMutableArray.new()
+}
 
-    // coscript.setShouldKeepAround(true);
-    // coscript.scheduleWithInterval_jsFunction(0.2,function() {
-    //     var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
-    //     ToolbarInventory.updateSelectionLabelStringValue(selection.count() + "")
-    //     coscript.setShouldKeepAround(false); // Tearing down the session.
-    // }
+function SelectionInventory(type, artboards) {
+    this.type = type
+    this.artboards = NSMutableArray.new()
+    if (artboards != nil) { this.artboards = artboards}
+}
 
 
-    coscript.setShouldKeepAround(true); // Setting session as a long running.
 
-    // First argument is a delay in seconds.
-    coscript.scheduleWithInterval_jsFunction(0.4,function() {
-        var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
 
+var onSelectionChanged = function(context) {
+    coscript.setShouldKeepAround(true);
 
-        ToolbarInventory.updateSelectionLabelStringValue(analyseActionType(selection) + "")
-
-        // if (testObject != nil) {
-        //     // var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
-        //     // var selection = context.actionContext.newSelection
-        //     // log(selection)
-        //
-        //     var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
-        //     // log(selectionCorrect)
-        //
-        //     if (selection.count() > 0) {
-        //         var currentName = "Artboards: "
-        //
-        //         var counterA = 0
-        //         var counterL = 0
-        //
-        //         for (var i = 0; i < selection.count(); i++) {
-        //               if (TypeInventory.isArtboardType([[selection objectAtIndex:i] className])) { counterA++ }
-        //             // currentName += selection[i].name() + ", "
-        //         }
-        //         testObject.updateSelectionLabel(currentName + counterA)
-        //     }
-        //     else {
-        //         testObject.updateSelectionLabel("Select Layers")
-        //     }
-        //
-        // }
-
-        coscript.setShouldKeepAround(false); // Tearing down the session.
-    });
+    coscript.scheduleWithInterval_jsFunction(0.1,function() {
+        var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
 
+        var currentSelectionType = SelectionInventory.analyseActionType(selection).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" }
 
+        ToolbarInventory.updateSelectionLabelStringValue(selectionTitle + "")
 
+        coscript.setShouldKeepAround(false);
+    });
 
+};
 
 
-    //
-    // var selection = NSDocumentController.sharedDocumentController().currentDocument().selectedLayers();
-    // var tempCount = [selection count]
-    // if (tempCount < 2) {
-    //     log("Handler")
-    //
-    //     var threadDictionary = NSThread.mainThread().threadDictionary()
-    //     log(threadDictionary)
-    //     testObject = threadDictionary[testThreadIdentifier]
-    //
-    //     if (testObject == nil) {
-    //       log("Object is NULL")
-    //
-    //       threadDictionary[testThreadIdentifier] = new TestObject("example");
-    //       log(threadDictionary)
-    //
-    //       threadDictionary = NSThread.mainThread().threadDictionary()
-    //
-    //       log("Object should be changed. Let's check it")
-    //       log(threadDictionary)
-    //       var changedObject = threadDictionary[testThreadIdentifier]
-    //       log(changedObject.panel)
-    //     }
-    //     else {
-    //       log(threadDictionary[testThreadIdentifier])
-    //       var changedObject = threadDictionary[testThreadIdentifier]
-    //       log(changedObject.panel)
-    //     }
-    // }
-    // else {
-    //   log("gonna Clean. Base is")
-    //
-    //   var threadDictionary = NSThread.mainThread().threadDictionary()
-    //   log(threadDictionary[testThreadIdentifier])
-    //   threadDictionary.removeObjectForKey(testThreadIdentifier);
-    //   log("works?")
-    //   log(NSThread.mainThread().threadDictionary())
-    //
-    // }
-
-
-
-
-
-    // log(NSThread.mainThread().threadDictionary())
-		// log(UIBar)
-		// if (UIBar == nil) {
-		// 	log("NO")
-    //
-    //
-    //
-    //
-    //   threadDictionary = NSThread.mainThread().threadDictionary()
-    //   UIBar = threadDictionary[testThreadIdentifier]
-    //
-    //   log(UIBar)
-		// }
-};
 
 
 // 0 – Select layers
@@ -122,7 +47,12 @@ var onSelectionChanged = function(context) {
 // 3 – layers / all states for layers
 // 4 – selected states for layers
 
-var analyseActionType = function(selection) {
+SelectionInventory.analyseActionType = function(selection) {
+    if (selection == nil) {
+        if (showLog) { log("Warning. Selection is nil") }
+        return new SelectionInventory(0)
+    }
+
     var localLayers = NSMutableArray.new()
     var localArtboards = NSMutableArray.new()
 
@@ -132,7 +62,7 @@ var analyseActionType = function(selection) {
         else { localLayers.addObject([selection objectAtIndex:i]) }
     }
 
-    if (localLayers.count() < 1) { return 0 }
+    if (localLayers.count() < 1) { return new SelectionInventory(0) }
 
     var isSameName = true
     if (localLayers.count() > 0) {
@@ -171,9 +101,10 @@ var analyseActionType = function(selection) {
 
     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 }
+    var type = -1
+    if (localArtboards.count() == 1) { type = isSameName ? 1 : 3 }
+    else if (localArtboards.count() > 1) { type = isSameName ? 2 : 4 }
+    else { type = 0 }
 
-    return 0
+    return new SelectionInventory(type, localArtboards)
 }

Added support for ActionTypes