Skip to content

Instantly share code, notes, and snippets.

@explorador
Last active October 4, 2017 02:20
Show Gist options
  • Save explorador/72684c2f99603b3aed4b33fb052f3503 to your computer and use it in GitHub Desktop.
Save explorador/72684c2f99603b3aed4b33fb052f3503 to your computer and use it in GitHub Desktop.
/**
* Replace Layer Name
*
* Paste here:
* /Applications/Adobe Photoshop Version/Presets/Scripts/Replace Layer Name.jsx
*/
var docRef = app.activeDocument;
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
function newGroupFromLayers(doc) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putClass( sTID('layerSection') );
desc.putReference( cTID('null'), ref );
var lref = new ActionReference();
lref.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
desc.putReference( cTID('From'), lref);
executeAction( cTID('Mk '), desc, DialogModes.NO );
};
function undo() {
executeAction(cTID("undo", undefined, DialogModes.NO));
};
function getSelectedLayers(doc) {
var selLayers = [];
newGroupFromLayers();
var group = doc.activeLayer;
var layers = group.layers;
for (var i = 0; i < layers.length; i++) {
selLayers.push(layers[i]);
}
undo();
return selLayers;
};
var selectedLayers = getSelectedLayers(app.activeDocument);
// Getting the command contents
function getSystemCommandStdout (command)
{
var stdout = "";
var tempFile = new File (Folder.temp + "/temp.txt");
app.system (command + " > " + tempFile.fsName);
if (tempFile.open ("r"))
{
stdout = tempFile.read ();
tempFile.close ();
tempFile.remove ();
}
return stdout;
}
// Using Terminal/Command LIne
function GetClipboard(){
var clipboard;
if(File.fs == "Macintosh"){
clipboard = getSystemCommandStdout("pbpaste");
} else {
clipboard = getSystemCommandStdout("Get-Clipboard");
}
return clipboard;
}
// Make String url friendly
function string_to_slug(str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, "-")
.replace(/[^\w\-]+/g, "")
.replace(/\-\-+/g, "-")
.replace(/^-+/, "")
.replace(/-+$/, "");
return str;
}
for( i = 0; i < selectedLayers.length; i++) {
selectedLayers[i].selected = true;
docRef.activeLayer = selectedLayers[i];
docRef.activeLayer.name = string_to_slug( GetClipboard() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment