Skip to content

Commit 9530619

Browse files
author
Timur
committed
Generate unique when possible pictures
1 parent 41dee84 commit 9530619

File tree

1 file changed

+44
-53
lines changed

1 file changed

+44
-53
lines changed

js/loadImage.js

100644100755
Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,72 @@ var loadImages = function(dataPath, groupName, pictureName){
22
var groupName = groupName || 'picture';
33
pictureName = pictureName || 'picture';
44

5-
function loadImages(selectedImgCount){
6-
log('selectedImgCount: '+selectedImgCount);
5+
function loadImages(imgAmount){
6+
var fileManager = [NSFileManager defaultManager];
7+
var scriptPath = sketch.scriptPath;
8+
var pluginFolder = scriptPath.match(/Plugins\/([\w -])*/)[0] + "/";
9+
var sketchPluginsPath = scriptPath.replace(/Plugins([\w \/ -])*.sketchplugin$/, "");
10+
var imagesPath = sketchPluginsPath + pluginFolder + dataPath;
11+
var imagesFileNames = [fileManager contentsOfDirectoryAtPath:imagesPath error:nil];
12+
var imageCount = [imagesFileNames count] -1;
13+
var selectedPaths = [];
714

8-
var imagesCollection = [],
9-
fileManager = [NSFileManager defaultManager];
1015

11-
if(tools.majorVersion() == 3){
12-
var scriptPath = sketch.scriptPath;
13-
var pluginFolder = scriptPath.match(/Plugins\/([\w -])*/)[0] + '/';
14-
var sketchPluginsPath = scriptPath.replace(/Plugins([\w \/ -])*.sketchplugin$/, '');
15-
imagesPath = sketchPluginsPath + pluginFolder + dataPath;
16-
}
17-
log('imagesPath: '+imagesPath);
18-
var imagesFileNames = [fileManager contentsOfDirectoryAtPath:imagesPath error:nil],
19-
imgLen = [imagesFileNames count];
20-
21-
log('imagesFileNames (before clean): '+imagesFileNames);
22-
23-
// clean out invalid files
24-
var imagesFileNamesCleaned = [];
25-
for (var i = 0; i < imgLen; i++) {
26-
if (imagesFileNames[i] == '.DS_Store') {
27-
log('skipping '+imagesFileNames[i]);
28-
29-
} else {
30-
imagesFileNamesCleaned.push(imagesFileNames[i]);
31-
}
32-
}
33-
34-
log('imagesFileNamesCleaned: '+imagesFileNamesCleaned);
16+
if(imgAmount > imageCount){
17+
while(imgAmount--) {
18+
var index = Math.floor(Math.random() * imageCount);
19+
do {
20+
index = index > imageCount ? 0 : index + 1;
21+
var fileName = imagesFileNames[index];
22+
var filePath = imagesPath + fileName;
23+
} while(![fileManager fileExistsAtPath: filePath] || fileName == '.DS_Store')
3524

36-
var offset = 0;
37-
for (var i = 0; i < selectedImgCount; i++) {
38-
var filename = '';
39-
if (i+1 > imagesFileNamesCleaned.length) {
40-
// no more images (reached end) - move back and use the first ones
41-
filename = imagesFileNamesCleaned[offset++];
42-
} else {
43-
filename = imagesFileNamesCleaned[i];
25+
selectedPaths.push(filePath);
4426
}
45-
46-
var pathToFile = imagesPath+filename;
47-
48-
if ([fileManager fileExistsAtPath: pathToFile]) {
49-
var newImage = [[NSImage alloc] initWithContentsOfFile:pathToFile];
50-
imagesCollection.push(newImage);
27+
}
28+
else{
29+
while(imgAmount--) {
30+
do {
31+
var index = Math.floor(Math.random() * imageCount);
32+
var fileName = imagesFileNames[index];
33+
var filePath = imagesPath + fileName;
34+
var match = selectedPaths.filter(function(selectedPath){return filePath == selectedPath;});
35+
} while(fileName == '.DS_Store' || ![fileManager fileExistsAtPath: filePath] || match.length >= 1);
36+
selectedPaths.push(filePath);
5137
}
5238
}
39+
40+
return selectedPaths.map(function(imagePath){
41+
if ([fileManager fileExistsAtPath: imagePath]) {
42+
var image = [[NSImage alloc] initWithContentsOfFile:imagePath];
43+
return image;
44+
}
45+
})
46+
}
5347

54-
return imagesCollection;
55-
};
56-
57-
function main(){
48+
function main(){
5849
var allLayers = [[doc currentPage] layers],
59-
imagesCollection = loadImages([selection count]);
50+
imagesCollection = loadImages([selection count] + 1);
6051

6152
for(var i = 0; i < [selection count]; i++){
6253
var layer = selection[i];
6354
if([layer class] == MSShapeGroup){
6455
var image = imagesCollection[i];
6556
var fill = layer.style().fills().firstObject();
66-
fill.setFillType(4);
67-
//log(tools.minorVersion())
57+
fill.setFillType(4);
6858
if(tools.minorVersion() >= 1){
69-
var coll = layer.style().fills().firstObject().documentData().images();
59+
var coll = layer.style().fills().firstObject().documentData().images();
7060
[fill setPatternImage:image collection:coll]
7161
}
7262
else{
7363
layer.style().fills().firstObject().setPatternImage( image );
74-
}
64+
}
7565
layer.style().fills().firstObject().setPatternFillType(1);
7666
}
7767
}
7868

79-
if([selection count] == 0) alert('Select at least one vector shape');
69+
if([selection count] == 0) [doc showMessage:'Select at least one vector shape'];;
8070
}
8171
main();
82-
};
72+
}
73+

0 commit comments

Comments
 (0)