Skip to main content
Participant
June 11, 2015
Question

Is it possible to run a script automatically when a document is saved?

  • June 11, 2015
  • 2 replies
  • 487 views

What I am after is a script (for mac) that will enter the user name in the Author field of the File Information.

If the script could dynamically pick up the user name, that would be useful, but not essential. The main thing is it would need to run on save as I cannot rely on people to remember to enter the details themselves.

Any help hugely appreciated

Carl

This topic has been closed for replies.

2 replies

Participant
June 11, 2015

Thanks,

That seems to kind of work... But now my ⌘S command is not working and when I manually save the file (using the File Menu) the Title bar of the doc doesn't recognise the save and still gives the "Save Changes" dialogue box when you close.

Also, just realised it really messes around when adding files to a Book!

Any ideas? I don't really know about coding so I really appreciate the help.

Carl

Jump_Over
Legend
June 16, 2015

Hi,

1. No idea what you mean by "⌘S command is not working".

2. If your goal is to edit file (modify file info) after save ==> file has some changes (unsaved) right?

3. Maybe changing eventListener type to "beforeSave" could help?

Jarek

Participant
June 16, 2015

NAILED IT!

Thanks Jarek!

Carl

Jump_Over
Legend
June 11, 2015

Hi,

If you place a script into user "Startup Scripts" folder

and

if your script will define an eventListener (type "afterSave")

==> it could be done.

Example:

#target indesign

#targetengine "session"

var

  myEventListener = app.eventListeners.item("mAfSave");

  if (!myEventListener.isValid) {

  myEventListener = app.addEventListener( "afterSave", doJob );

  myEventListener.name = "mAfSave";

  }

function doJob() {

  if (!app.documents.length) return;

  var myDoc = app.activeDocument;

  myDoc.metadataPreferences.author = app.userName; // Win or Mac app user

//~ myDoc.metadataPreferences.author = $.getenv("USERNAME"); // Windows logged user

//~ myDoc.metadataPreferences.author = $.getenv("USER"); // MAC logged user

  }

Jarek