Last active
December 20, 2015 05:57
-
-
Save matsuhisa/c91a93583064628edd5e to your computer and use it in GitHub Desktop.
Sketch のプラグインをCocoaScriptを使って作る ref: http://qiita.com/matsuhisa_h/items/aee924bc2360a9180f83
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/Library/Containers/com.bohemiancoding.sketch3/Data/Library/Application Support/com.bohemiancoding.sketch3/Plugins/SketchTestPlugin1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
├── SketchTestPlugin1.sketchplugin | |
│ └── Contents | |
│ └── Sketch | |
│ ├── between.cocoascript | |
│ ├── images.cocoascript | |
│ ├── manifest.json | |
│ ├── random_shop.cocoascript | |
│ ├── random_text.cocoascript | |
│ ├── random_tokyo.cocoascript | |
│ ├── revew_average.cocoascript | |
│ ├── revew_average_star.cocoascript | |
│ └── script.cocoascript | |
└── data | |
├── areas | |
│ └── tokyo.js | |
├── names | |
│ └── names.js | |
├── photo | |
│ └── 1.jpg | |
├── reviews | |
└── shops | |
└── names.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import "library.cocoascript"; | |
@import '../../../data/shops/names.js' | |
var onRun = function(context) { | |
var selection = context.selection | |
for (var i = 0, l = [selection count]; i < l; i++) { | |
var layer = selection[i]; | |
layer.stringValue = data[Math.floor(Math.random() * data.length)]; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import "library.cocoascript"; | |
var onRun = function(context) { | |
var documentName = context.document.displayName(); | |
var selection = context.selection | |
var max = 4; | |
var min = 1; | |
for (var i = 0, l = [selection count]; i < l; i++) { | |
var layer = selection[i]; | |
var avarage = ( Math.random() * max) ) + min; | |
layer.stringValue = String(avarage.toFixed(2)); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import "library.cocoascript"; | |
var onRun = function(context) { | |
var selection = context.selection | |
for (var i = 0, l = [selection count]; i < l; i++) { | |
var layer = selection[i]; | |
var color = MSColor.colorWithRed_green_blue_alpha(0.988, 0.29, 0.471, 1.0); | |
layer.textColor = color; | |
layer.stringValue = avarage_string(); | |
} | |
}; | |
function avarage_string() { | |
var max = 5; | |
var min = 1; | |
var avarage_string = ""; | |
var avarage = ( Math.random() * max) ) + min; | |
var avarage_integer = Math.floor(avarage.toFixed(2)); | |
for(var i = min; i <= max; i++) { | |
if( i <= avarage_integer) { | |
avarage_string = avarage_string + "★"; | |
}else { | |
avarage_string = avarage_string + "☆"; | |
} | |
} | |
return avarage_string; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import "library.cocoascript"; | |
var onRun = function(context) { | |
var selection = context.selection | |
var data_path = 'data/photo/'; | |
var fileManager = [NSFileManager defaultManager]; | |
var script_path = context.scriptPath; | |
var plugin_folder = script_path.match(/Plugins\/([\w -])*/)[0] + "/"; | |
var sketch_plugins_path = script_path.replace(/Plugins\/([\w -])*\/([\w -])*.sketchplugin(.*)/, ""); | |
var images_path = sketch_plugins_path + plugin_folder + data_path; | |
var imagesFileNames = [fileManager contentsOfDirectoryAtPath:images_path error:nil]; | |
var imageCount = [imagesFileNames count] - 1; | |
var selected_paths = []; | |
for(var i = 1; i < 100; i++){ | |
var index = Math.floor(Math.random() * imageCount); | |
index = index >= imageCount ? 0 : index + 1; | |
var file_name = imagesFileNames[index]; | |
var file_path = images_path + file_name; | |
selected_paths.push(file_path); | |
} | |
for(var i = 0; i < [selection count]; i++){ | |
var layer = selection[i]; | |
log(selected_paths[i]); | |
var image = [[NSImage alloc] initWithContentsOfFile:selected_paths[i]]; | |
if([layer class] == MSShapeGroup){ | |
var fill = layer.style().fills().firstObject(); | |
fill.setFillType(4); | |
var coll = layer.style().fills().firstObject().documentData().images(); | |
[fill setPatternImage:image collection:coll] | |
layer.style().fills().firstObject().setPatternFillType(1); | |
} | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment