Skip to content

Commit c14da40

Browse files
committed
Create Rad faces
1 parent 015e499 commit c14da40

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Photos/Rad faces

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//Based on the original source of Nick Stamas @nickstamas https://github.com/nickstamas/Sketch-Quick-Pic
2+
//Modified by @timur_carpeev
3+
4+
if ([selection count] > 0) {
5+
var request = [[NSMutableURLRequest alloc] init];
6+
[request setHTTPMethod:@"GET"];
7+
var queryString = "http://www.radfaces.com/public/radfaces.json";
8+
[request setURL:[NSURL URLWithString:queryString]];
9+
10+
var error = [[NSError alloc] init];
11+
var responseCode = null;
12+
13+
var oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:responseCode error:error];
14+
15+
var dataString = [[NSString alloc] initWithData:oResponseData
16+
encoding:NSUTF8StringEncoding];
17+
18+
var pattern = new RegExp("\\\\'", "g");
19+
var validJSONString = dataString.replace(pattern, "'");
20+
21+
var data = JSON.parse(validJSONString);
22+
23+
log(data.length)
24+
25+
if (data.length > 0) {
26+
for (var i=0; i <= [selection length]; i++) {
27+
var randomIndex = Math.floor(Math.random()*(data.length-1))
28+
var imageURLString = data[randomIndex].image;
29+
var url = [[NSURL alloc] initWithString: imageURLString];
30+
31+
var newImage = [[NSImage alloc] initByReferencingURL:url];
32+
33+
var layer = [selection objectAtIndex:i];
34+
35+
layer.setName(data[randomIndex].name)
36+
37+
layer.style().fills().firstObject().setPatternFillType(1);
38+
39+
var coll = layer.style().fills().firstObject().documentData().images();
40+
41+
[fill setPatternImage: newImage collection:coll];
42+
}
43+
} else {
44+
var message = "No images found tagged with: " + tag;
45+
[doc showMessage: message];
46+
}
47+
} else {
48+
[doc showMessage:"No layer is selected."];
49+
}

0 commit comments

Comments
 (0)