-
Notifications
You must be signed in to change notification settings - Fork 19
/
personas.sketchplugin
117 lines (92 loc) · 2.88 KB
/
personas.sketchplugin
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const PATTERN_FILL_IMAGE = 1;
const FILL_TYPE_FILL = 4;
if (isNothingSelected()) {
var app = NSApplication.sharedApplication();
return [app displayDialog:"Please select at least one layer or group." withTitle:"Nothing Selected"];
}
if (isGroupSelected()) {
updateGroups(context.selection);
} else {
updateLayers(context.selection, getUser());
}
function updateGroups(groups) {
var users = getUsers(groups.count());
for (var i=0; i < groups.count(); i++) {
var group = groups[i];
var user = users[i].user;
updateLayers(group.layers().array(), user);
}
}
function updateLayers(layers, user) {
for (var i=0; i < layers.count(); i++) {
var layer = layers[i];
if (layer.className() == 'MSShapeGroup') {
setImage(layer, user);
} else {
setText(layer, user);
}
}
}
function getUser() {
return getUsers(1)[0];
}
function getUsers(count) {
var request = NSMutableURLRequest.new();
[request setHTTPMethod:@"GET"];
var queryString = "http://api.randomuser.me?nat=us&results=" + count;
[request setURL:[NSURL URLWithString:queryString]];
var error = NSError.new();
var responseCode = null;
var oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:responseCode error:error];
var dataString = [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
var data = JSON.parse(dataString);
return data.results;
}
function setImage(layer, user) {
var name = layer.name();
if (name == 'large' || name == 'thumbnail') {
size = name
} else {
size = 'medium'
}
var imageURLString = user.picture[size];
var url = [[NSURL alloc] initWithString: imageURLString];
var imageData = [[NSImage alloc] initByReferencingURL:url];
var fill = layer.style().fills().firstObject();
var image = [[MSImageData alloc] initWithImage: imageData convertColorSpace: false]]
layer.style().fills().firstObject().setImage(image);
layer.style().fills().firstObject().setPatternFillType(PATTERN_FILL_IMAGE);
fill.setFillType(FILL_TYPE_FILL);
}
function setText(layer, user) {
var name = layer.name();
var text;
if (name == 'name') {
text = user.name.first + ' ' + user.name.last;
text = capitalize(text);
} else if (name == 'location') {
text = user.location.city + ', ' + user.location.state;
text = capitalize(text);
} else if (name == 'dob') {
unix_time = user.dob;
date = new Date(unix_time * 1000);
text = date.getMonth() + "/" + date.getDate() + "/" + date.getFullYear();
} else {
text = user[name];
}
layer.stringValue = text;
updateDisplayHack(layer);
}
function updateDisplayHack(layer) {
layer.name = layer.name();
layer.adjustFrameToFit();
}
function capitalize(s){
return s.toLowerCase().replace( /\b./g, function(a) { return a.toUpperCase(); } );
}
function isGroupSelected() {
return context.selection.firstObject().className() == 'MSLayerGroup';
}
function isNothingSelected() {
return context.selection.count() == 0;
}