Skip to content

Commit

Permalink
Fix Auto/Fast Slice bug; Plugin menu language will same as Sketch lan…
Browse files Browse the repository at this point in the history
…guage; Add Select locked layers.
  • Loading branch information
Ashung Hung committed May 29, 2021
1 parent 2803e2d commit 206a813
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 81 deletions.
26 changes: 17 additions & 9 deletions automate-sketch.sketchplugin/Contents/Resources/manifest_en.json
Expand Up @@ -5,14 +5,22 @@
"email": "Ashung.hung@gmail.com",
"homepage": "https://github.com/Ashung/Automate-Sketch",
"appcast": "https://ashung.github.io/Automate-Sketch/appcast.xml",
"version": "72.0.20210508",
"version": "72.3.20210529",
"identifier": "com.ashung.hung.automate_sketch",
"icon": "icon.png",
"suppliesData": true,
"compatibleVersion": "3",
"bundleVersion": "1",
"scope": "application",
"commands": [
{
"handlers": {
"actions": {
"OpenDocument": "onOpenDocument"
}
},
"script": "Help/Change_Languages.js"
},
{
"name": "Fit to Parent Width with Margin",
"identifier": "fit_to_parent_width_with_margin",
Expand Down Expand Up @@ -100,6 +108,13 @@
"script": "Layer/Select_Layers_By_Type.js",
"icon": "icon_runner.png"
},
{
"name": "Locked Layers",
"identifier": "select_layers_by_type_locked",
"handler": "selectAllLockedLayerInSelection",
"script": "Layer/Select_Layers_By_Type.js",
"icon": "icon_runner.png"
},
{
"name": "Select Layers By Name",
"identifier": "select_layers_by_name",
Expand Down Expand Up @@ -487,12 +502,6 @@
"script": "Help/help.js",
"icon": "icon_runner.png"
},
{
"name": "Change Menu Language",
"identifier": "change_language",
"script": "Help/Change_Languages.js",
"icon": "icon_runner.png"
},
{
"name": "Copy Selected Layer Name",
"identifier": "copy_selected_layer_name",
Expand Down Expand Up @@ -1759,6 +1768,7 @@
"select_layers_by_type_slice",
"select_layers_by_type_exportable",
"-",
"select_layers_by_type_locked",
"select_all_hidden_layers",
"select_transparency_layers"
]
Expand Down Expand Up @@ -2096,8 +2106,6 @@
"change_log",
"report_issue",
"-",
"change_language",
"-",
"disable_google_analytics",
"-",
"donate"
Expand Down
26 changes: 17 additions & 9 deletions automate-sketch.sketchplugin/Contents/Resources/manifest_zh.json
Expand Up @@ -5,14 +5,22 @@
"email": "Ashung.hung@gmail.com",
"homepage": "https://github.com/Ashung/Automate-Sketch",
"appcast": "https://ashung.github.io/Automate-Sketch/appcast.xml",
"version": "72.0.20210508",
"version": "72.3.20210529",
"identifier": "com.ashung.hung.automate_sketch",
"icon": "icon.png",
"suppliesData": true,
"compatibleVersion": "3",
"bundleVersion": "1",
"scope": "application",
"commands": [
{
"handlers": {
"actions": {
"OpenDocument": "onOpenDocument"
}
},
"script": "Help/Change_Languages.js"
},
{
"name": "带边距适配父级宽",
"identifier": "fit_to_parent_width_with_margin",
Expand Down Expand Up @@ -100,6 +108,13 @@
"script": "Layer/Select_Layers_By_Type.js",
"icon": "icon_runner.png"
},
{
"name": "锁定图层",
"identifier": "select_layers_by_type_locked",
"handler": "selectAllLockedLayerInSelection",
"script": "Layer/Select_Layers_By_Type.js",
"icon": "icon_runner.png"
},
{
"name": "基于名称选择图层",
"identifier": "select_layers_by_name",
Expand Down Expand Up @@ -487,12 +502,6 @@
"script": "Help/help.js",
"icon": "icon_runner.png"
},
{
"name": "更改菜单语言",
"identifier": "change_language",
"script": "Help/Change_Languages.js",
"icon": "icon_runner.png"
},
{
"name": "复制选中图层的名称",
"identifier": "copy_selected_layer_name",
Expand Down Expand Up @@ -1759,6 +1768,7 @@
"select_layers_by_type_slice",
"select_layers_by_type_exportable",
"-",
"select_layers_by_type_locked",
"select_all_hidden_layers",
"select_transparency_layers"
]
Expand Down Expand Up @@ -2096,8 +2106,6 @@
"change_log",
"report_issue",
"-",
"change_language",
"-",
"disable_google_analytics",
"-",
"donate"
Expand Down
@@ -1,55 +1,26 @@
var onRun = function(context) {
var onOpenDocument = function(context) {

var ga = require("../modules/Google_Analytics");
ga("Help");
var preferences = require("../modules/Preferences");
var userDefaults = NSUserDefaults.standardUserDefaults();
var sketchLanguage = String(userDefaults.objectForKey("AppleLanguages").firstObject()).replace(/-\w*/, "");
var pluginLanguage = preferences.get("pluginLanguage");

var Dialog = require("../modules/Dialog").dialog;
var ui = require("../modules/Dialog").ui;
if (pluginLanguage != sketchLanguage) {
var languageFileURL = context.plugin.urlForResourceNamed("manifest_" + sketchLanguage + ".json");

var manifestFilePath = context.plugin.url().path() + "/Contents/Sketch/manifest.json";

var supportLanguages = {
"en": "English",
"zh": "简体中文"
};
var supportLanguagesKeys = Object.keys(supportLanguages);
var supportLanguagesValues = [];
for (var key in supportLanguages) {
supportLanguagesValues.push(supportLanguages[""+key+""]);
}

// Dialog
var dialog = new Dialog("Change Menu Language");

var languagesView = ui.popupButton(supportLanguagesValues);
dialog.addView(languagesView);

// Click OK button
var responseCode = dialog.run();
if (responseCode == 1000) {
var languageIndex = languagesView.indexOfSelectedItem();
var languageFileURL = context.plugin.urlForResourceNamed("manifest_" + supportLanguagesKeys[languageIndex] + ".json");
console.log(languageFileURL)
if (languageFileURL) {
var manifestFilePath = context.plugin.url().path() + "/Contents/Sketch/manifest.json";
var languageFilePath = languageFileURL.path();
// Remove manifest.json
NSFileManager.defaultManager().removeItemAtPath_error_(
manifestFilePath, nil
);
NSFileManager.defaultManager().removeItemAtPath_error(manifestFilePath, nil);
// Replace manifest.json
NSFileManager.defaultManager().copyItemAtPath_toPath_error_(
languageFilePath, manifestFilePath, nil
);
NSFileManager.defaultManager().copyItemAtPath_toPath_error(languageFilePath, manifestFilePath, nil);
// Reload Plugin
AppController.sharedInstance().pluginManager().reloadPlugins();
} else {
var alert = require("sketch/ui").alert;
alert(
"Language file not found.",
"Language file \"" + context.plugin.url().path() + "/Contents/Resources/manifest_" + supportLanguagesKeys[languageIndex] + ".json\" does not existed."
);
}

preferences.set("pluginLanguage", sketchLanguage);
}
}


};
Expand Up @@ -72,7 +72,6 @@ var selectAllExportableInSelection = function(context) {
} else {
child.select_byExtendingSelection(true, true);
}

totalCount ++;
}
}
Expand All @@ -88,6 +87,43 @@ var selectAllExportableInSelection = function(context) {

};

var selectAllLockedLayerInSelection = function(context) {
var ga = require("../modules/Google_Analytics");
ga("Layer");

var doc = context.document;
var page = doc.currentPage();
var selection = context.selection;

page.deselectAllLayers();

var count = 0;
if (selection.count() > 0) {
selection.forEach(function(layer) {
layer.children().forEach(function(child) {
if (child.isLocked()) {
child.select_byExtendingSelection(true, true);
count ++;
}
});
});
} else {
page.children().forEach(function(child) {
if (child.isLocked()) {
child.select_byExtendingSelection(true, true);
count ++;
}
});
}

if (count == 0) {
doc.showMessage("No locked layer found.");
} else {
doc.showMessage("Select " + totalCount + " locked layer" + (count > 1 ? "s" : "") + ".");
}

};

function selectLayersInSelectionByType(context, type) {
var ga = require("../modules/Google_Analytics");
ga("Layer");
Expand Down Expand Up @@ -125,15 +161,15 @@ function selectLayersInSelectionByType(context, type) {
} else {
doc.showMessage(`Select ${totalCount} ${type} layers.`);
}

}

function selectLayersInParent_byType(parent, type, callback) {

var layerType = require("../modules/Type");
var appVersion = sketch.version.sketch;
var count = 0;

if (
parent.containsLayers() && parent.class() != "MSShapeGroup"
) {
Expand Down
Expand Up @@ -6,7 +6,6 @@ var onRun = function(context) {
ga("Slice");

var preferences = require("../modules/Preferences");
var help = require("../modules/Help");
var Dialog = require("../modules/Dialog").dialog;
var ui = require("../modules/Dialog").ui;

Expand Down Expand Up @@ -86,10 +85,8 @@ var onRun = function(context) {
}

var slice = MSSliceLayer.sliceLayerFromLayer(layer);
slice.makeOriginIntegral();
allSlices.push(slice);

var msRect = help.getMSRectFromMSLayers([slice, layer]);
slice.absoluteRect().setRect(msRect.rect());

if (sliceWith > 0) {
slice.frame().setWidth(sliceWith);
Expand Down
Expand Up @@ -4,7 +4,6 @@ var runFastSlice = function(context) {
ga("Slice");

var preferences = require("../modules/Preferences");
var help = require("../modules/Help");
var document = context.document;
var exportPresets = MSExportPreset.allExportPresets();

Expand Down Expand Up @@ -57,8 +56,6 @@ var runFastSlice = function(context) {
while (layer = loopSelection.nextObject()) {

var slice = MSSliceLayer.sliceLayerFromLayer(layer);
var msRect = help.getMSRectFromMSLayers([slice, layer]);
slice.absoluteRect().setRect(msRect.rect());
slice.makeOriginIntegral();

// layer order
Expand Down
Expand Up @@ -11,11 +11,14 @@ var onRun = function(context) {

var userDefaults = NSUserDefaults.standardUserDefaults();
var shortcuts = userDefaults.dictionaryForKey("NSUserKeyEquivalents");
var shortcutMenuTitles;

var shortcutMenuTitles = [];
if (shortcuts) {
shortcutMenuTitles = util.toArray(shortcuts.allKeys()).map(function(item) {
return String(item).split("\u001b").splice(3).join("->");
});
} else {
shortcuts = NSDictionary.alloc().init();
}

var commands = __command.pluginBundle().commands();
Expand Down
26 changes: 17 additions & 9 deletions automate-sketch.sketchplugin/Contents/Sketch/manifest.json
Expand Up @@ -5,14 +5,22 @@
"email": "Ashung.hung@gmail.com",
"homepage": "https://github.com/Ashung/Automate-Sketch",
"appcast": "https://ashung.github.io/Automate-Sketch/appcast.xml",
"version": "72.0.20210508",
"version": "72.3.20210529",
"identifier": "com.ashung.hung.automate_sketch",
"icon": "icon.png",
"suppliesData": true,
"compatibleVersion": "3",
"bundleVersion": "1",
"scope": "application",
"commands": [
{
"handlers": {
"actions": {
"OpenDocument": "onOpenDocument"
}
},
"script": "Help/Change_Languages.js"
},
{
"name": "Fit to Parent Width with Margin",
"identifier": "fit_to_parent_width_with_margin",
Expand Down Expand Up @@ -100,6 +108,13 @@
"script": "Layer/Select_Layers_By_Type.js",
"icon": "icon_runner.png"
},
{
"name": "Locked Layers",
"identifier": "select_layers_by_type_locked",
"handler": "selectAllLockedLayerInSelection",
"script": "Layer/Select_Layers_By_Type.js",
"icon": "icon_runner.png"
},
{
"name": "Select Layers By Name",
"identifier": "select_layers_by_name",
Expand Down Expand Up @@ -487,12 +502,6 @@
"script": "Help/help.js",
"icon": "icon_runner.png"
},
{
"name": "Change Menu Language",
"identifier": "change_language",
"script": "Help/Change_Languages.js",
"icon": "icon_runner.png"
},
{
"name": "Copy Selected Layer Name",
"identifier": "copy_selected_layer_name",
Expand Down Expand Up @@ -1759,6 +1768,7 @@
"select_layers_by_type_slice",
"select_layers_by_type_exportable",
"-",
"select_layers_by_type_locked",
"select_all_hidden_layers",
"select_transparency_layers"
]
Expand Down Expand Up @@ -2096,8 +2106,6 @@
"change_log",
"report_issue",
"-",
"change_language",
"-",
"disable_google_analytics",
"-",
"donate"
Expand Down

0 comments on commit 206a813

Please sign in to comment.