How to hook the command [ID_APPLY_VIEW_TEMPLATE] when executed from the view properties button

Hi everyone!

Hoping all of you are doing good today!, I am looking for a way to show a warning message when a user intend to change a view template settings.
Is it possible to hook the command ID_APPLY_VIEW_TEMPLATE when it is executed from the view properties button?.
-At the moment my hook code is working only when the command is executed from the view tab:
success view t hook

-But, when I execute the same from the view properties button, the hook code does nothing:
fail view t hook

This is the hook code I am using (thanks to @Jean-Marc):

import os
from pyrevit import EXEC_PARAMS
from pyrevit import forms

doc = __revit__.ActiveUIDocument.Document

ans = forms.alert("WARNING!\n\n"
                    "Do you really want to modify this view template?",
                    options=["Continue",
                            "Cancel"],
                    title="Apply View Template",
                    footer="HDS Hooks")
if ans  == "Continue":
    try:
        from hooksScripts import hooksLogger
        hooksLogger("Apply view template", doc)       
    except:
        pass
elif ans  == "Cancel":
    EXEC_PARAMS.event_args.Cancel = True
else:
    EXEC_PARAMS.event_args.Cancel = True

It would be that the Command ID is not the same when we use from the View tab and from the view properties?, Thanks in advance and greetings from Mexico City.

you may want to check your journal files right after editing the view template from the properties panel to check if there is a system postable command available to map as a hook

Hi @Jean-Marc thanks for your answer, I have checked the journal file but it seems there is no system postable command to do this mapping. I assume that it is not possible using hooks without it, is it right?, this is the fragment of the txt where my actions relative to view template appears.

 '
  '***********************************************************
  '***********************************************************
  '<<End build CT>> 
  ' 0:< ::72:: Delta VM: Avail -46 -> 134177135 MB, Used -24 -> 1343 MB, Peak +1 -> 1368 MB; RAM: Avail -174 -> 26552 MB, Used +8 -> 1394 MB, Peak +8 -> 1394 MB 
  ' 0:< GUI Resource Usage GDI: Avail 9116, Used 884, User: Used 434 
  'E 08-Aug-2023 08:52:56.811;   0:< 
  Jrn.MouseMove    0 ,     27 ,    342
  'E 08-Aug-2023 08:52:56.811;   0:< 
  Jrn.PropertiesPalette "Activate" 
  'E 08-Aug-2023 08:52:56.814;   0:< 
  Jrn.Grid "Control; FormView , Properties , IDD_PROPERTIES_PALETTE; IDC_SYMBOL_GRID"  _
          , "Selection"  ,  "" 
  'E 08-Aug-2023 08:52:56.815;   0:< 
  Jrn.Grid "Control; FormView , Properties , IDD_PROPERTIES_PALETTE; IDC_SYMBOL_GRID"  _
          , "MoveCurrentCell"  ,  "  View Template", "ValueCol" 
  'E 08-Aug-2023 08:52:56.932;   0:< 
  Jrn.Grid "Control; FormView , Properties , IDD_PROPERTIES_PALETTE; IDC_SYMBOL_GRID"  _
          , "Button"  ,  "  View Template", "ValueCol" 
  ' 0:< Unnecessary nesting;ElemGrid_2;-1;IDS_MODIFY_ELEM_ATTRIB ;N++EB(NB); 
  ' 0:< Transaction ElemGRid_3 with ParamDef 
  'C 08-Aug-2023 08:52:56.934;   0:< ADialog::doModal start 
    'E 08-Aug-2023 08:52:58.471;   0:< 
    Jrn.ListBox "Modal , Assign View Template , Dialog_Revit_ViewTemplates"  _
              , "Control_Revit_ViewsList"  _
              , "Select"  , "Architectural Plan" 
    'E 08-Aug-2023 08:52:59.684;   0:< 
    Jrn.PushButton "Modal , Assign View Template , Dialog_Revit_ViewTemplates"  _
              , "OK, IDOK" 
    ' 0:< Unnecessary nesting;ViewTemplatesDlg_1;-1;IDS_MODIFY_ELEM_ATTRIB ;N++EB(NB); 
    ' 0:< Unnecessary nesting;ViewTemplatesDlg_5;-1;ID_APPLY_VIEW_TEMPLATE ;N++EB(NB); 
    ' 4:< SLOG $3a5f2697 2023-08-08 08:52:59.716 >EOTC 
    ' 4:< SLOG $3a5f2697 2023-08-08 08:52:59.726 >EOTC:MEE 
    ' 7:< SLOG $3a5f2697 2023-08-08 08:52:59.816 .EOTC:MEE:LockUsers  RW locked 
    ' 7:< EP::readDeltas 80 -- no deltas 
    ' 8:< syncEPtoDisk stats: 0 misses / 1 hits =  0.0 ratio 
    ' 8:< start logDiffEP of only CENTRAL_BORROWS 
    ' 8:< stop logDiffEP of only CENTRAL_BORROWS 
    ' 8:< >ER::dumpCache 0 OLD versus 0 NEW 
    ' 8:< <ER::dumpCache 
    'C 08-Aug-2023 08:52:59.832;   6:< [ggranadosNFAMX] acquired element 1378266: ' 3D View, Level 2 Section Box ', in workset ' View "3D View: Level 2 Section Box" ' 
    ' 7:< SLOG $3a5f2697 2023-08-08 08:52:59.836 .EOTC:MEE:LockUsers  RW unlocked 
    '  0.110867       5:<<transactWithCentral 0- PostEditingRequest[1] 
    ' 4:< SLOG $3a5f2697 2023-08-08 08:52:59.847 <EOTC:MEE 
    '  0.141371      4:<<BorrowElements::borrowFromCentral 
    '  0.142201     3:<<BorrowElements::operator() 
    ' 2:< SLOG $3a5f2697 2023-08-08 08:52:59.858 <EOTC 
    '  0.153663    2:<<EndOrAbortUndoTransaction()--end of transaction checks;DOPT;/ALL_GAPS   0.011463/TurnOff_GAPs 10% 
    'H 08-Aug-2023 08:52:59.916;   1:< 
    Jrn.Data  _
            "Transaction Successful"  , "Apply Template Properties" 

Yep, that is what I thought too.
A different path would be to use iupdater / event listener initiated at startup.
Not really my cup of tea. But there are a few forum posts about it.

Got it, I will take a look on it!
Thanks so much Jean!

@Nicholas.Miles, sorry for including you here for this, Do you think using iupdater can be a solution for this issue, I have no experience using iupdater, this works saving the code in a folder like the hooks?
Sorry again and thanks in advance!

@Gil_Granados No I don’t think an iupdater would be able to elegantly solve this problem.

I did a quick test and an iupdater does not get notified when you apply view templates from the ribbon button, but that may be because of the parameter I was watching. An iupdater simply watches for changes to an element so it may be possible to make an iupdater work if you can find the relevant parameters on the view that will always change if you apply a template from the ribbon or properties panel. But an iupdater does not have the ability to “cancel” the change. You would have to cache the previous view template / parameters and manually change them back if you canceled the change from the dialog window.

When I tesed it, the iupdater got notified when you changed the view template from the properties panel, but you get notified after the template has been applied (after you click ok/apply from the view template menu).

Hi @Nicholas.Miles, thanks so much for your time, I will check some possibilities considering your answer!