PDA

View Full Version : Determine if a text frame exists


step
November 5th, 2012, 12:17 PM
Is it possible to determine whether a text frame of a certain name exists?

I'm populating (via OnRecordStart) a series of text frames with variable information and if the text frame doesn't exist for a version, I don't want FP to attempt to assign content to it. I realize that this won't be an issue during composition but I'm mainly asking in order to not get errors at validation.

Here's what I'm trying to do:

(FindTextFrame("Rate1_" + Field("version")).exists) ? FindTextFrame("Rate1_" + Field("version")).content = Field("rate1") : "";
(FindTextFrame("Rate2_" + Field("version")).exists) ? FindTextFrame("Rate2_" + Field("version")).content = Field("rate2") : "";

scubajbc
November 5th, 2012, 01:20 PM
You should be able to use...

try
{
whatever code
}

catch (e)
{
if whatever code errors, run this code
}

ThomasLewis
January 12th, 2018, 11:26 AM
Sorry to pull up such an old post but I was unable to find anything similar. I'm running into the same issue where I need to check if a frame exists in validation.

Using the Try/Catch method still causes the validator to return "null". Is there any way around that?

Dan Korn
January 16th, 2018, 09:23 AM
Sorry to pull up such an old post but I was unable to find anything similar. I'm running into the same issue where I need to check if a frame exists in validation.

Using the Try/Catch method still causes the validator to return "null". Is there any way around that?
I don't know what you mean. I'm not very good at guessing what's in your rule without seeing it.

A try/catch statement doesn't make a rule return anything, unless there's a return statement inside the catch block.

Also, you should be calling FindTextFrame inside of the OnRecordStart callback rule, which doesn't need to return anything anyway.

So... I'm not sure what the problem is in some code that I can't see.

Dan Korn
January 16th, 2018, 03:08 PM
Maybe this will help. Here's how I would answer the original question. Again, these examples would all be in the OnRecordStart rule.

try
{
FindTextFrame("Rate1_" + Field("version")).content = Field("rate1");
}
catch (e)
{
}

try
{
FindTextFrame("Rate2_" + Field("version")).content = Field("rate2");
}
catch (e)
{
}

Or, more generally:
var myFrame;
try
{
myFrame = FindTextFrame("your frame name");
}
catch (e)
{
Print('Text frame not found!'); // <- or skip this if you want
}
if (myFrame)
{
myFrame.content = "Hello there!"; // <- or whatever else you need to do
}

You could even make a function:
function TryToFindTextFrame(name)
{
try
{
return FindTextFrame(name);
}
catch (e)
{
Print('Text frame "' + name + '" not found!'); // <- or skip this if you want
return null;
}
}
And call it like so:
var myFrame = TryToFindTextFrame("your frame name");
if (myFrame)
myFrame.content = "Hello there!";
Or you could make a function to do whatever you need:
function SetTextFrameContent(frameName, content)
{
try
{
FindTextFrame(frameName).content = content;
}
catch (e)
{
Print('Text frame "' + frameName+ '" not found!'); // <- or skip this if you want
}
}
And simply call that:
SetTextFrameContent("your frame name", "Hello there!");
Or, back to the original question:
SetTextFrameContent("Rate1_" + Field("version"), Field("rate1"));
SetTextFrameContent("Rate2_" + Field("version"), Field("rate2"));

ThomasLewis
January 17th, 2018, 10:50 AM
Those examples were all very helpful.

I'll try to explain where the issue is. I have a series of templates that all use very similar javascript for the OnRecordStart callback but also require the operator to modify the text frames on a per job basis.

Instead of maintaining all the code in each template I am using the Load function to bring in a single JS file. These templates will be used by operators that would be unable to diagnose javascript issues but can understand errors returned by validation.

The thought was to have them simply open OnRecordStart and tap the validate button. If everything checks out, great. But if one of the text frames is missing, then it would return an error specified in the script.

This is where my confusion came in. If you put return "test" directly into OnRecordStart, it will actually return "test" to the user. However, if you put return "test", or anything else, into an externally loaded JS file, it returns "null".

I was attempting the try/catch method and instead of nothing happening, I wanted to use return to return an actual "hey, you forgot to put in the text frame", instead it returns "null".

So, the examples all work fine, I guess the real issue is I am unable to return a custom message when text frame is not found.

Dan Korn
January 17th, 2018, 12:52 PM
Instead of maintaining all the code in each template I am using the Load function to bring in a single JS file. These templates will be used by operators that would be unable to diagnose javascript issues but can understand errors returned by validation.
Okay. The explanation is somewhat helpful. But still, I don't have any of your files, so I can only look at this through a very narrow keyhole.
The thought was to have them simply open OnRecordStart and tap the validate button. If everything checks out, great. But if one of the text frames is missing, then it would return an error specified in the script.

This is where my confusion came in. If you put return "test" directly into OnRecordStart, it will actually return "test" to the user. However, if you put return "test", or anything else, into an externally loaded JS file, it returns "null".

That's not true. At least that's not true in my experience. I've written plenty of external JS files, and put returns in them, and had the return value show up when I validate a rule that's loading an external JS file and calling functions in it.

I suspect that you're putting the "return" in the wrong place. The thing about an external JS file is that it doesn't "return" anything. Your external JS file can have one or more functions in it, and your rules can call those functions, and each function can return something, but that's not the same as just putting a "return" statement in the JS file, outside of any function.

Again, though, instead of having to guess at what's in your JS file, it would be easier to for me diagnose it if I could look at it.

Furthermore, if there's really an error that you want the template designer to see, then instead of simply returning a message, you probably want to throw it instead, so that it'generates an exception. This has a couple of advantages over simply a return value: First, the box that pops up when you click Validate will have an error icon with it, which denotes an error condition much more explicitly than just the default message box with the title "Expressions OK". Second, the error message pops up when you try to save the rule, even if you don't click Validate. More about how to throw an exception instead of just returning a message below....

Also, putting on my moderator hat, it seems that your question is not really about determining whether a frame exists (the original question in this thread). Your question is really about how to return an error from an external JS file to the Rule Editor at validation time. So it really should be a new thread for this new question. And even if we figure it out the answer to your question here, that answer will only serve to confuse anyone who finds this thread later and just wants to know the answer to the original question, especially since it's highly unlikely that they'll be using an external JS file.

I was attempting the try/catch method and instead of nothing happening, I wanted to use return to return an actual "hey, you forgot to put in the text frame", instead it returns "null".
Again, I think that's because you're returning something in the wrong place, but I can't really know for sure without seeing your code.
So, the examples all work fine, I guess the real issue is I am unable to return a custom message when text frame is not found.
"... from an external JS file," is really the end of that statement. The code I provided in my previous post does answer the original question.

So, let's try to answer your question about returning an error from an external JS file.

First of all, what's wrong with the exception message that gets thrown by default when the a frame with the name passed to FindTextFrame isn't found? You know, "Error in FindFrame, no frame named 'Rate1_whatever'"?

Anyway, since you don't like that message, you can certainly throw whatever exception message you want, like so:
function TryToFindTextFrame(name)
{
try
{
return FindTextFrame(name);
}
catch (e)
{
throw 'Hey, you forgot to put in the text frame named "' + name + '"!';
}
}
You can even add to the default exception message if you want:
function TryToFindTextFrame(name)
{
try
{
return FindTextFrame(name);
}
catch (e)
{
throw "Error calling external JS function: " + e;
}
}
Or you can throw an exception for any condition you want, such as if some kind of business logic is not met, or a field value doesn't conform to some kind of pattern, or basically anything. It doesn't even have to be in an existing "catch" block; you can just throw whenever; for instance:
if (!Field("version"))
throw "Invalid data!";

Learning how to throw and handle exceptions effectively is an important part of writing code, especially code that other people will be using. Professional programmers often talk about a variation of the "80/20 rule," which says that you'll spend 20 percent of the time writing the code for any program to handle cases where all of the inputs are in the correct and expected formats, and 80 percent of the time writing code to try to gracefully handle invalid data and other error conditions and report meaningful error messages back to the user of your code, or the end user of the program.

ThomasLewis
January 17th, 2018, 03:01 PM
Attached is my template with the external JS file. When you open OnRecordStart and click validate, it returns "null". If I paste the code from the JS file directly into OnRecordStart, it returns the error as you would expect.

I get where you are coming from trying to keep things organized. Hopefully this will make more sense with the attached sample.

step
January 17th, 2018, 03:54 PM
Try putting your logic in a function (called "validateTextFrames" for example) and then call it from your OnRecordStart callback.

JavaScript file:

function validateTextFrames() {
// Your required frames.
var frames = [
'Your First Text Frame Name',
'Your Second Text Frame Name'
];

for (var i in frames) {
try {
var frameName = frames[i];
if (FindTextFrame(frameName))
delete frames[i];
} catch (e) {}
}
if ((frames = frames.filter(String)).length)
ReportError('Missing required text frame(s):\n"' + frames.join('"\n"') + '"');
}


OnRecordStart callback:

Load("errortest.js");
validateTextFrames();


As far as returning a value from your external JS file, you have to return the function in your FP callback:
JS file:

function testReturn() {
return 'hello from your external js file!';
}


OnRecordStart:

Load("errortest.js");
return testReturn();

ThomasLewis
January 17th, 2018, 04:27 PM
Adding that extra function call directly to OnRecordStart did the trick. It's working perfectly now. It's just a matter of updating all the templates to include that extra line. Thanks so much for all the help :)

Dan Korn
January 18th, 2018, 09:56 AM
Attached is my template with the external JS file. When you open OnRecordStart and click validate, it returns "null". If I paste the code from the JS file directly into OnRecordStart, it returns the error as you would expect.

I get where you are coming from trying to keep things organized. Hopefully this will make more sense with the attached sample.
Thanks. As I expected, you're not actually returning anything in your rule. There's no "return" statement in there.

As I said before, and as Step demonstrates, the proper way to use an external JS file (which, again, is a topic pretty far afield from the original question in this thread) is to put a function in there, then call that function after loading the file.

Merely calling the Load function doesn't execute any code in the external JS file; though it will cause any objects and functions declared in the JS file to be included in the active JavaScript context (which allows you to use/call them after calling Load).

The reason that calling Load from your JS file causes a box saying that you're returning null to pop up is because you're trying to call a function outside the context of another function or rule, on the last line, where you're calling the TryToFindTextFrame function. (Which I never suggested to do, by the way; I only said you should declare it in the JS file, not call it in the JS file.) But as the message box notes, the return value from a callback rule has no effect on the composition anyway.

Step's solution is great, but I would put the Load call in the JavaScript Globals, instead of in OnRecordStart. For one thing, you don't need to load it over and over again on every record. You could put it in OnJobStart instead, but then it won't necessarily get loaded up when you validate another rule. The purpose of the JavaScript Globals is to declare objects and functions, which is exactly what you're doing by loading the JS file.

Anyway, having said that, I'm still not sure that you're gaining that much by doing all of this. If your end user template designer tries to call FindTextFrame on a frame that doesn't exist, that will already raise an exception and put up the box with the error icon when they validate or try to save the rule. All this work does is simply put up a slightly different error message saying basically the same thing.

But maybe I'm missing something else that's not shown in the very minimal example job you posted. If I knew more about the real job where you "require the operator to modify the text frames on a per job basis", I might be able to offer more specific suggestions, or maybe even another approach.

ThomasLewis
January 18th, 2018, 11:09 AM
With the previous way I was trying to do this, where the only javascript in the entire template was the Load function, no errors appear at all at save and validation only would result in a "null" message. This is why I was trying to change that message, I was just unaware that could not be done fully in the external file when everything else in there was working just fine.

Whether it is supposed to execute or not, everything in the actual production JS file I was loading in did preview and render in the final output. Now that I am calling it as a function, I get the proper errors that you would expect. It all make's a lot more sense now thanks to both of your help.