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

FM2019 event-driven scripts do not run automatically while in Autorun

Participant ,
Dec 03, 2019 Dec 03, 2019

Copy link to clipboard

Copied

Hello fellows,

I see that in FM2019, exactly like in FM2015, event-driven scripts do not fire automatically in Autorun mode. To make them run, I still need to select a script in the Scripts window and click run. Is there anything that can be done about it, except for filing a bug and waiting for the best? 😉

 

Thanks,

Roman

TOPICS
Scripting

Views

1.4K

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 ,
Dec 03, 2019 Dec 03, 2019

Copy link to clipboard

Copied

Hi Roman, Can you be more specific? Thanks. -Rick

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
Participant ,
Dec 03, 2019 Dec 03, 2019

Copy link to clipboard

Copied

Hi Rick,

 

I appreciate your quick response!

I have a script that saves an active doc or book as FM13 while hitting Save or Ctrl+S. I moved it to Autorun. 

When I run FM19, open a book or book component, make changes and save them, the file is not saved as FM13, until I go to the Scripts pod, select the event script and hit Run. I experienced the same problem with event scripts in FM13. I don't see this was impoved in FM19.

 

Best Regards,

Roman

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
Community Expert ,
Dec 03, 2019 Dec 03, 2019

Copy link to clipboard

Copied

Is the script in your startup folder? Can you post the script? Thanks.

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
Participant ,
Dec 03, 2019 Dec 03, 2019

Copy link to clipboard

Copied

Hi Rick,

 

Sure, the file is in the startup folder. I clicked Move to Autorun. This part does work at least.

 

Here is the script itself.

#target framemaker

Notification (Constants.FA_Note_PostSaveDoc, Constants.FA_Note_PostSaveBook, true);

var oDoc = app.ActiveDoc;
var oBook = app.ActiveBook;

function Notify (note, object, sparam, iparam) {

    switch (note) {
        case Constants.FA_Note_PostSaveDoc:
        SaveToFM13(oDoc);
        break;
        case Constants.FA_Note_PostSaveBook:
        SaveToFM13(oBook);
        break;
    }
}

function SaveToFM13(file) 
{	
    if(file.ObjectValid()) 
    {
        var params = GetSaveDefaultParams();
        var returnParamsp = new PropVals();
        var i;
        i = GetPropIndex(params, Constants.FS_FileType);
        params[i].propVal.ival = Constants.FV_SaveFmtBinary130;
        i = GetPropIndex(params, Constants.FS_SaveAsModeName);
            params[i].propVal.ival = Constants.FV_SaveAsNameProvided;
        i = GetPropIndex (params, Constants.FS_SaveMode);
            params[i].propVal.ival = Constants.FV_ModeSaveAs;
        file.Save(file.Name, params, returnParamsp);
        return; 
        }
}

 

 

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
Community Expert ,
Dec 03, 2019 Dec 03, 2019

Copy link to clipboard

Copied

Hi Roman,

The outer part of the script only runs on startup when there is no active document or book. You don't need these lines at all:

var oDoc = app.ActiveDoc;
var oBook = app.ActiveBook;

The object parameter inside the Notify function will either be the document or book that is being saved, so you can just use this:

SaveToFM13(object);

in both places.

One more thing: I am not sure if this is correct syntax:

 

Notification (Constants.FA_Note_PostSaveDoc, Constants.FA_Note_PostSaveBook, true);

I would use this just in case:

Notification (Constants.FA_Note_PostSaveDoc, true);
Notification (Constants.FA_Note_PostSaveBook, true);

-Rick

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
Participant ,
Dec 04, 2019 Dec 04, 2019

Copy link to clipboard

Copied

Hi Rick,

 

Thank you for your input!

Not sure what you meant by "The outer part of the script only runs on startup".

I updated the script as per your input. When trying to save, I started getting error "-94". After unregistering the script and adding it again, it started working properly. Not sure why there is a need to unregister a script after making changes to it. Theoretically, restarting FM should have been enough. Just another bug in ES?

 

Thanks,

Roman

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
Participant ,
Dec 04, 2019 Dec 04, 2019

Copy link to clipboard

Copied

Actually after I restarted FM again, I keep getting error -94 when saving a doc...

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
Community Expert ,
Dec 04, 2019 Dec 04, 2019

Copy link to clipboard

Copied

Hi Roman, Please post the updated script. Thanks. -Rick

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
Participant ,
Dec 04, 2019 Dec 04, 2019

Copy link to clipboard

Copied

Hi Rick,

I just followed your instructions:

#target framemaker

Notification (Constants.FA_Note_PostSaveDoc, true);
Notification (Constants.FA_Note_PostSaveBook, true);

function Notify (note, object, sparam, iparam) {

    switch (note) {
        case Constants.FA_Note_PostSaveDoc :
        SaveToFM13(object);
        break;
        case Constants.FA_Note_PostSaveBook :
        SaveToFM13(object);
        break;
    }
}

function SaveToFM13(file) 
{	
    if(file.ObjectValid()) 
    {
        var params = GetSaveDefaultParams();
        var returnParamsp = new PropVals();
        var i;
        i = GetPropIndex(params, Constants.FS_FileType);
        params[i].propVal.ival = Constants.FV_SaveFmtBinary130;
        i = GetPropIndex(params, Constants.FS_SaveAsModeName);
            params[i].propVal.ival = Constants.FV_SaveAsNameProvided;
        i = GetPropIndex (params, Constants.FS_SaveMode);
            params[i].propVal.ival = Constants.FV_ModeSaveAs;
        file.Save(file.Name, params, returnParamsp);
        alert("Error: " + FA_errno);
        return; 
        }
}

  

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
Community Expert ,
Dec 04, 2019 Dec 04, 2019

Copy link to clipboard

Copied

You do have an alert in the script:

alert("Error: " + FA_errno);

Does the script work otherwise? Does it save down to FM 2015?

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
Participant ,
Dec 04, 2019 Dec 04, 2019

Copy link to clipboard

Copied

Hi Rick,

 

Yes, I've added the alert just for debugging. If the script runs OK, I am supposed to get Error: 0.

The script fails saving as FM13.

Not sure what the problem is.

 

Best regards,

Roman

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
Community Expert ,
Dec 04, 2019 Dec 04, 2019

Copy link to clipboard

Copied

Try removing these:

        i = GetPropIndex(params, Constants.FS_SaveAsModeName);
            params[i].propVal.ival = Constants.FV_SaveAsNameProvided;
        i = GetPropIndex (params, Constants.FS_SaveMode);
            params[i].propVal.ival = Constants.FV_ModeSaveAs;

In the scripts I have written like this, I don't use these.

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
Participant ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

Hi Rick,

 

Removing these params makes no difference. Actually, Klaus recommended adding them. I haven't seen them in use before. The ES documentation is awful. Not clear when these params should be set.

 

Regards,

Roman

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
Enthusiast ,
Dec 04, 2019 Dec 04, 2019

Copy link to clipboard

Copied

I did severals tests and in my opinion this is a bug.

When the function is called "manually", everything works.

I've tried using SimpleSave(), but that also didn't work:  FA_errno -44

I've tried SimpleSave with interactive = 1 and that also failed.

So using Notification always is a kind of "russian roulette".

I had several cases where notification failed by "forgetting" global variables.

 

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
Community Expert ,
Dec 04, 2019 Dec 04, 2019

Copy link to clipboard

Copied

Hi Klaus, I haven't tested Roman's code, but I checked some of my past scripts and in one I actually used PreSave events to do this. Roman, try modifying the code below and testing it. Thanks. -Rick

#target framemaker

Notification (Constants.FA_Note_PreSaveDoc, true);
Notification (Constants.FA_Note_PreSaveBook, true);

function Notify (note, object, sparam, iparam) {
 
    switch (note) {
        case Constants.FA_Note_PreSaveDoc :
        case Constants.FA_Note_PreSaveBook :
            if (app.VersionMajor > 12) { 
                // Cancel the built-in save.
                ReturnValue (Constants.FR_CancelOperation);
                saveAsFm12 (object, sparam);
            }
            break;
    }
}
 
function saveAsFm12 (doc, name) {
	
    var params, returnParams, i;
    
	// Get required parameters for the save function.
    params = GetSaveDefaultParams ();
    returnParams = new PropVals ();
    
    // Get the FileType save parameter and set it to FrameMaker 12.
    i = GetPropIndex (params, Constants.FS_FileType);
    params[i].propVal.ival = Constants.FV_SaveFmtBinary120;
    // Add the name to the recently opened list.
    i = GetPropIndex (params, Constants.FS_UpdateFRVList);
    params[i].propVal.ival = true;

    // Save the document as FrameMaker 12.
    doc.Save (name, params, returnParams);
    PrintSaveStatus (returnParams);
}

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
Participant ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

Rick,

Thank you for another version of the script! 

Could you please explain why you check the app version and why you save as FM12 and not as FM13?

In addition, it's not clear to me what the following does:

Constants.FS_UpdateFRVList

PrintSaveStatus (returnParams);

 

The script runs correctly for a while but then error -94 pops up again and I can no longer save the file in any format, including mif.

 

Best Regards,

Roman

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
Community Expert ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

LATEST

Hi Roman, I just copied an existing script that I had and didn't bother to update it. You should be able to change the version number and remove the version test.

 

When the script cancels the save, the file will not be added to the Recent Files list so that is what the Constants.FS_UpdateFRVList is supposed to do.

 

I think the PrintSaveStatus will print any errors to the FrameMaker Console.

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
Participant ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

I think I've spotted the problem.

 

For some reason, the script corrupts the file when it tries to save it as FM13, after several Save operations. When I mif-washed the file, the script worked correctly 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