├── README.md ├── sketch-artwork-fetcher.sketchplugin └── Contents │ └── Sketch │ ├── manifest.json │ └── script.cocoascript └── tutorial └── fetch.gif /README.md: -------------------------------------------------------------------------------- 1 | Artwork Fetcher Plugin for Sketch 2 | ============= 3 | 4 | Sketch.app plugin for fetching artworks from Last.fm web-service and filling selected shapes with them. Allows to fetch artworks by their album and/or artist names. 5 | 6 | Initially, the plugin was developed to fulfil our own needs at [Semibold Mammoth](http://mmth.us) when we designed [Simplify for Mac](http://mmth.us/simplify) and its [Tape.im](http://tape.im) web-service. 7 | 8 | ## Demo 9 | ![Fetching artworks](https://raw.githubusercontent.com/mmth/sketch-artwork-fetcher/master/tutorial/fetch.gif) 10 | 11 | ## Usage 12 | 1. Select at least one shape you want to fill with an artwork 13 | 2. Navigate the menu bar and select `Plugins ▸ Artwork Fetcher ▸ Fetch Artworks From Last.fm` 14 | 15 | **Keyboard Shortcut** 16 | The default command bind for adding artworks to all selected shapes is: 17 | `Control` + `Command` + `L` 18 | 19 | **Usage Notes** 20 | Last.fm API allows to fetch no more than 50 artworks. Anyway, downloading more than 10–15 artworks at a time can cause complete freezing of Sketch. It depends on your Internet broadband and performance of your Mac. 21 | 22 | It worth mentioning that the plugin works only with Sketch 3.1 and newer. 23 | 24 | 25 | 26 | ## Installation 27 | 1. Download and open `sketch-artwork-fetcher-master.zip` 28 | 2. Navigate the Sketch menu bar to `Plugins ▸ Reveal Plugins Folder…` 29 | 3. Place the `sketch-artwork-fetcher.sketchplugin` bundle into the revealed plugins directory 30 | 4. ??? 31 | 5. Profit. 32 | 33 | ## Contribution 34 | Any contributions you make to this repository are greatly appreciated. Feel free to send a pull request if you've made an improvement. The recommended steps to follow for any contribution are: 35 | 36 | 1. Fork it 37 | 2. Create your feature branch (`git checkout -b my-new-feature`) 38 | 3. Commit your changes (`git commit -am 'Added some feature'`) 39 | 4. Push to the branch (`git push origin my-new-feature`) 40 | 5. Create new Pull Request -------------------------------------------------------------------------------- /sketch-artwork-fetcher.sketchplugin/Contents/Sketch/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "author" : "Maxim Melnikov", 3 | "commands" : [ 4 | { 5 | "script" : "script.cocoascript", 6 | "handler" : "onRun", 7 | "shortcut" : "cmd ctrl l", 8 | "name" : "Fetch Artworks From Last.fm", 9 | "identifier" : "sketch-artwork-fetcher" 10 | } 11 | ], 12 | "menu" : { 13 | "items" : [ 14 | "sketch-artwork-fetcher" 15 | ], 16 | "title" : "Artwork Fetcher" 17 | }, 18 | "identifier" : "com.example.sketch.f6c1cab6-9e35-47ff-8259-a0cf0cc12a56", 19 | "version" : "1.2", 20 | "description" : "Plugin to fetch artworks by their album or artist names", 21 | "authorEmail" : "mxmmlnkv@gmail.com", 22 | "compatibleVersion" : "3.6", 23 | "homepage": "http://github.com/mmth/sketch-artwork-fetcher", 24 | "name" : "Artwork Fetcher" 25 | } 26 | -------------------------------------------------------------------------------- /sketch-artwork-fetcher.sketchplugin/Contents/Sketch/script.cocoascript: -------------------------------------------------------------------------------- 1 | var onRun = function(context) { 2 | var doc = context.document, 3 | selection = context.selection, 4 | 5 | coversCollection = [], 6 | coversUrls = [], 7 | urlPattern = /^https?:\/\/(?:[aA-zZ\-]+\.)+[a-z]{2,6}(?:\/[^/#?]+)+\.(?:jpg|gif|png)$/ig 8 | 9 | function getSketchVersionNumber() { 10 | const version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] 11 | var versionNumber = version.stringByReplacingOccurrencesOfString_withString(".", "") + "" 12 | while(versionNumber.length != 3) { 13 | versionNumber += "0" 14 | } 15 | return parseInt(versionNumber) 16 | } 17 | 18 | var sketchVersion = getSketchVersionNumber() 19 | 20 | function alert(msg, title) { 21 | title = title || "Error" 22 | app = [NSApplication sharedApplication] 23 | [app displayDialog: msg withTitle: title] 24 | } 25 | 26 | function get(url) { 27 | var request = NSURLRequest.requestWithURL(NSURL.URLWithString(url)) 28 | var response = NSURLConnection.sendSynchronousRequest_returningResponse_error(request, null, null) 29 | return response; 30 | } 31 | 32 | function getQuery() { 33 | query = [doc askForUserInput: 'Enter artists and/or album names:' initialValue: ''] 34 | // if user entered nothing 35 | if (query == '') { 36 | doc.showMessage('You forgot to enter the query :-(') 37 | getQuery() 38 | // if user cancelled a box 39 | } else if (query === null) { 40 | return 41 | // finally, everything is ok 42 | } else { 43 | fetchCovers(query) 44 | } 45 | } 46 | 47 | function fetchCovers(query) { // fetching cover arts 48 | url = 'http://ws.audioscrobbler.com/2.0/?method=album.search&album=' + encodeURIComponent(query) + '&api_key=2486cfd15a30b079cde22315f0268a2d&format=json' 49 | json = JSON.parse(NSString.alloc().initWithData_encoding(get(url), NSUTF8StringEncoding)) 50 | albums = json.results.albummatches.album // getting list of albums 51 | if (albums.length > 0) { 52 | albums.map(function (album) { 53 | image = album.image.pop()["#text"] 54 | if (image !== "" && image !== undefined) { 55 | coversUrls.push(image); 56 | } 57 | }) 58 | } 59 | (coversUrls.length > 0) ? insertToSketch() : doc.showMessage("Nothing found for '" + query + "' query.") 60 | } 61 | 62 | function loadCovers(layersAmount) { 63 | numberOfCovers = coversUrls.length 64 | for (var i = 0; i < layersAmount; i++) { 65 | r = Math.floor(Math.random() * numberOfCovers) 66 | imageUrl = coversUrls[r] 67 | newImage = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString: imageUrl]] 68 | coversCollection.push(newImage) 69 | } 70 | return coversCollection; 71 | } 72 | 73 | function insertToSketch() { 74 | allLayers = doc.currentPage().layers() 75 | coversCollection = loadCovers(selection.count()) 76 | for (var i = 0; i < selection.count(); i++) { 77 | layer = selection[i] 78 | if ([layer class] == MSShapeGroup) { 79 | imageData = coversCollection[i] 80 | fill = layer.style().fills().firstObject() 81 | fill.setFillType(4) 82 | 83 | if (sketchVersion > 370) { 84 | image = [[MSImageData alloc] initWithImage: imageData convertColorSpace: false]] 85 | fill.setImage(image) 86 | } else if(sketchVersion < 350) { 87 | [fill setPatternImage:imageData collection:[[fill documentData] images]] 88 | } else { 89 | fill.setPatternImage(imageData) 90 | } 91 | fill.setPatternFillType(1) 92 | } 93 | } 94 | } 95 | 96 | function activate() { 97 | if ([selection count] == 0) { 98 | doc.showMessage('Select at least one shape.') 99 | } else { 100 | getQuery() 101 | } 102 | } 103 | 104 | activate(); 105 | }; 106 | -------------------------------------------------------------------------------- /tutorial/fetch.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/din/sketch-artwork-fetcher/15f84dba3af115976e78f6d39a0a75ce0d9430ec/tutorial/fetch.gif --------------------------------------------------------------------------------