Skip to content

Instantly share code, notes, and snippets.

View MarshySwamp's full-sized avatar

MarshySwamp

View GitHub Profile
@MarshySwamp
MarshySwamp / Remove Filename Extension.jsx
Last active February 6, 2024 00:26
Code Snippet: Remove Filename Extension (3 different methods)
/* Remove Filename Extension (4 different methods) */
var baseName = app.activeDocument.name.split('.')[0];
// or for multiple period characters
var extensionIndex = activeDocument.name.lastIndexOf(".");
if (extensionIndex != -1) {
var baseName = activeDocument.name.substr(0, extensionIndex);
alert(baseName);
@MarshySwamp
MarshySwamp / Save and Restore Dialogs or Rulers.jsx
Created November 16, 2019 10:57
Code Snippet: Save and Restore Dialogs or Rulers
// Save the current ruler units and set to pixels
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Do stuff...
// Restore the ruler units
app.preferences.rulerUnits = savedRuler;
// Save the current dialog display settings
var savedDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
@MarshySwamp
MarshySwamp / Error Check for Open Doc.jsx
Last active January 4, 2020 23:10
Code Snippet: Error Check for Open Document
/* Start Open Document Error Check - Part A: If */
if (app.documents.length > 0) {
/* Main Code Start */
alert('Do something here!');
/* Main Code Finish */
}
/* Finish Open Document Error Check - Part A: If */
@MarshySwamp
MarshySwamp / Error Check for Open or Unsaved Doc.jsx
Last active January 4, 2021 00:30
Code Snippet: Error Check for Open/Unsaved Doc
if (!documents.length) {
alert('There are no documents open.', 'No Document');
} else {
alert('You have a document open!');
}
// or
if (app.documents.length > 0) {
@MarshySwamp
MarshySwamp / Remove px from document width and height.jsx
Last active July 11, 2022 13:39
Code Snippet: Remove ' px' from document width and height
// Remove ' px' from document width and height
app.preferences.rulerUnits = Units.PIXELS;
var w = app.activeDocument.width.toString().replace(' px', '');
var h = app.activeDocument.height.toString().replace(' px', '');
//or
var w = app.activeDocument.width.value;
var h = app.activeDocument.height.value;
//or
@MarshySwamp
MarshySwamp / remove all.jsx
Created November 24, 2019 00:43
Code Snippet: Bulk removal of common Photoshop document items
// remove all paths
app.activeDocument.pathItems.removeAll();
// remove all alphas
app.activeDocument.channels.removeAll();
// remove all layercomps
app.activeDocument.layerComps.removeAll();
// remove all color samplers
@MarshySwamp
MarshySwamp / Move Layer Content.jsx
Created December 17, 2019 03:52
Code Snippet: Move Layer Content (Translate)
// Move the current layer in 5px from the right and down 5px from the top
app.activeDocument.activeLayer.translate( -5, 5 );
@MarshySwamp
MarshySwamp / Run Action.jsx
Last active March 16, 2021 22:07
Code Snippet: Run Action
app.doAction("Molten Lead","Default Actions.atn");
// or
var actionName = "Molten Lead"; // Action to run
var actionSet = "Default Actions.atn"; // Action set to run
app.doAction(actionName,actionSet);
// or
@MarshySwamp
MarshySwamp / Run Menu Item - Collapse All Groups.jsx
Last active April 15, 2022 16:52
Code snippet: Run menu item - Collapse All Groups
/* An example of running a menu item command.
Use Script Listener to find the menu name, often by inserting the menu command into an action */
// Collapse all layer groups/sets
app.runMenuItem(stringIDToTypeID('collapseAllGroupsEvent'));
// another example:
// More info:
@MarshySwamp
MarshySwamp / Deselect All Layers.jsx
Created January 3, 2020 15:21
Code Snippet: Deselect All Layers (2 Different Methods)
// Deselect All Layers
// forums.adobe.com/message/5204655#5204655 - Michael L Hale
app.runMenuItem(stringIDToTypeID('selectNoLayers'));
// Or...
// Deselect All Layers (AM Code Through Clean SL)
selectNoLayers();
function selectNoLayers() {
var c2t = function (s) {