Thread View: adobe.scripting.indesign
11 messages
11 total messages
Started by Skempy
Wed, 18 Feb 2009 16:09
[JS CS4] Why is this If Else test not working?
Author: Skempy
Date: Wed, 18 Feb 2009 16:09
Date: Wed, 18 Feb 2009 16:09
22 lines
1097 bytes
1097 bytes
The following script pops up an alert showing me the results of a live Preflight running in a document.
var myPreflight = app.preflightProcesses[0];
var myReport = myPreflight.processResults;
alert(myReport);
If there are no errors then the alert says "None".
My test below doesn't work and gives the "Errors found" alert even if there are no Preflight errors in the document.
if (myReport == "None")
{
alert ("No errors found");
}
else
{
alert ("Errors found");
}
Can anyone explain where I am going wrong.
Thanks
Re: [JS CS4] Why is this If Else test not working?
Author: Dave_Saunders
Date: Wed, 18 Feb 2009 16:32
Date: Wed, 18 Feb 2009 16:32
7 lines
1146 bytes
1146 bytes
It can be very confusing when you use an alert like this to test an assumption. The alert code is quite smart. It knows it can only post a string to the alert dialog, so it bends over backwards to convert whatever you send it into a string. So, chances are the myReport is not a string (I've not done any actual coding with this part of the object model) but something else which when forced to a string by the alert code comes out as "None". Put a breakpoint before the alert and use ESTK's data browser to work out what's going on. Dave
Re: [JS CS4] Why is this If Else test not working?
Author: Skempy
Date: Wed, 18 Feb 2009 17:25
Date: Wed, 18 Feb 2009 17:25
13 lines
938 bytes
938 bytes
Thanks Dave, I haven't used the Data Browser up to now but I have done what you suggested and can see the reference to myReport at the end of the list in the Data Browser. The value alongside it is None. I am not sure if I am looking at the right thing. Can you suggest a good resource for learning to use the ESTK. Thanks Simon
Re: [JS CS4] Why is this If Else test not working?
Author: Dave_Saunders
Date: Wed, 18 Feb 2009 17:44
Date: Wed, 18 Feb 2009 17:44
9 lines
950 bytes
950 bytes
You are looking at the right thing. There is a guide available in ESTK's Help menu that's pretty good, I think. Try using the JavaScript Console. With the script at the breakpoint type: myReport.constructor.name into the Console and see what you get. If it says String, then there must be an invisible character in there of some kind. Dave
Re: [JS CS4] Why is this If Else test not working?
Author: Skempy
Date: Wed, 18 Feb 2009 18:00
Date: Wed, 18 Feb 2009 18:00
11 lines
835 bytes
835 bytes
Dave, Yes, it does give String as the result. How do I go about finding the hidden character. Or is there another way of testing if a Preflight has no errors? I will definately commit to learnig more about the ESTK. Thanks Simon
Re: [JS CS4] Why is this If Else test not working?
Author: Dave_Saunders
Date: Wed, 18 Feb 2009 19:08
Date: Wed, 18 Feb 2009 19:08
7 lines
867 bytes
867 bytes
Instead of sending it to an alert, insert it into an empty InDesign text frame and then see what appears. Assuming you prime an insertion point before running your script, all you have to do is replace the alert with: app.selection[0].contents = myReport; Dave
Re: [JS CS4] Why is this If Else test not working?
Author: Skempy
Date: Thu, 19 Feb 2009 07:47
Date: Thu, 19 Feb 2009 07:47
7 lines
740 bytes
740 bytes
Dave, This gives "None" followed by a paragraph mark and then a forced line break. Can I test just the first 4 characters for a match? Simon
Re: [JS CS4] Why is this If Else test not working?
Author: Skempy
Date: Thu, 19 Feb 2009 08:04
Date: Thu, 19 Feb 2009 08:04
21 lines
1055 bytes
1055 bytes
Dave,
This works:
var myPreflight = app.preflightProcesses[0];
var myReport = myPreflight.processResults;
var myTest = myReport.indexOf("None");
if (myTest == 0)
{
alert ("No errors found");
}
else
{
alert ("Errors found");
}
If there are no preflight errors then myTest returns "0", if there are any preflight errors found then myTest returns "-1".
Thank you for your time and for pointing me in the right direction.
Simon.
Re: [JS CS4] Why is this If Else test not working?
Author: Dirk_Becker
Date: Thu, 19 Feb 2009 11:07
Date: Thu, 19 Feb 2009 11:07
11 lines
977 bytes
977 bytes
The toSource() function is often helpful in such cases. alert(myReport.toSource()); Native objects just yield a "resolve...", use their properties property to drill down. alert(myObject.properties.toSource()); Of course, instead of alert() there is also the ESTK console. Dirk
Re: [JS CS4] Why is this If Else test not working?
Author: Harbs
Date: Thu, 19 Feb 2009 12:08
Date: Thu, 19 Feb 2009 12:08
7 lines
823 bytes
823 bytes
I would strip out trailing white space like this: myReport = myReport.replace(/\s+$/, ''); -- Harbs http://www.in-tools.com
Re: [JS CS4] Why is this If Else test not working?
Author: Skempy
Date: Thu, 19 Feb 2009 13:42
Date: Thu, 19 Feb 2009 13:42
7 lines
789 bytes
789 bytes
Thanks Harbs, I like this approach. I recognise this as Grep and I know that \s+ is any sequence of whitespace, tabs and returns. But what does $ do? And the first and last / ? Thanks Simon
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads