Testing a variable?

Adobe Community Professional ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

I'm sure there's a simple answer to this. I'm stumped.

We track items in production by "PIN" numbers. These are typically not number but a series of digits and letters. When the "PIN" starts with a number the test fails. How can I force the argument to be handled as a string and not a number? I tried to use "toString" method without success.
This is the "test" portion of the script:

standardCrop ('100515', 'Fingerprint');

function standardCrop(PIN, printType) {
    var aDoc = app.activeDocument;
    // var PIN = PIN.toString();
    var fileName = aDoc.name;
    var myLayer = aDoc.activeLayer;
    var itemsCount = myLayer.pageItems.length;
    var thePrint = myLayer.pageItems[PIN];
    if (fileName.indexOf(PIN) == -1) {
        alert("File name does not match: " + PIN)
        return (false);
    }
    if (thePrint.name.indexOf(PIN) == -1) {
        alert("Selected Print name does not match: " + PIN)
        return (false);
    }
}
TOPICS
Scripting

Views

164

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Enthusiast , Jan 08, 2021 Jan 08, 2021
I think Illustraror converts the indices of its collections from stings to numbers (which is the opposite of core JS).  Try var thePrint = myLayer.pageItems.getByName[PIN];

Likes

Translate

Translate
Adobe Community Professional ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

I think by the looks of it, your problem is that your function is not set up to return anything except False, or undefined which is also falsy.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

Hi @Silly-V. The returned item is used in Keyboard Maestro to drive other macros.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

It's always false or undefined unless you have a return true somewhere in there.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

Oh, I see. I'll adjust my script accordingly. Thanks!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

I think Illustraror converts the indices of its collections from stings to numbers (which is the opposite of core JS).  Try

 

var thePrint = myLayer.pageItems.getByName[PIN];

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

@femkeblanco , That did it! Although the name or number goes in ( ) I think.

Thanks for the suggestion. Been struggling with that for a while. 🙂

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

Sorry, yes, it should have been round brackets.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

Hi @femkeblanco 

Your code is correct with round brackets.

@femkeblanco  wrote:


"… I think Illustraror converts the indices of its collections from stings to numbers (which is the opposite of core JS) …"

 

However, your assumption is wrong!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

Please give a few examples of your filenames.

And give a screenshot of your Layers Panel with the "PIN" item visible.

 

IMHO there is no need to do such complicated operations.

See if this already helps:

var myString = "Abracadabra";
var myResult = myString.match(RegExp("123"));
$.writeln(myResult);  // result: null

var myString = "123Abracadabra";
var myResult = myString.match(RegExp("123"));
$.writeln(myResult);  // result: 123
var myString = "Abra123cadabra";
var myResult = myString.match(RegExp("123"));
$.writeln(myResult);  // result: 123
var myString = "Abracadabra123";
var myResult = myString.match(RegExp("123"));
$.writeln(myResult);  // result: 123
var myString = "123";
var myResult = myString.match(RegExp("123"));
$.writeln(myResult);  // result: 123

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

@pixxxelschubser , Thanks for your help. This is typically what I'm testing:

rcraighead_0-1610144386222.png

After processing, the layer panel looks like this:

rcraighead_1-1610144657698.png

 

There have been issues with the wrong PIN being used to name the group so I've tried to build in a test against the locked "PIN". Here are a few PIN examples:

AMKQ89RM

DHN7ETNK

DA6ENJNY

6ED00CDD

 

My knowledge of RegEx is very limited. I realize I need to learn more. Thanks for the examples.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

var thePrint = myLayer.pageItems[PIN];

expects a number

 

for example

var thePrint = myLayer.pageItems[0];

is the top most page item

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

Sorry, we replied at the same time.

 

I still don't quite understand what you really want. The character sequences (PIN examples) are file names AND also the names of a page element? Correct?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

Yes. When an artist locks an order a PIN variable is captured from our web-based fulfillment app. That number is used to name a PDF and groups within the file. On rare occasions a layer group gets named incorrectly (wrong PIN number). This test is meant to catch these errors before the file is uploaded to the fulfillment app. The names are also tested in subsequent processes.

 

I could just pull the PIN from the file name to name the groups, but I feel that would mask any errors that occur.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Jan 09, 2021 Jan 09, 2021

Copy link to clipboard

Copied

Ok.

Thank you for showing some PIN sequences. Are these always combinations of 8 digits and/or letters?

 

But we need a bit more. Please show the associated file names in order to be able to create a specific rule.

eg prefix-PIN-suffix.extension

 

Then it should be easy to extract the PIN and compare whether a group with the same name exists.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

Hmm, but they let us access some things via a name, don't they? I just remembered: And they do - but those kinds of things will always have a name. Layers always get a name and so do swatches and some other things.

So a test on layers has these findings:

If you name a layer the number 0 and 1, etc and use layers[0] or layers["0"], it will honor the index. So if your layer at 0 index is named "1" and layer at 1 index is named "0", it will say the name is "1".

If there's only one layer and its name is "1", it will still produce an error:

Silly-V_0-1610145901084.png

So, the only way to get layers with such funny names is to use that getByName function.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

@Silly-V 

we are the same opinion.

But it seems these are groups and not layers or sublayers.

 

And I still don't know what @rcraighead really want.

 

 

             [ Edited ]

Oops.

I missreading your post a bit.

@ wrote:
"… If you name a layer the number 0 and 1, etc and use layers[0] or layers["0"], it will honor the index. So if your layer at 0 index is named "1" and layer at 1 index is named "0", it will say the name is "1" …"


is a little different:

If you name a layer the number 0 and 1, etc and use layers[0] or layers["0"], it will honor the index 0. So if your layer at 0 index is named "1" and layer at 1 index is named "0", it will say the name is "1"…

… and layer at 1 index is named "0" and use layers[1] or layers["1"], it will say the name is "0" 

 

Please read my posting:

https://community.adobe.com/t5/illustrator/testing-a-variable/m-p/11738110?page=2#M258723

Hope that makes a bit more sense for you.

😉

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 09, 2021 Jan 09, 2021

Copy link to clipboard

Copied

LATEST

I think you are saying the same thing as me. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

If you have

- an item at index 0 named "A"; and

- an item at index 1 named "0"

 

alert( activeDocument.pathItems["0"].name )  // "A"

 

So I assumed "0" is convereted to 0.  Is that not the case?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

That is true.

But you got me wrong.

 

The "index" is the "key" object of an array and not a name. And a valid number in a string in "key" object is always treated as a number. This is much more critical (for example) in PHP

 

 

// valid in PHP not Adobe Extendscript
arr[0]  // key is digit 0
arr["0"]  // key is digit 0
arr["0.1"]  // key is digit 0
arr[false]  // key is digit 0
arr[true]  // key is digit 1

arr["00"]  // key is string "00"
arr["+0"]  // key is string "0"

 

 

 

In Adobe Extendscript only a valid integer in a string will stay as integer.

 

 

var aDoc = app.activeDocument;
$.writeln(aDoc.layers["0"].name);  // key is digit 0  - the same as layer[0]
$.writeln(aDoc.layers["08"].name);  // key is string "08" - the same as layers.getByName("08")
$.writeln(aDoc.layers["8x"].name);  // key is string "8x" - the same as layers.getByName("8x")

 

 

 

 

And your example:

@femkeblanco  wrote:

If you have

- an item at index 0 named "A"; and

- an item at index 1 named "0"

 

alert( activeDocument.pathItems["0"].name )  // "A"

 

So I assumed "0" is convereted to 0.  Is that not the case?


is right.

and furthermore:

alert( activeDocument.pathItems["1"].name )  // "0"

 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

Yes, I think we are at cross-purposes here. In my mind "index" and "name" are interchangeable because collections in Illustrator are array-like objects (i.e. both array-like and objects) and I have always used the bracket notation to access objects, whether using an index or name.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

alert( activeDocument.pathItems["0345"].name ) // not a problem - pathItem by name
alert( activeDocument.pathItems["3450"].name ) // PROBLEM - pathItem by key with Integer value

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines