Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Burns committed Nov 3, 2017
0 parents commit 73bda8b
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
21 changes: 21 additions & 0 deletions LICENSE.md
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Jason Burns (Sonburn)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
@@ -0,0 +1,54 @@
![Unused Style Remover](https://raw.githubusercontent.com/sonburn/unused-style-remover/master/logo.png)

Remove unused layer and text styles.

![Unused Style Remover](https://raw.githubusercontent.com/sonburn/unused-style-remover/master/Screenshots/Symbol%20Swapper.png)

<a href="http://bit.ly/SketchRunnerWebsite">
<img width="160" height="41" src="http://bit.ly/RunnerBadgeBlue" alt="runner-badge-blue">
</a>

<a href="https://sketchpacks.com/sonburn/unused-style-remover/install">
<img width="160" height="41" src="http://sketchpacks-com.s3.amazonaws.com/assets/badges/sketchpacks-badge-install.png" >
</a>

<a href="https://www.paypal.me/sonburn">
<img width="160" height="41" src="https://raw.githubusercontent.com/DWilliames/PDF-export-sketch-plugin/master/images/paypal-badge.png">
</a>

# Usage

* cmd option shift u - Remove unused layer and text styles

# Installation

## Automatic
Search for Unused Style Remover in [Sketchrunner](http://sketchrunner.com/), [Sketchpacks](https://sketchpacks.com/), or [Sketch Toolbox](http://sketchtoolbox.com/) if you have one of those installed.

Once installed, Sketch will automatically notify you when an update is available (version 0.1 and later).

## Manual

1. Download and open unused-style-remover-master.zip
2. Navigate to Unused Style Remover.sketchplugin and copy/move to your plugins directory

To find your plugins directory...

1. In the Sketch menu, navigate to Plugins > Manage Plugins...
2. Click the cog in the lower left of the plugins window, and click Reveal Plugins Folder

# Changelog

* **0.1** - Initial commit.

# Contact

<a class="twitter-follow-button" href="https://twitter.com/sonburn">Follow @sonburn</a>

# Support

If you find this plugin helpful, consider shouting me ☕️ via <a href="https://www.paypal.me/sonburn">PayPal</a>.

# License

Copyright (c) 2017 Jason Burns (Sonburn). See LICENSE.md for further details.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions Unused Style Remover.sketchplugin/Contents/Sketch/manifest.json
@@ -0,0 +1,26 @@
{
"author" : "Jason Burns",
"commands" : [
{
"name" : "Unused Style Remover",
"shortcut" : "cmd option shift u",
"identifier" : "styleRemover",
"description" : "Remove unused layer and text styles.",
"script" : "script.cocoascript",
"icon" : "icon-sr.png"
}
],
"menu" : {
"title" : "Unused Style Remover",
"items" : [
"styleRemover"
],
"isRoot" : true
},
"identifier" : "com.sonburn.sketchplugins.unused-style-remover",
"version" : "0.1",
"description" : "Remove unused layer and text styles.",
"authorEmail" : "jason.burns@synchronoss.com",
"name" : "Unused Style Remover",
"appcast" : "https://raw.githubusercontent.com/sonburn/unused-style-remover/master/appcast.xml"
}
176 changes: 176 additions & 0 deletions Unused Style Remover.sketchplugin/Contents/Sketch/script.cocoascript
@@ -0,0 +1,176 @@
var strPluginName = "Unused Style Remover";

var onRun = function(context) {
var alertWindow = COSAlertWindow.new(),
pluginIconPath = context.plugin.urlForResourceNamed("icon.png").path(),
pluginIcon = NSImage.alloc().initByReferencingFile(pluginIconPath);

alertWindow.setIcon(pluginIcon);
alertWindow.setMessageText(strPluginName);
alertWindow.setInformativeText("Remove unused layer and text styles.");

var contentFrameWidth = 300,
contentFrameHeight = 192,
contentFrameGutter = 15;

var unusedLayerStyles = getUnusedStyles(0);

var layerStyleWidth = contentFrameWidth - contentFrameGutter,
layerStyleHeight = 24,
layerStyleLabel = createBoldLabel("Unused Layer Styles (" + unusedLayerStyles.length + ")",12,NSMakeRect(0,0,contentFrameWidth,16)),
layerStyleFrame = createScrollView(NSMakeRect(0,0,contentFrameWidth,contentFrameHeight)),
layerStyleContent = createContentView(NSMakeRect(0,0,layerStyleWidth,unusedLayerStyles.length*layerStyleHeight)),
layerStyleCount = 0,
layerStyleCheckboxes = [];

for (var i = 0; i < unusedLayerStyles.length; i++) {
var unusedLayerStyle = createCheckbox({name:unusedLayerStyles[i].name(),value:i},1,NSMakeRect(0,layerStyleHeight*layerStyleCount,layerStyleWidth,layerStyleHeight));

layerStyleCheckboxes.push(unusedLayerStyle);
layerStyleContent.addSubview(unusedLayerStyle);

layerStyleCount++;
}

layerStyleFrame.setDocumentView(layerStyleContent);

var unusedTextStyles = getUnusedStyles(1);

var textStyleWidth = contentFrameWidth - contentFrameGutter,
textStyleHeight = 24,
textStyleLabel = createBoldLabel("Unused Text Styles (" + unusedTextStyles.length + ")",12,NSMakeRect(0,0,contentFrameWidth,16)),
textStyleFrame = createScrollView(NSMakeRect(0,0,contentFrameWidth,contentFrameHeight)),
textStyleContent = createContentView(NSMakeRect(0,0,textStyleWidth,unusedTextStyles.length*textStyleHeight)),
textStyleCount = 0,
textStyleCheckboxes = [];

for (var i = 0; i < unusedTextStyles.length; i++) {
var unusedTextStyle = createCheckbox({name:unusedTextStyles[i].name(),value:i},1,NSMakeRect(0,textStyleHeight*textStyleCount,textStyleWidth,textStyleHeight));

textStyleCheckboxes.push(unusedTextStyle);
textStyleContent.addSubview(unusedTextStyle);

textStyleCount++;
}

textStyleFrame.setDocumentView(textStyleContent);

alertWindow.addAccessoryView(layerStyleLabel);
alertWindow.addAccessoryView(layerStyleFrame);
alertWindow.addAccessoryView(textStyleLabel);
alertWindow.addAccessoryView(textStyleFrame);

alertWindow.addButtonWithTitle("Remove Unused Styles");
alertWindow.addButtonWithTitle("Close");

var alertResponse = alertWindow.runModal();

if (alertResponse == 1000) {
var layerStylesToRemove = NSMutableArray.array(),
textStylesToRemove = NSMutableArray.array();

for (var i = 0; i < unusedLayerStyles.length; i++) {
if (layerStyleCheckboxes[i].state() == 1) layerStylesToRemove.addObject(unusedLayerStyles[i]);
}

for (var i = 0; i < unusedTextStyles.length; i++) {
if (textStyleCheckboxes[i].state() == 1) textStylesToRemove.addObject(unusedTextStyles[i]);
}

for (var i = 0; i < layerStylesToRemove.length; i++) {
context.document.documentData().layerStyles().removeSharedStyle(layerStylesToRemove[i]);
}

for (var i = 0; i < textStylesToRemove.length; i++) {
context.document.documentData().layerTextStyles().removeSharedStyle(textStylesToRemove[i]);
}

context.document.reloadInspector();

context.document.showMessage(layerStylesToRemove.length + " layer styles, and " + textStylesToRemove.length + " text styles were removed");
} else return false;
}

function createCheckbox(item,state,frame) {
var checkbox = NSButton.alloc().initWithFrame(frame),
state = (state == false) ? NSOffState : NSOnState;

checkbox.setButtonType(NSSwitchButton);
checkbox.setBezelStyle(0);
checkbox.setTitle(item.name);
checkbox.setTag(item.value);
checkbox.setState(state);

return checkbox;
}

function createBoldLabel(text,size,frame) {
var label = NSTextField.alloc().initWithFrame(frame);

label.setStringValue(text);
label.setFont(NSFont.boldSystemFontOfSize(size));
label.setBezeled(0);
label.setDrawsBackground(0);
label.setEditable(0);
label.setSelectable(0);

return label;
}

function createContentView(frame) {
var view = NSView.alloc().initWithFrame(frame);

view.setFlipped(1);

return view;
}

function createScrollView(frame) {
var view = NSScrollView.alloc().initWithFrame(frame);

view.setHasVerticalScroller(1);

return view;
}

function getUnusedStyles(type) {
var usedStyleIDs = [],
pageLoop = MSDocument.currentDocument().pages().objectEnumerator(),
page;

while (page = pageLoop.nextObject()) {
var predicate = (type == 0) ? NSPredicate.predicateWithFormat("className != %@ && style.sharedObjectID != nil","MSTextLayer") : NSPredicate.predicateWithFormat("className == %@ && style.sharedObjectID != nil","MSTextLayer"),
layers = page.children().filteredArrayUsingPredicate(predicate),
layerLoop = layers.objectEnumerator(),
layer;

while (layer = layerLoop.nextObject()) {
var styleID = String(layer.style().sharedObjectID());

if (usedStyleIDs.indexOf(styleID) == -1) {
usedStyleIDs.push(styleID);
}
}
}

var unusedStyles = NSMutableArray.array(),
styles = (type == 0) ? MSDocument.currentDocument().documentData().layerStyles().objects() : MSDocument.currentDocument().documentData().layerTextStyles().objects(),
styleLoop = styles.objectEnumerator(),
style;

while (style = styleLoop.nextObject()) {
var styleID = String(style.objectID());

if (usedStyleIDs.indexOf(styleID) == -1) {
unusedStyles.addObject(style);
}
}

var sortByName = NSSortDescriptor.sortDescriptorWithKey_ascending("name",1);

return unusedStyles.sortedArrayUsingDescriptors([sortByName]);
}

function removeDuplicates(value,index,self) {
return self.indexOf(value) === index;
}
20 changes: 20 additions & 0 deletions appcast.xml
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Unused Style Remover</title>
<link>http://sparkle-project.org/files/sparkletestcast.xml</link>
<description>Remove unused layer and text styles.</description>
<language>en</language>
<item>
<title>Version 0.1</title>
<description>
<![CDATA[
<ul>
<li>Initial commit.</li>
</ul>
]]>
</description>
<enclosure url="https://github.com/sonburn/unused-style-remover/archive/master.zip" sparkle:version="0.1" />
</item>
</channel>
</rss>
Binary file added logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 73bda8b

Please sign in to comment.