• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

RE: How to get script to activate?

Guest
Apr 16, 2012 Apr 16, 2012

Copy link to clipboard

Copied

I found a script that saves copies of InDesign pages to the desktop. It works great for my needs.

I would love to have this script activate whenever someone hits the Save shortcut (Command S), which currently saves our InDesign pages to our content management system (WoodWing).

Since I can't have a keyboard shortcut perform two commands at once, I am looking for a scripting solution.

Can anyone point me in the right direction?

• • • • • • •

//Save to Backup Folder.jsx

//

// This script is designed to save copies of the current document to a backup-location

//

var desktop_path = Folder.desktop.toString();

var backup_path = desktop_path+"/backup"

if (Folder(backup_path.exists == false )) {

          Folder(backup_path).create();

}

if (app.documents.length > 0) {

          main();

}

function main() {

          var doc = app.activeDocument;

          try {

                    if (doc.saved == true) {

                              var doc_file = doc.fullName;

                              var doc_name = doc.name;

                              var now = new Date();

                              var datestamp = now.getFullYear().toString() + "-" + two_digit(now.getMonth()+1) + "-" + two_digit(now.getDate());

                              var timestamp =  two_digit(now.getHours()) + two_digit(now.getMinutes()) + two_digit(now.getSeconds());

                              var target_folder = Folder(backup_path + "/" + datestamp);

                              if (target_folder.exists == false ) {target_folder.create(); }

                              var target_file = target_folder.toString() + "/" + timestamp + " — " + doc_name;

                              if (doc_file.copy(target_file) == false) {

                                        alert("Backup error\rCould not create backup copy.");

                              }

                    }

          } catch (e) {

                    alert(e);

          }

          doc.save();

 

          function two_digit(n) {

                    if (n < 10) {

                              return "0" + n.toString();

                    } else {

                              return n.toString();

                    }

          }

}

TOPICS
Scripting

Views

924

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 16, 2012 Apr 16, 2012

Copy link to clipboard

Copied

To make this same script ALSO do a regular Save, add this Magic Lyne at the top

app.activeDocument.save();

just above the line that says "main()". Then use the Keyboard Shortcut Editor to assign Ctrl+S to this script.

I wonder what will happen when you use it, though, on a *new* document. I guess you have to try it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 17, 2012 Apr 17, 2012

Copy link to clipboard

Copied

Create a startup script and add an event handler like so:

app.menuActions.itemByID(260).addEventListener("beforeInvoke",saveBackup);

Where "saveBackup" is the name of your function. You need to use a persistent engine for this to work correctly.

(260 is the id of the "Save" menuAction. If you mapped the shortcut to another menuAction, you'll have to change that to the correct one...)

Harbs

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 18, 2012 Apr 18, 2012

Copy link to clipboard

Copied

Thanks [Jongware] and Harbs for the assistance.

Harbs - I need to learn up on what a persistent engine is. Any suggests?

Thanks again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 18, 2012 Apr 18, 2012

Copy link to clipboard

Copied

LATEST

Just add the following line at the start of your script:

#targetengine "anyNameYouWant"

That will tell InDesign to run your script in the specified scripting engine and the global variables will persist after the script stops running.

Harbs

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines