0

I'm successfully getting a reference to a non-threaded text frame that I want to delete, but calling .remove() on it results in error 45 'Object is invalid'

Seems like this code should work? But it doesn't :-(

var workbooklegal = workbook.pages.item(1).pageItems.item('govcapost-legal');
alert(workbooklegal) // [Object PageItem]
workbooklegal.remove();
Ted Fitzpatrick
  • 910
  • 1
  • 8
  • 18

2 Answers2

0

After some research, I discovered that the syntax .items('scriptlabel') is not supported for newer versions of InDesign. I iterated through the pageItems collection of the page, testing for .label == 'scriptlabel' and this worked to get a valid object for scripting commands.

Ted Fitzpatrick
  • 910
  • 1
  • 8
  • 18
0

The command

collection.item('someRandomName') will always return a (virtual) object wether a real object of the name exists or not. So you need to be careful when you want to work with such objects. You need to first check, if they actually exist. You can do this by testing for their isValid property.

var workbooklegal = workbook.pages.item(1).pageItems.item('govcapost-legal');
if(workbooklegal.isValid) {
  workbooklegal.remove();
}

Your issue has nothing to do with script labels, as collection.item('someRandomName') will look for the item in the collection with that name as assigned in the layer panel.

mdomino
  • 1,195
  • 1
  • 8
  • 22