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);
}
}
1 Correct answer
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];
Explore related tutorials & articles
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.
Copy link to clipboard
Copied
Hi @Silly-V. The returned item is used in Keyboard Maestro to drive other macros.
Copy link to clipboard
Copied
It's always false or undefined unless you have a return true somewhere in there.
Copy link to clipboard
Copied
Oh, I see. I'll adjust my script accordingly. Thanks!
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];
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. 🙂
Copy link to clipboard
Copied
Sorry, yes, it should have been round brackets.
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!
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
Copy link to clipboard
Copied
@pixxxelschubser , Thanks for your help. This is typically what I'm testing:
After processing, the layer panel looks like this:
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.
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
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?
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.
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.
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:
So, the only way to get layers with such funny names is to use that getByName function.
Copy link to clipboard
Copied
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.
😉
Copy link to clipboard
Copied
I think you are saying the same thing as me.
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?
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"
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.
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

