Problems with Photoshop's onChanging function - photoshop

I'm trying to change some label text depending on the state of a checkbox for Photoshop Script UI. Only it's not updating at all. No idea why.
Ideally I'd like it to say "Some text" when ticked and "Some other text" when left unchecked. And change dynamically.
Here's my code
// DIALOG
// ======
var dlg = new Window("dialog");
dlg.text = "Title";
dlg.preferredSize.width = 180;
dlg.preferredSize.height = 100;
dlg.orientation = "column";
dlg.alignChildren = ["center","top"];
dlg.spacing = 10;
dlg.margins = 16;
var statictext1 = dlg.add("statictext", undefined, undefined, {name: "statictext1"});
statictext1.text = "Some static text";
statictext1.justify = "center";
var checkbox1 = dlg.add("checkbox", undefined, undefined, {name: "checkbox1"});
checkbox1.text = "Some text";
checkbox1.value = true;
// GROUP1
// ======
var group1 = dlg.add("group", undefined, {name: "group1"});
group1.orientation = "row";
group1.alignChildren = ["left","center"];
group1.spacing = 10;
group1.margins = 0;
// add buttons
group1.add ("button", undefined, "OK");
group1.add ("button", undefined, "Cancel");
// Define behavior for when the slider value changes
dlg.checkbox1.onChanging = function()
{
var textArr = ["Some text", "Some other text"]
var val = dlg.checkbox1.value;
// Update the label text with the current checkbox value.
dlg.checkbox1.text = textArr[val];
}
var myReturn = dlg.show();

The Checkbox control fires the onClick event and not the onChanging event.
The only controls that fire the onChanging event are:
EditText
Scrollbar
Slider
Therefore consider changing this part in your script:
// Define behavior for when the slider value changes
dlg.checkbox1.onChanging = function() {
// ...
}
to the following instead:
// Define behavior for when the checkbox value changes
dlg.checkbox1.onClick = function() {
var textArr = ["Some text", "Some other text"]
var val = checkbox1.value ? textArr[0] : textArr[1];
statictext1.text = val;
}
Explanation
In the body of the preceding function for the onClick event, i.e. the part that reads;
var val = checkbox1.value ? textArr[0] : textArr[1];
we utilize the conditional (ternary) operator to check the value property of checkbox1. If checkbox1 is checked (true) we assign the string "Some text" to the val variable, otherwise if checkbox1 is unchecked (false) we assign the string "Some other text" to the val variable instead.
Additional changes:
The following changes also need to be carried out:
As checkbox1's initial value is set to true, i.e. it's checked, you'll need to change line number 13 from this:
statictext1.text = "Some static text";
to this:
statictext1.text = "Some text";
Also consider setting the size property for statictext1. As you can see on line number 15 below the following has been added:
statictext1.size = [180, 20];
This will ensure it's width accomodates the "Some other text" string.
Revised script:
Here is the full revised script:
// DIALOG
// ======
var dlg = new Window("dialog");
dlg.text = "Title";
dlg.preferredSize.width = 180;
dlg.preferredSize.height = 100;
dlg.orientation = "column";
dlg.alignChildren = ["center","top"];
dlg.spacing = 10;
dlg.margins = 16;
var statictext1 = dlg.add("statictext", undefined, undefined, {name: "statictext1"});
statictext1.text = "Some text"; // <--- Note: Also change this !
statictext1.justify = "center";
statictext1.size = [180, 20]; // <--- Note: Also set the width/height !
var checkbox1 = dlg.add("checkbox", undefined, undefined, {name: "checkbox1"});
checkbox1.text = "Some text";
checkbox1.value = true;
// GROUP1
// ======
var group1 = dlg.add("group", undefined, {name: "group1"});
group1.orientation = "row";
group1.alignChildren = ["left","center"];
group1.spacing = 10;
group1.margins = 0;
// add buttons
group1.add ("button", undefined, "OK");
group1.add ("button", undefined, "Cancel");
// Define behavior for when the checkbox value changes
dlg.checkbox1.onClick = function() {
var textArr = ["Some text", "Some other text"]
var val = checkbox1.value ? textArr[0] : textArr[1];
statictext1.text = val;
}
var myReturn = dlg.show();

Related

Updating Adobe Illustrator content with script, without closing GUI

I am currently building an Adobe Illustrator Script, which starts a GUI and performs some text changes within the open Illustrator file. Unfortunately, the changes are only seen after closing the GUI. Is there some kind of way to preview the changes triggered by the GUI?
Here is what I have so far:
function startGUI() {
// DIALOG
// ======
var dialog = new Window("dialog");
dialog.text = "Dialog";
dialog.orientation = "column";
dialog.alignChildren = ["center","top"];
dialog.spacing = 10;
dialog.margins = 16;
// GRPNAME
// =======
var grpName = dialog.add("group", undefined, {name: "grpName"});
grpName.orientation = "row";
grpName.alignChildren = ["left","center"];
grpName.spacing = 10;
grpName.margins = 0;
var titleName = grpName.add("statictext", undefined, undefined, {name: "titleName"});
titleName.text = "Name";
titleName.preferredSize.width = 64;
var valName = grpName.add('edittext {properties: {name: "valName"}}');
valName.text = "Ela Ayah Kayis";
valName.preferredSize.width = 200;
// GRPFONTSIZE
// ===========
var grpFontSize = dialog.add("group", undefined, {name: "grpFontSize"});
grpFontSize.preferredSize.width = 250;
grpFontSize.orientation = "row";
grpFontSize.alignChildren = ["left","top"];
grpFontSize.spacing = 10;
grpFontSize.margins = 0;
grpFontSize.alignment = ["left","top"];
var titleFontSize = grpFontSize.add("statictext", undefined, undefined, {name: "titleFontSize"});
titleFontSize.text = "Font Size";
titleFontSize.preferredSize.width = 64;
var valFontSize = grpFontSize.add('edittext {properties: {name: "valFontSize"}}');
valFontSize.text = "15";
valFontSize.preferredSize.width = 30;
valFontSize.alignment = ["center","center"];
var titleFontSizeUnit = grpFontSize.add("statictext", undefined, undefined, {name: "titleFontSizeUnit"});
titleFontSizeUnit.text = "pt";
titleFontSizeUnit.alignment = ["left","center"];
var sliderFontSize = grpFontSize.add("slider", undefined, undefined, undefined, undefined, {name: "sliderFontSize"});
sliderFontSize.minvalue = 11;
sliderFontSize.maxvalue = 15;
sliderFontSize.value = 15;
sliderFontSize.preferredSize.width = 135;
sliderFontSize.onChanging = function(){
valFontSize.text = Math.round(sliderFontSize.value)
}
// DIALOG
// ======
var divider1 = dialog.add("panel", undefined, undefined, {name: "divider1"});
divider1.alignment = "fill";
// GROUP BUTTONS
// =============
var group1 = dialog.add("group", undefined, {name: "group1"});
group1.orientation = "row";
group1.alignChildren = ["right","center"];
group1.spacing = 10;
group1.margins = 0;
group1.alignment = ["center","top"];
var btnReplaceNames = group1.add("button", undefined, undefined, {name: "btnReplaceNames"});
btnReplaceNames.text = "Replace Names";
btnReplaceNames.justify = "left";
btnReplaceNames.alignment = ["right","bottom"];
var btnClose = group1.add("button", undefined, undefined, {name: "btnClose"});
btnClose.text = "Close";
// Event Listener for the replace button
btnReplaceNames.onClick = function() {
var inputText = valName.text;
updateNames(inputText, Math.round(sliderFontSize.value));
dialog.update();
}
// Event listener for the quit button
btnClose.onClick = function() {
dialog.close(); }
dialog.show();
}
function updateNames(chosenName, fontSize) {
var txtFrames = app.activeDocument.textFrames;
$.writeln("start");
if (txtFrames.length > 0) {
for ( i = 0; i < txtFrames.length; i++ ) {
if (txtFrames[i].name == 'Text-Name') {
txtFrames[i].textRange.characterAttributes.size = fontSize;
txtFrames[i].textRange.characterAttributes.leading = fontSize;
txtFrames[i].contents = chosenName;
// $.writeln(txtFrames[i].textRange.characters.length);
// $.writeln(txtFrames[i].textRange.lines.length);
}
}
}
$.writeln("done");
}
startGUI();
Closing the GUI and starting it automatically also didn't solve the issue.
....
updateNames(inputText, Math.round(sliderFontSize.value));
dialog.update();
app.redraw(); // < add this, and it will work.
....

how can I set a vars value insted of the var

I'm making buttons and when i make them i set the onclick to use i and i want them to use the value of i when the button was made not when it was clicked
i cannot make the buttons manually there are over 3000
while (i < 3634) {
const button = document.createElement("button");
button.innerText = swf[i];
button.type = "submit";
button.name = swf[i];
button.onclick = function () {
nes(swf[i])
};
button.classList.add("button");
document.body.appendChild(button);
i++
}
`
thank you
I used the buttons name instead of setting the name and function separately
while (i < 3633) {
const button = document.createElement("button");
button.innerText = swf[i];
button.type = "submit";
button.name = swf[i];
button.id= swf[i];
button.onclick = function () {
nes(button.name)
};
button.id= button.id + swf[i] + ")";
button.classList.add("button");
document.body.appendChild(button);
i++
}

Include a lambda inside an if..else statement

I have the following function:
fun ValueListItem(
label: String,
value: String,
onClick: (() -> Unit)? = null
) {
}
But calling it like this is not allowed:
var edited = false
ValueListItem(
label = "my label",
value = "some value",
onClick = if (viewmodel.canEdit) { edited = true } else null
)
But calling it like this is allowed:
var edited = false
val onEditDateTime = {
edited = true
}
ValueListItem(
label = "my label",
value = "some value",
onClick = if (viewmodel.canEdit) onEditDateTime else null
)
How can I pass the lambda in a conditional statement rather than having to declare the lambda as an extra construct.
By writing
ValueListItem(
label = "my label",
value = "some value",
onClick = if (viewmodel.canEdit) { edited = true } else null
)
The compiler sees the braces as part of the if-statement syntax.
Simply adding another pair of braces like this should do the trick I think
ValueListItem(
label = "my label",
value = "some value",
onClick = if (viewmodel.canEdit) {{ edited = true }} else null
)

Global Variables only Called Once then Lost their Scope

I Have a Problem with my Script and hope helping me, and thanks in advance, i still learning javascript and try to learn somthing new everyday, i have a script that draw modeless interface (palette) and have button including (option) that make another new palette (for options), i made the variables as globals for the option palette, but the problem is the global variables is only called once!, and the script lose the global variable scope!.
enter image description here
so my question is how to make the variables not losing its scope and retain in the memory? as long the script run, as an Example if the user move the slider and hit (Show alert due options) its only run once and then lose the scope, even the slider no longer interact with the user and update text box, please test the code to see the problem, and thank again for any help or advice.
Best
M.Hasanain
//Global Variables only Called Once then Lost their Scope!
#targetengine "session1";
var w = new Window("palette", {independent:true}); //Main Palette Windows
var findoptions = new Window("palette"); //Options Palette
function Main() {
// Check to see whether any InDesign documents are open.
// If no documents are open, display an error message.
if (app.documents.length > 0) {
var myDoc = app.activeDocument;
}else{
// No documents are open, so display an error message.
alert("No InDesign documents are open. Please open a document and try again.");
}
}
//---------------------------------------------------------
//Making Palettes Windows
//---------------------------------------------------------
#targetengine "session1";
var w = new Window("palette", {independent:true}); //Main Palette Windows
var findoptions = new Window("palette"); //Options Palette
//gqmanager is the (GREP Query Manager) outside the main Function
w.text = "Test the Connection Between Global Variables and Palettes";
w.preferredSize.width = 500;
w.alignChildren = ["center", "center"]; //"left";
w.orientation = "column"; //"row";
w.spacing = 10;
w.margins = 16;
//Parent - Input Panel Prepare
var InputPanel = w.add("panel", undefined, undefined, { name: "panel1" });
InputPanel.text = "Text Find : ";
InputPanel.preferredSize.width = 1000;
InputPanel.orientation = "row";
InputPanel.alignChildren = ["center", "center"];
InputPanel.spacing = 10;
InputPanel.margins = 16;
//Children - input Panel Inside Prepare
var myInputPanelInside = InputPanel.add("group", undefined, { name: "myInput" });
//--Adding Find What
myInputPanelInside.add("statictext", undefined, "Find What :");
//myInputPanelInside.alignment = "center";
var myGREPString = myInputPanelInside.add("edittext", undefined, "SAMPLE");
myGREPString.helpTip = "Enter Your Text"
myGREPString.characters = 20;
myGREPString.enabled = true;
myGREPString.preferredSize.width = 460;
var Button1 = myInputPanelInside.add("button", undefined, "Options");
//Parent - Radio Panel Prepare
var RadioPanel = w.add("panel", undefined, undefined, { name: "panel2" });
RadioPanel.text = "Select Desired Option : ";
RadioPanel.preferredSize.width = 1000;
RadioPanel.orientation = "row";
RadioPanel.alignChildren = ["center", "center"];
RadioPanel.spacing = 10;
RadioPanel.margins = 16;
//Children - input Panel Inside Prepare
var myRadioPanelInside = RadioPanel.add("group", undefined, { name: "myRadio" });
myRadioPanelInside.preferredSize.width = 500;
myRadioPanelInside.alignChildren = ["center", "center"];
//Adding Radio Buttons
var radio1 = myRadioPanelInside.add("radiobutton", undefined, "Option 1");
var radio2 = myRadioPanelInside.add("radiobutton", undefined, "Option 2");
var radio3 = myRadioPanelInside.add("radiobutton", undefined, "Option 3");
radio1.preferredSize.width = 200;
radio2.preferredSize.width = 200;
radio3.preferredSize.width = 200;
//Previous Default Condition
radio1.value = true;
var myButtonGroup = w.add("group");
myButtonGroup.alignment = "center";
var Button2 = myButtonGroup.add("button", undefined, "Show Alert Due Options");
var Button3 = myButtonGroup.add("button", undefined, "Exit");
Button1.onClick = function () {
CalltheFindOptions();
}
Button2.onClick = function () { Find(); };
function Find() {
doRadioButtonOpt();
}
Button3.onClick = function() {Canceled();};
function Canceled() {
ExitSure();
}
//After Drawing Interface
var a = w.show();
function ExitSure() {
var a = w.close();
exit(0);
}
//User Selection for Radio Buttons
function doRadioButtonOpt() {
myDoc = app.activeDocument;
if (radio1.value == true) {
TestVars();
}
}
function TestVars() {
#targetengine "session1";
var myDoc = app.activeDocument
var TimeMs = Number(SliderControlText.text); //Converting Text to Number
//Show Results Found as User Wish
if (DontShowResults.value == true) { //no Show only Apply
alert("you Select not to Show Results!");
}else{ //Direct Show and Apply
if (ShowResultsDirect.value == true) {
alert("you Select to Show Results in real time!");
}else{ //Show and Apply By WaitinhTime!
if (ShowResults.value == true) { //Show and Apply
alert("you Select to Show Results with Specific time!");
$.sleep(TimeMs); //Wait ms
}
}
}
alert("Do you need somthing else?, try again", "Finish Report");
}
var DontShowResults;
var ShowResultsDirect;
var ShowResults;
var SliderControlText;
var slider;
//--------------------------------------------Building the Find Options Palette-----------------------------------------//
//--------------------------------------------------------------------------------------------------------------------------------//
function CalltheFindOptions() {
#targetengine "session1";
//Find Options Window
findoptions.text = "Find Options";
//Parent - Input Panel Prepare
SelectPanel = findoptions.add("panel", undefined, undefined, { name: "panel1" });
SelectPanel.text = " Find Options : ";
SelectPanel.preferredSize.width = 1000;
SelectPanel.orientation = "row";
SelectPanel.alignChildren = ["center", "center"];
SelectPanel.spacing = 10;
SelectPanel.margins = 16;
//Children - input Panel Inside Prepare
mySelectPanelInside = SelectPanel.add("group", undefined, { name: "mySelOpt" });
DontShowResults = mySelectPanelInside.add("checkbox", undefined, "Don't Show Results");
DontShowResults.value = true; //by Default
DontShowResults.alignment = "left";
ShowResultsDirect = mySelectPanelInside.add("checkbox", undefined, "Show Results");
ShowResultsDirect.value = false; //by Default
ShowResults = mySelectPanelInside.add("checkbox", undefined, "Show Results Delayed in milliseconds(Ms) :");
ShowResults.value = false; //by Default
//Adding Slider to Control MS Time
SliderControlText = mySelectPanelInside.add ("edittext", undefined, 10, {readonly: false}); //read only prevent user Entering Nums
SliderControlText.characters = 3;
slider = mySelectPanelInside.add ("slider {minvalue: 1, maxvalue: 100, value: 10}");
//Slider Listener Plus SliderControl Text Listener
slider.onChanging = function () {SliderControlText.text = slider.value;} //Listen to Slider
var c = findoptions.show();
}
If I understand you correctly you need to use as global variables values rather than the objects. And you can change the values of global variables by onClick events.
Here is the bottom part of your code (the rest part of the code wasn't changed):
...
function TestVars() {
#targetengine "session1";
var myDoc = app.activeDocument
var TimeMs = Number(SliderControlText_text); //Converting Text to Number
//Show Results Found as User Wish
if (DontShowResults_value == true) { //no Show only Apply
alert("you Select not to Show Results!");
} else { //Direct Show and Apply
if (ShowResultsDirect_value == true) {
alert("you Select to Show Results in real time!");
} else { //Show and Apply By WaitinhTime!
if (ShowResults_value == true) { //Show and Apply
alert("you Select to Show Results with Specific time!");
$.sleep(TimeMs); //Wait ms
}
}
}
alert("Do you need somthing else?, try again", "Finish Report");
}
// values! not objects
var DontShowResults_value = true;
var ShowResultsDirect_value = false;
var ShowResults_value = false;
var SliderControlText_text = '10';
var slider_value = 10;
//--------------------------------------------Building the Find Options Palette-----------------------------------------//
//--------------------------------------------------------------------------------------------------------------------------------//
function CalltheFindOptions() {
#targetengine "session1";
//Find Options Window
findoptions.text = "Find Options";
//Parent - Input Panel Prepare
SelectPanel = findoptions.add("panel", undefined, undefined, {
name: "panel1"
});
SelectPanel.text = " Find Options : ";
SelectPanel.preferredSize.width = 1000;
SelectPanel.orientation = "row";
SelectPanel.alignChildren = ["center", "center"];
SelectPanel.spacing = 10;
SelectPanel.margins = 16;
//Children - input Panel Inside Prepare
var mySelectPanelInside = SelectPanel.add("group", undefined, {
name: "mySelOpt"
});
var DontShowResults = mySelectPanelInside.add("checkbox", undefined, "Don't Show Results");
DontShowResults.value = DontShowResults_value; //by Default
DontShowResults.alignment = "left";
// change the global variable by click
DontShowResults.onClick = function() { DontShowResults_value = DontShowResults.value }
var ShowResultsDirect = mySelectPanelInside.add("checkbox", undefined, "Show Results");
ShowResultsDirect.value = false; //by Default
// change the global variable by click
ShowResultsDirect.onClick = function() { ShowResultsDirect_value = ShowResultsDirect.value }
var ShowResults = mySelectPanelInside.add("checkbox", undefined, "Show Results Delayed in milliseconds(Ms) :");
ShowResults.value = ShowResults_value; //by Default
// change the global variable by click
ShowResults.onClick = function() { ShowResults_value = ShowResults.value }
//Adding Slider to Control MS Time
// SliderControlText = mySelectPanelInside.add("edittext", undefined, 10, {
var SliderControlText = mySelectPanelInside.add("edittext", undefined, SliderControlText_text, {
readonly: false
}); //read only prevent user Entering Nums
SliderControlText.characters = 3;
var slider = mySelectPanelInside.add("slider {minvalue: 1, maxvalue: 100, value: 10}");
// change slider every time as numbers is changes
SliderControlText.onChanging = function() {slider.value = SliderControlText.text};
//Slider Listener Plus SliderControl Text Listener
slider.onChanging = function () {
SliderControlText.text = slider.value;
// change the global variable by onChange
SliderControlText_text = SliderControlText.text;
} //Listen to Slider
findoptions.show();
}
It seems it works as intended.
As far as I can tell (I can be wrong), the cause of the problem was that the objects ShowResultsDirect, ShowResultsDirect, etc, disappears as soon as you close the palette. Because they are elements of the palette. They can't keep values if the palette is closed. That why they worked well only when you open the palette first time.

Creating dynamic labels for dojo bar chart

I have bar chart, whose x-axis labels is generated dynamically from db, I am getting these values through ajax call data
'{value:1,text:"x"},{value:2,text:"Vikash"},{value:3,text:"y"},{value:4,text:"z"}'.
How to pass these values to label parameter. I tried passing it as an array and also as json but nothing seems to work.. Any ideas..
Here is my code:
makeBarChart=function() {
animChart = new dojox.charting.Chart2D("animChart");
animChart.setTheme(dojox.charting.themes.MiamiNice)
.addAxis("x",{
labels: [ myLabelSeriesarray ], gap: 20}).
addAxis("y", {
vertical: true,
fixLower: "major",
fixUpper: "major",
includeZero: true
}).
addPlot("default", {
type: "ClusteredColumns",
gap: 10
}).
addSeries("Series A", closedSeries).
addSeries("Series B", othersSeries).
render();
};
dojo.addOnLoad(makeBarChart);
jsp code:
var labelDis = new Array(pieData.length); // pieData is the JSON value getting from Java
for(var i = 0;i<pieData.length;i++){
labelDis[i] = new Array(2);
labelDis[i]['value'] = i + 1;
labelDis[i]['text'] = pieData[i]['axis'];
}
Java/Servlet code:
JSONObject jSONObject = new JSONObject();
jSONObject.put("axis", "value from database");
jSONArray.put(jSONObject);
out.print(jSONArray.toString());
Suppose you have received data in json format through ajax call and you want to create dynamic labels using that data.
var jsonArray= ["label1","label2","label3"];
var labelsArray = [];
for (var i = 0; i < jsonArray.length; i++,) {
var obj = {};
obj.value = i;
obj.text = jsonArray[i];
labelsArray.push(obj);
}
Set the labels in x Axis to this labelsArray
this.chart1.addAxis("x", {
title: "Collection Date",
titleOrientation: "away",
titleGap:20,
labels:labelsArray
})