Skip to main content
Known Participant
August 22, 2021
Answered

Script to save, close, reopen, and continue ("Error 8702: there is no document")

  • August 22, 2021
  • 1 reply
  • 1360 views

Hi all,

 

I'm trying to create a script which will allow me to save an Illustrator file, close it, reopen it, and continue working on it (in this case, add a layer). I've tried a number of different methods, but each one gives me an error ("8702: there is no document"). I'm guessing this is due to the document variable being reset once the document is reopened? I've tried a few different things, but can't seem to crack it:

 

Firstly I tried running it as a single function--

var doc = app.activeDocument
var docPath = app.activeDocument.fullName

app.executeMenuCommand("save");
app.executeMenuCommand("close");
app.open(new File(docPath));

var newLayer = doc.layers.add();
  newLayer.name = "New";

 

Then I tried breaking it into seperate fucntions, with the variable redefined--

saveCloseReopen();
addLayer();

function saveCloseReopen(){
  var doc = app.activeDocument
  var docPath = app.activeDocument.fullName

  app.executeMenuCommand("save");
  app.executeMenuCommand("close");
  app.open(new File(docPath));
}

function addLayer(){
  var doc = app.activeDocument
  var newLayer = doc.layers.add();
    newLayer.name = "New";
}

I tried using app.documents[0] instead of activeDocument to define the variable; I tried running app.activeDocument = doc after reopening, which I've had work in a similar Photoshop script. I've even tried writing each function as a seperate script, and calling them individually using $.evalFile -- I was really surprised this didn't work, but I'm still getting the error. 

 

Any advice massively appreciated, I'm ready to pull my hair out at this point!!

 

Thanks in advance.

This topic has been closed for replies.
Correct answer femkeblanco

I don't know why, but executeMenuCommand("close") messes up the document or documents collection (documents.length is 1 but there is no documents[0]).  Using the standard doc.close() appears to sidestep this.  See if this works for you. 

 

var doc = app.activeDocument;
var docPath = app.activeDocument.fullName;
app.executeMenuCommand("save");
// app.executeMenuCommand("close");
doc.close();
app.open(new File(docPath));
var doc = app.activeDocument;
var newLayer = doc.layers.add();
newLayer.name = "New";

 

1 reply

femkeblanco
femkeblancoCorrect answer
Legend
August 22, 2021

I don't know why, but executeMenuCommand("close") messes up the document or documents collection (documents.length is 1 but there is no documents[0]).  Using the standard doc.close() appears to sidestep this.  See if this works for you. 

 

var doc = app.activeDocument;
var docPath = app.activeDocument.fullName;
app.executeMenuCommand("save");
// app.executeMenuCommand("close");
doc.close();
app.open(new File(docPath));
var doc = app.activeDocument;
var newLayer = doc.layers.add();
newLayer.name = "New";

 

Known Participant
August 22, 2021

Amazing, thanks so much for this, works perfectly!