Copy link to clipboard
Copied
I have sifted thru posts, but can't find a solution ...
I want to execute a script from a button that will evaluate many image fields to determine if an image is assigned to the field. The purpose is to clear a hidden text field of the original path of the imported icon in each. I have tried:
var isIcon = this.getField("Image17_af_image").value;
which returns "null"
var isIcon = this.getField("Image17_af_image").buttonGetIcon();
which returns [object Icon]
var isIcon = util.iconStreamFromIcon(this.getField("Image17_af_image").buttonGetIcon());
which returns [object Stream]
Is it possible to get a "Yes"/"No" response?
Copy link to clipboard
Copied
Maybe the width and height of the icon object will give interesting info. Maybe not; perhaps there is always some kind of image assigned, even if it is blank.
Copy link to clipboard
Copied
You can do it like this:
var buttonIcon = this.getField("Button3").buttonGetIcon();
try {
var buttonIconStream = util.iconStreamFromIcon(buttonIcon);
var streamLength = buttonIconStream.width*buttonIconStream.height;
app.alert("Icon size: " + streamLength);
} catch (e) {
app.alert("No icon found.");
}
Copy link to clipboard
Copied
Thanks try67! I'll be looking at "try" functionality. Expect it will be very useful.
Code performing as needed:
var i = 0
for (var i=16;i<=112;i++)
if(i<70 || i>71){
{
s=i+"";
}
var buttonIcon = this.getField("Image"+s+"_af_image").buttonGetIcon();
try {
var buttonIconStream = util.iconStreamFromIcon(buttonIcon);
var streamLength = buttonIconStream.width*buttonIconStream.height;
// app.alert(streamLength);
}
catch (e) {
getField("FilePost"+s).value="";
getField("Text"+s).value="";
// app.alert("No icon found.");
}
}
Copy link to clipboard
Copied
You can comment out the streamLength variable, too.
The try-catch clause is used to catch run-time errors and handle them properly (instead of just existing the code immediately). The iconStreamFromIcon method throws such an error when you use it on an empty Icon object.
Copy link to clipboard
Copied
THX! You're an awesome resource. The streamLength components are good to know anyway. Added to my personal toolkit.
Can you send me a link to try-catch doc and the return codes for catch?
Copy link to clipboard
Copied
try-catch is explained here: https://www.w3schools.com/js/js_errors.asp
The documentation of the iconStreamFromIcon method doesn't actually mention that it throws an error when the Icon object is empty. I found that out myself and then used that information to catch the error and handle it. This is the error message you get without the try-catch clause:
RaiseError: Expected a dict object.
Util.iconStreamFromIcon:2:Console undefined:Exec
===> Expected a dict object.
Copy link to clipboard
Copied
Thanks again try67. Will be reviewing the doc.
Nothing beats the significant hands-on you have.
Copy link to clipboard
Copied
I'm using the following script to set any one of over 100 images I have on a form.
** Thanks try67! **
I want to conditionally "clear image" instead of assigning an image, without using a blank file. Is this do-able?
s = event.target.name;
s1 = s.substr(5,s.length-14);
FP = "FilePost"+s1
//app.alert(FP);
var cRtn = app.response (
{
cQuestion:"Paste the Image File Name",
cTitle:"Assign Image",
//bPassword:true,
//cDefault: global.cLastPswd,
cLabel:"File Name:"});
if(cRtn && cRtn.length) {
}
else app.alert("File name invalid or not present");
var strDIPath = "/" + cRtn.replace(/\:?\\/g,"/");
var strDIPath = strDIPath.replace(/"/g,"");
var cRtn = strDIPath;
event.target.buttonImportIcon(cRtn);
getField(FP).value=cRtn;
Copy link to clipboard
Copied
No, a script can't clear an field's image, unfortunately.
Copy link to clipboard
Copied
Sounds to me like an easy hole to plug. Oh well. Thanks!