<NME> action-simulate-keynote.cocoascript
<BEF> @import 'inventory/classes/state.cocoascript'

// generate code for layers
var runSimulateKeynote = function() {
	StatisticsInventory.setStatus(Status.simulateID())
	FramerInventory.setUserSelection(currentSelection)
	[[NSPasteboard generalPasteboard] clearContents]

	var layers = []
	var pasteboardContent = ""

	var currentPage = FramerInventory.getCurrentPage()
	FramerInventory.addExportPageAndReturn(currentPage)
	FramerInventory.cleanExportPage()

	if (userDefaults.exportFramerFolder == "") {
			selectFolder()
			return
	}

	var duplicatedArtboard = FramerInventory.returnDuplicatedArtboards(FramerInventory.returnArtboardsScope())
	if (duplicatedArtboard != nil) {
		if (showLog) { log("Warning: duplicated artboards names are found") }
		selectDuplicatedArtboards([duplicatedArtboard name])
		return
	}

	var sortedSelection = FramerInventory.organiseSelection([currentPage exportableLayers])
	if (sortedSelection == nil) {
		[currentDocument showMessage:"Nothing to simulate. Please, make some layers exportable"]
		return
	}


	if (!DemoInventory.isRegistered() && DemoInventory.actionsLeft() <= 0) {
		showExpiredModal()
		return
	}

	var artboardsMaps = OrderInventory.getArtboardsLayers()
	var artboardsNames = OrderInventory.getArtboardsNames()

	for (var i = 0; i < [sortedSelection count]; i++) {
		var currentLayer = [sortedSelection objectAtIndex: i]
		var layerName = [currentLayer name]
		var layer = new Layer(layerName)

		var hasInitialState = false
		var hasCurrentState = false


		for (var j = 0; j < artboardsMaps.count(); j++) {
			var currentMap = [artboardsMaps objectAtIndex:j]

			var hasPrevState = hasCurrentState
			var prevMapLayer = mapStateLayer

			var mapStateLayer = [currentMap objectForKey:layerName]

			if (mapStateLayer != nil) {
				hasCurrentState = true

				if (j == 0) { hasInitialState = true }
				else if (!hasInitialState) {
					var fakeFirstState = new State(mapStateLayer)
					fakeFirstState.createHiddenState([artboardsNames firstObject])
					layer.addState(fakeFirstState)
					hasInitialState = true
				}

				layer.composeState(mapStateLayer)
			}
			else {
				hasCurrentState = false

				if (hasPrevState) {
					var fakeCloseState = new State(prevMapLayer)
					fakeCloseState.createHiddenState([artboardsNames objectAtIndex:j])
					layer.addState(fakeCloseState)
				}
			}
		}

		layers.push(layer)
	}

	pasteboardContent += DescribeInventory.generateRetinaVariable() + ln()
	pasteboardContent += DescribeInventory.generatePhoneModel() + dln()
	pasteboardContent += DescribeInventory.sectionView()

	for (var i = 0; i < layers.length; i++) { pasteboardContent += layers[i].printLayer() }

	pasteboardContent += ln()
	pasteboardContent += DescribeInventory.sectionModel()
	pasteboardContent += DescribeInventory.generateCycleButton()

	var cycleStates = FramerInventory.getUsedStatesNames())
	pasteboardContent += DescribeInventory.generateCycleStates(cycleStates)

	pasteboardContent += DescribeInventory.generateLayers(sortedSelection)
	pasteboardContent += DescribeInventory.generateCycler() + ln()
	pasteboardContent += DescribeInventory.sectionCustom()

	if (userDefaults.exportFramerFolder != "" && [[NSFileManager defaultManager] fileExistsAtPath:userDefaults.exportFramerFolder + "/app.coffee"]) {
			var currentString = [NSString stringWithContentsOfFile:userDefaults.exportFramerFolder + "/app.coffee" encoding:NSUTF8StringEncoding error:nil]
			var currentPrototypeParts = currentString.split(DescribeInventory.sectionCustom())

			if (currentPrototypeParts.length > 1) { pasteboardContent += currentPrototypeParts[1] }
			else { pasteboardContent += DescribeInventory.generateHandler() }
	}
	else { pasteboardContent += DescribeInventory.generateHandler() }


	[[NSPasteboard generalPasteboard] setString:pasteboardContent forType:NSStringPboardType];
	DemoInventory.actionUsed()
	FramerInventory.showDoneMessage()

	StatisticsInventory.setStatus(Status.waitingID())
	return pasteboardContent
}

<MSG> Exportables in Scope for Export Prototypes

– now plugin takes care of exportables within selected scope instead of
current page

<DFF> @@ -9,8 +9,7 @@ var runSimulateKeynote = function() {
 	var layers = []
 	var pasteboardContent = ""
 
-	var currentPage = FramerInventory.getCurrentPage()
-	FramerInventory.addExportPageAndReturn(currentPage)
+	FramerInventory.addExportPageAndReturn(FramerInventory.getCurrentPage())
 	FramerInventory.cleanExportPage()
 
 	if (userDefaults.exportFramerFolder == "") {
@@ -18,14 +17,15 @@ var runSimulateKeynote = function() {
 			return
 	}
 
-	var duplicatedArtboard = FramerInventory.returnDuplicatedArtboards(FramerInventory.returnArtboardsScope())
+	var artboardsScope = FramerInventory.returnArtboardsScope()
+	var duplicatedArtboard = FramerInventory.returnDuplicatedArtboards(artboardsScope)
 	if (duplicatedArtboard != nil) {
 		if (showLog) { log("Warning: duplicated artboards names are found") }
 		selectDuplicatedArtboards([duplicatedArtboard name])
 		return
 	}
 
-	var sortedSelection = FramerInventory.organiseSelection([currentPage exportableLayers])
+	var sortedSelection = FramerInventory.organiseSelection(returnExportableLayersForSelectedScope(artboardsScope))
 	if (sortedSelection == nil) {
 		[currentDocument showMessage:"Nothing to simulate. Please, make some layers exportable"]
 		return
@@ -118,3 +118,48 @@ var runSimulateKeynote = function() {
 	StatisticsInventory.setStatus(Status.waitingID())
 	return pasteboardContent
 }
+
+
+
+
+
+
+var returnExportableLayersForSelectedScope = function(selectedScope) {
+	var currentPage = FramerInventory.getCurrentPage()
+	var pageExportables = [currentPage exportableLayers]
+
+	var scopeExportables = NSMutableArray.new()
+
+	for (var i = 0; i < pageExportables.count(); i++) {
+			var currentLayer = [pageExportables objectAtIndex: i]
+
+			if (exportableInScope(currentLayer, selectedScope)) {
+				scopeExportables.addObject(currentLayer)
+				// log("+1 exportable")
+				// log(currentLayer)
+				// log("---")
+			}
+
+	}
+
+	// log("ALL: ")
+	// log([[currentPage exportableLayers] count])
+	//
+	// log("SEL: ")
+	// log([scopeExportables count])
+
+	// return [currentPage exportableLayers]
+	return scopeExportables
+}
+
+
+var exportableInScope = function(layer, artboardsScope) {
+	var currentParentArtboard = TypeInventory.findParentArtboard(layer)
+
+	for (var i = 0; i < artboardsScope.count(); i++) {
+		var currentScopeArtboard = [artboardsScope objectAtIndex:i]
+		if (FramerInventory.sameArtboards(currentParentArtboard, currentScopeArtboard)) { return true }
+	}
+
+	return false
+}

Exportables in Scope for Export Prototypes