Skip to main content
Whatevermajorloser
Inspiring
July 14, 2015
Question

How to move activeDocument's position in app.documents[]

  • July 14, 2015
  • 1 reply
  • 886 views

Hello,

Is there a way to move the current document's position in the app.documents array? I've got this function that will transfer part of an image's name (in this case an ID number) from one to the other. The way I've got it set up is that the user has to have at least one image open containing the ID number they want to extract and then they run the script on another image where they want the ID number to go in to. (If that makes sense?)

function connectOu() {

    if (app.documents.length <= 1){

    alert ("Oops! \nThere needs to be at least one other image open with the PID number you want this transferred to.")

    } else {

    var originalPid = activeDocument.name

    var originalPath = activeDocument.path

    var originalLength = activeDocument.name.length

    var originalType = originalPid.substring(originalLength - 6,originalLength - 4)

    var whatType = prompt ('What type of image should this be?', originalType)

        if (whatType === null) {

    } else {

    activeDocument.save()

    activeDocument.close()

    var fileRef = new File(originalPath + "/" + originalPid)

    var docRef = app.open (fileRef)

    activeDocument = app.documents[0]

    var newPid = activeDocument.name.substring(0,6)

    var newPath = activeDocument.path

    var middlePart = activeDocument.name.substring(6, activeDocument.name.length - 6)

    activeDocument = documents.getByName(originalPid)

    var saveFile = new File(newPath+ "/" + newPid + middlePart + whatType + ".tif");

    activeDocument.saveAs(saveFile, TiffSaveOptions,false,Extension.LOWERCASE);

    }

}

}

Right now the script closes the activeDocument and reopens it so that it is always the last one in the array due to the fact that the script targets the first image in the array for the extraction. Doing it this way means the script is a little slow and I'd love to speed it up by simply moving the activeDocument's position instead of opening and closing it.

I appreciate any help I can get!

Thanks.

This topic has been closed for replies.

1 reply

c.pfaffenbichler
Community Expert
Community Expert
July 15, 2015

Why close the document at all?

You can define a variable

var originalFile = activeDocument;

and use that to select the document again if necessary.

activeDocument = originalFile;
Whatevermajorloser
Inspiring
July 15, 2015

Hi c.pfaffenbichler,

Thanks for your response. Isn't the order of the documents in the array determined by the order in which they were actually opened? I don't think switching between them changes that, does it? So I think, If I went with your suggestion, I'd have a problem if the user had only opened two documents, the first one being the one they actually run the script on. In that case, the script would just select app.documents[0] and would just copy the ID Number from the image's filename that's the activeDocument and nothing would change..

Thanks

c.pfaffenbichler
Community Expert
Community Expert
July 15, 2015

Could you please post screenshots that clarify the actual task?

Why you employ Save As, do you want to keep two versions of the file/s?