Copy link to clipboard
Copied
I'm trying to assign properties of placedItem.file. However, whenever I try to access placedItem.file it prints/returns "undefined is not an object".
I've used the alert command to print the object e.g. alert(placedItem.file) and it prints the file location of the image. However, when I try to assign that value to a variable ( var exampleName = placedItem.file ) it also returns the same error.
I've also tried accessing sub properties of file, like file.fullName and file.displayName. It returns the same error. If anyone has insight that would be awesome. I really just need to return the path of an image to a variable.
I have ran the following with no problem such as you are experiencing.
function test(){
if(app.documents.length>0){
var doc=app.activeDocument;
var s = doc.placedItems[0];
var info = s.file;
alert(info);
}
}
test();
Copy link to clipboard
Copied
CHAPTER 1: JavaScript Object Reference PageItems 119
Getting references to external files in page items
Before running this script, open a document that contains one or more linked images.
// Gets all file-references in the current document using the pageItems object,
// then displays them in a new document
if ( app.documents.length > 0 ) {
var fileReferences = new Array();
var sourceDoc = app.activeDocument;
var sourceName =sourceDoc.name;
for ( i = 0; i < sourceDoc.pageItems.length; i++ ) {
artItem = sourceDoc.pageItems;
switch ( artItem.typename ) {
case "PlacedItem":
fileReferences.push( artItem.file.fsName );
break;
case "RasterItem":
if ( ! artItem.embedded ) {
fileReferences.push( artItem.file.fsName );
}
break;
}
}
// Write the file references to a new document
var reportDoc = documents.add();
var areaTextPath = reportDoc.pathItems.rectangle( reportDoc.height,0,
reportDoc.width, reportDoc.height );
var fileNameText = reportDoc.textFrames.areaText( areaTextPath );
fileNameText.textRange.size = 24;
var paragraphCount = 3;
var text = "File references in \'" + sourceName + "\':\r\r";
for ( i = 0; i < fileReferences.length; i++ ) {
text += ( fileReferences + "\r" );
paragraphCount++;
}
fileNameText.contents = text;
}
Copy link to clipboard
Copied
I have ran the following with no problem such as you are experiencing.
function test(){
if(app.documents.length>0){
var doc=app.activeDocument;
var s = doc.placedItems[0];
var info = s.file;
alert(info);
}
}
test();
Copy link to clipboard
Copied
Thank you for the example guys. I feel very very silly - turns out I was trying to read the 'file' property of a PathItem (which obviously doesn't exist). I should have started printing the typenames from the beginning to double check what I was passing to the function. I appreciated the quick feedback.
While we're hear though - I was curious if you're familiar with the property 'absoluteZOrderPosition'. I couldn't find it in the AI CC Javascript manual, but it is a property that appears on my pageItems.
Copy link to clipboard
Copied
Hey looks like some hidden undocumented gem, I cannot make sense of it right now. For example, I have an item which has ZOrderPosition of 13, but absoluteZOrderPosition is 30. Hmm.
Copy link to clipboard
Copied
no such property on CS5
Copy link to clipboard
Copied
I'm seeing it on CC '14.
As silly noted, what its value means isn't readily apparent. If I find a pattern I'll report back on it.
Copy link to clipboard
Copied
I have no idea if this has any merit, but I go with it anyway.
zOrderPosition is object stacking order.
Could absoluteZOrderPosition be stacking order of points?
Not found in CS5, CS5 has no gradient on strokes so no need for stacking order of points?
rickardart has just posted this: Is there a way to chose when a path flows above itself and when it flows behind?
that's what made me think.
Can't test any of this as I only have CS5 here and am not back at work for a few days
Copy link to clipboard
Copied
From what I can tell this is not the case... Booo.
But am yet to find a zOrderPosition that differs from absoluteZOrderPosition.
will keep looking as I open files.
I have always done my scripting in Notepad++.
after having a play with ExtendScript Toolkit over the weekend I like the ability to run the script without even saving etc.
may be a dumb question, but there are obviously other features I can use.
such as how you found
it is a property that appears on my pageItems.
am I missing something important that I should be aware of?
Maybe this should have been a new Question...
Copy link to clipboard
Copied
he probably meant it appeared in the Object Model Viewer, under the Help menu in the ESTK, you can find all kind of undocumented objects there.
Copy link to clipboard
Copied
Thanks Carlos.
I'll have a play there.
Copy link to clipboard
Copied
The mystery property does not appear to be documented in the CC OMV, but it does appear in the Data Browser.
function test(){
var doc = app.activeDocument;
var p = doc.pageItems[0];
alert(p.name+". Normal Z-Order: "+p.zOrderPosition+", Absolute Z-Order: "+p.absoluteZOrderPosition);
}
test();
The above code used on doc screen-shot below prints out: Grp_2. Normal Z-Order: 7, Absolute Z-Order: 13
My experiment here, suggests that absolute order takes into account the child elements of all elements (to show z-order in entire document), and the 'regular' z-order is about the stacking within a container element.
Like layer.pageItems vs document.pageItems ?
Though when I first looked at this property inside one of my more cluttered real-life documents, I remember that I couldn't verify this relationship. Maybe I was tired or something.
Copy link to clipboard
Copied
Actually I found AbsoluteZOrderPosition by writing a function that prints the names of every attribute of an object pasted to it. I don't think it's in the ESTK.
Copy link to clipboard
Copied
any chance of seeing that code snippet slowMotion?
and silly-v, that actually sounds quite logical.
now i wish there was a pointZOrderPosition.
Copy link to clipboard
Copied
function GetKeys(obj)
{
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}
var props = GetKeys(yourPlacedItemGoesHere);
alert(props);
Copy link to clipboard
Copied
Thanks,
I'l have a play with it.