Skip to content

Instantly share code, notes, and snippets.

@GoodBoyNinja
GoodBoyNinja / genRand.jsx
Created June 7, 2021 21:04
Generate a random number between a min and a max. Limit the decimal places to a specific amount if you want to. Set decimal places to 0 to return a rounded int.
function genRand(min, max, decimalPlaces) {
// you could add some error checking to make sure all arguments exist
var result = Math.random() * (max - min) + min;
if (decimalPlaces > 0) {
var power = Math.pow(10, decimalPlaces);
var result = Math.floor(result * power) / power;
}
if (decimalPlaces === 0) {
result = Math.round(result);
@GoodBoyNinja
GoodBoyNinja / lerp.jsx
Created June 7, 2021 21:02
A basic lerp function for ExtendScript. amt is similar to t in the more conventional naming of lerp. This function is similar to the Linear function inside After-Effects expression, however it does not let you map the range to something else, and the argument list order is different. This function works with arrays of numbers as well (for exampl…
function lerp(start, end, amt) {
if (start == undefined || end == undefined || amt == undefined) {
// can't lerp, start end amount is missing
if (start !== undefined) {
return start;
}
return 0;
}
function oneDLerp(start, end, amt) {
@GoodBoyNinja
GoodBoyNinja / filterColorProps.jsx
Last active November 17, 2023 22:26
filter color properties out of an array of properties.
function FilterColorProperties(propsArray) {
var result = [];
//getting selected colors...
var propsArray =
propsArray == undefined ? (app.project.activeItem.selectedProperties || []) : propsArray;
if (!propsArray || !propsArray.length) {
// no selected colors because no properties are selected
return result;
}
@GoodBoyNinja
GoodBoyNinja / freezeLayerAtTime.jsx
Created June 7, 2021 20:53
pass a layer to freeze it where the time indicator currently is. Pass a specific time (in seconds) to freeze somewhere else.
function freezeLayerAtCurrentTime(layer, specificTime) {
if (!layer) {
// no layer
return false
};
try{
layer.timeRemapEnabled = true;
var timeRemapProp = layer("ADBE Time Remapping");
@GoodBoyNinja
GoodBoyNinja / separateGroupsAndProperties.jsx
Created June 7, 2021 20:51
This function takes an array of properties and separates them into properties, groups and indexed groups. It returns an object, you you can later on access the array you are interested in. Example: var variableName = separateGroupsAndProperties([app.project.activeItem.selectedProperties]).groups;
function separateGroupsAndProperties (propsArray) {
// sorting an array of props to props an group... return an object
var resultObject = {
all: [],
props: [],
groups: [],
indexedGroups: [],
};
@GoodBoyNinja
GoodBoyNinja / getItemsByName.jsx
Created June 7, 2021 20:46
Loops through the After-Effects project items and returns an array of items that match the name that you passed into the function
function GetItemsByName (nameString) {
// returns array of found matches
if (!nameString) {
return false;
}
var matches = [];
for (var i = 1; i <= app.project.numItems; i++) {
var crnt = app.project.item(i);
var crntName = crnt.name;
@GoodBoyNinja
GoodBoyNinja / checkGumroadLicense.jsx
Created June 7, 2021 20:42
If you are planning on uploading a script to Gumroad you can use this function in order to contact Gumroad through the script, and receive a response. Note that it uses JSON.parse, which means you need to have json2.js included in your code in order to convert the response into an object. You can learn more about it here: https://help.gumroad.co…
function GumrdLicenseRequest(licenseKeyInput, incrementUseCount, showDialogs) {
// better check here if there is access to files and network
var showDialogs = (showDialogs == undefined) ? true : showDialogs;
licenseKeyInput == undefined ? undefined : licenseKeyInput;
incrementUseCount == undefined ? false : String(incrementUseCount);
if (
@GoodBoyNinja
GoodBoyNinja / pressed.jsx
Last active November 17, 2023 22:27
A small group of functions to let you intuitively check one of ctrl, shift or alt is pressed. Example: alert(pressed.shift())
var pressed = new (function () {
this.shift = function () {
if (ScriptUI.environment.keyboardState.shiftKey) {
return true;
}
return false;
};
this.alt = function () {
if (ScriptUI.environment.keyboardState.altKey) {
return true;
@GoodBoyNinja
GoodBoyNinja / clampNum.jsx
Last active November 17, 2023 22:27
Clamp a number to be in a range. You can also use this to clamp an array of numbers (like an rgb array for example)
function ClampNum(num, min, max, inErrReturn) {
function SingleChannelClamp(num, min, max) {
if (
isNaN(num) ||
min == undefined ||
isNaN(min) ||
isNaN(max) ||
max == undefined
) {
return num;
@GoodBoyNinja
GoodBoyNinja / modNum.jsx
Last active November 17, 2023 22:27
Modulate a number, including negative numbers.
function ModNum (n, m) {
try {
if (n >= m || n <= 0) {
return ((n % m) + m) % m;
} else {
return n;
}
} catch (e) {
// maybe print or alert an error
return n;