-
Notifications
You must be signed in to change notification settings - Fork 350
/
loadImage.js
executable file
·78 lines (68 loc) · 2.49 KB
/
loadImage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var loadImages = function(context, dataPath, groupName, pictureName){
var doc = context.document;
var selection = context.selection;
var bundle = context.plugin
var groupName = groupName || 'picture';
pictureName = pictureName || 'picture';
function getImageCollection(imgAmount){
var fileManager = [NSFileManager defaultManager];
var imagesPath = bundle.url() + "Contents/Sketch/" + dataPath;
imagesPath = imagesPath.replace("%20", " ").replace("file://", "")
log(imagesPath)
var imagesFileNames = [fileManager contentsOfDirectoryAtPath:imagesPath error:nil];
var imageCount = [imagesFileNames count] -1;
var selectedPaths = [];
if(imgAmount > imageCount){ //Load images in sequential order
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{ //Load unique images
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 = getImageCollection(selection.length + 1);
for(var i = 0; i < selection.length; i++){
var layer = selection[i];
if([layer class] == MSShapeGroup) {
var image = imagesCollection[i];
log(image);
var fill = layer.style().fills().firstObject();
fill.setFillType(4);
var imageData;
if (MSApplicationMetadata.metadata().appVersion < 47) {
imageData = MSImageData.alloc().initWithImage_convertColorSpace(image, false);
} else {
imageData = MSImageData.alloc().initWithImage(image);
}
fill.setImage(imageData);
fill.setPatternFillType(1);
}
}
if(selection.length == 0) [doc showMessage:'Select at least one vector shape'];;
tools.checkPluginUpdate();
}
main();
}