<commit_before>var loadImages = function(dataPath, groupName, pictureName){
	var groupName = groupName || 'picture';
		pictureName = pictureName || 'picture';

	function loadImages(selectedImgCount){
		log('selectedImgCount: '+selectedImgCount);

		var imagesCollection = [],
			fileManager = [NSFileManager defaultManager];

		if(tools.majorVersion() == 3){
			var scriptPath = sketch.scriptPath;
			var pluginFolder = scriptPath.match(/Plugins\/([\w -])*/)[0] + '/';
			var sketchPluginsPath = scriptPath.replace(/Plugins([\w \/ -])*.sketchplugin$/, '');
			imagesPath =  sketchPluginsPath + pluginFolder + dataPath;
		}
		log('imagesPath: '+imagesPath);
		var imagesFileNames = [fileManager contentsOfDirectoryAtPath:imagesPath error:nil],
			imgLen = [imagesFileNames count];

		log('imagesFileNames (before clean): '+imagesFileNames);

		// clean out invalid files
		var imagesFileNamesCleaned = [];
		for (var i = 0; i < imgLen; i++) {
			if (imagesFileNames[i] == '.DS_Store') {
				log('skipping '+imagesFileNames[i]);

			} else {
				imagesFileNamesCleaned.push(imagesFileNames[i]);
			}
		}

		log('imagesFileNamesCleaned: '+imagesFileNamesCleaned);

		var offset = 0;
		for (var i = 0; i < selectedImgCount; i++) {
      var filename = '';
			if (i+1 > imagesFileNamesCleaned.length) {
				// no more images (reached end) - move back and use the first ones
				filename = imagesFileNamesCleaned[offset++];
			} else {
				filename = imagesFileNamesCleaned[i];
			}

			var pathToFile = imagesPath+filename;

			if ([fileManager fileExistsAtPath: pathToFile]) {
				var newImage = [[NSImage alloc] initWithContentsOfFile:pathToFile];
				imagesCollection.push(newImage);
			}
		}

		return imagesCollection;
	};

	function main(){
		var allLayers = [[doc currentPage] layers],
			imagesCollection = loadImages([selection count]);

		for(var i = 0; i < [selection count]; i++){
			var layer = selection[i];
            if([layer class] == MSShapeGroup){
                var image = imagesCollection[i];
                var fill = layer.style().fills().firstObject();
                fill.setFillType(4);
                //log(tools.minorVersion())
                if(tools.minorVersion() >= 1){
                	var coll = layer.style().fills().firstObject().documentData().images();
                	[fill setPatternImage:image collection:coll]
                }
                else{
                	layer.style().fills().firstObject().setPatternImage( image );
                }
                layer.style().fills().firstObject().setPatternFillType(1);
            }
		}

		if([selection count] == 0) alert('Select at least one vector shape');
	}
	main();
};<commit_msg>Generate unique when possible pictures<commit_after>var loadImages = function(dataPath, groupName, pictureName){
	var groupName = groupName || 'picture';
		pictureName = pictureName || 'picture';

	function loadImages(imgAmount){
		var fileManager = [NSFileManager defaultManager];
		var scriptPath = sketch.scriptPath;			
		var pluginFolder = scriptPath.match(/Plugins\/([\w -])*/)[0] + "/";
		var sketchPluginsPath = scriptPath.replace(/Plugins([\w \/ -])*.sketchplugin$/, "");
		var imagesPath =  sketchPluginsPath + pluginFolder + dataPath;
		var imagesFileNames = [fileManager contentsOfDirectoryAtPath:imagesPath error:nil];
		var imageCount = [imagesFileNames count] -1;
		var selectedPaths = [];


		if(imgAmount > imageCount){
			while(imgAmount--) {
				var index = Math.floor(Math.random() * imageCount);
				do {
					index = index > imageCount ? 0 : index + 1;
					var fileName = imagesFileNames[index];
					var filePath = imagesPath + fileName;			
				} while(![fileManager fileExistsAtPath: filePath] || fileName == '.DS_Store')

				selectedPaths.push(filePath);
			}
		} 
		else{
			while(imgAmount--) {
				do {
					var index = Math.floor(Math.random() * imageCount);
					var fileName = imagesFileNames[index];
					var filePath = imagesPath + fileName;
					var match = selectedPaths.filter(function(selectedPath){return filePath == selectedPath;});
				} while(fileName == '.DS_Store' || ![fileManager fileExistsAtPath: filePath] || match.length >= 1);
				selectedPaths.push(filePath);
			}
		}
		
		return selectedPaths.map(function(imagePath){
			if ([fileManager fileExistsAtPath: imagePath]) {				
				var image = [[NSImage alloc] initWithContentsOfFile:imagePath];			
				return image;
			}
		})
	}

	function main(){		
		var allLayers = [[doc currentPage] layers],
			imagesCollection = loadImages([selection count] + 1);

		for(var i = 0; i < [selection count]; i++){
			var layer = selection[i];
            if([layer class] == MSShapeGroup){
                var image = imagesCollection[i];
                var fill = layer.style().fills().firstObject();
                fill.setFillType(4);                
                if(tools.minorVersion() >= 1){
                	var coll = layer.style().fills().firstObject().documentData().images();              
                	[fill setPatternImage:image collection:coll]
                }
                else{
                	layer.style().fills().firstObject().setPatternImage( image );
                }                                                
                layer.style().fills().firstObject().setPatternFillType(1);
            }
		}

		if([selection count] == 0) [doc showMessage:'Select at least one vector shape'];;
	}
	main();
}

{'fraction_non_alphanumeric': 0.13700147710487445, 'fraction_numerical': 0.004062038404726735, 'mean_word_length': 2.844570617459191, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2705871', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1526, 'n_words': 345}