Skip to content

Commit

Permalink
Revise 'Apply Expression to Selection' UI, resolve #3
Browse files Browse the repository at this point in the history
  • Loading branch information
zlovatt committed Jul 6, 2022
1 parent fc99e40 commit a9eb7a1
Showing 1 changed file with 68 additions and 29 deletions.
97 changes: 68 additions & 29 deletions Apply Expression to Selection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,85 @@
* Prompts user for an expression, and applies it to selected properties
*
* @author Zack Lovatt <[email protected]>
* @version 0.1.0
* @version 0.2.0
*/
(function applyExpressionToSelection () {
var DEFAULT_EXPRESSION = "loopIn() + loopOut() - value";
var comp = app.project.activeItem;
(function applyExpressionToSelection() {
var DEFAULT_EXPRESSION = "loopIn() + loopOut() - value;";
_getUserExpression(DEFAULT_EXPRESSION);

if (!(comp && comp instanceof CompItem)) {
alert("Open a comp!");
return;
}
/**
* Applies an expression to selected prop(s)
*
* @param {string} expression Expression to apply
*/
function applyExpression(expression) {
var comp = app.project.activeItem;

if (!(comp && comp instanceof CompItem)) {
alert("Open a comp!");
return;
}

var props = comp.selectedProperties;
var props = comp.selectedProperties;

if (props.length === 0) {
alert("Select properties to apply expression to!");
return;
}
if (props.length === 0) {
alert("Select properties to apply expression to!");
return;
}

if (!expression || expression == "") {
alert("Enter an expression");
return;
}

app.beginUndoGroup("Apply Expression to Selected Properties");

var userExpression = prompt("Enter an expression!", DEFAULT_EXPRESSION);
try {
for (var ii = 0, il = props.length; ii < il; ii++) {
var prop = props[ii];

if (!userExpression) {
alert("Enter an expression");
return;
if (!prop.canSetExpression) {
continue;
}

prop.expression = expression;
}
} catch (e) {
alert(e);
} finally {
app.endUndoGroup();
}
}

app.beginUndoGroup("Apply Expression to Selected Properties");
function _getUserExpression(defaultExpression) {
var win = new Window("palette", "Apply Expression to Selection", undefined, {
resizeable: true
});
win.alignChildren = "fill";

win.add("statictext", undefined, "Expression to apply:");

try {
for (var ii = 0, il = props.length; ii < il; ii++) {
var prop = props[ii];
var editText = win.add("edittext", undefined, defaultExpression, {
multiline: true
});
editText.characters = 50;
editText.size = [500, 200];
editText.alignment = "fill";

if (!prop.canSetExpression) {
continue;
var btnOk = win.add("button", undefined, "Apply");
btnOk.onClick = function () {
var userExpression = editText.text;

if (userExpression == "") {
alert("No expression to apply!");
return;
}

prop.expression = userExpression;
}
} catch (e) {
alert(e);
} finally {
app.endUndoGroup();
win.close();
applyExpression(userExpression);
};
btnOk.alignment = "right";

win.show();
}
})();

0 comments on commit a9eb7a1

Please sign in to comment.