| | 1 | | #pragma rtGlobals=3 |
| | 2 | | #pragma rtFunctionErrors=1 |
| | 3 | | #pragma version=1.10 |
| | 4 | | #pragma TextEncoding="UTF-8" |
| | 5 | | #pragma ModuleName=IUTF_AutoRun |
| | 6 | |
|
| | 7 | | /// Creates a notebook with the special name "HistoryCarbonCopy" |
| | 8 | | /// which will hold a copy of the history |
| 5 | 9 | | Function CreateHistoryLog() |
| | 10 | |
|
| 5 | 11 | | DoWindow/K HistoryCarbonCopy |
| 5 | 12 | | NewNotebook/V=0/F=0/N=HistoryCarbonCopy |
| 5 | 13 | | End |
| | 14 | |
|
| | 15 | | /// Return the type of autorun mode we are in |
| | 16 | | /// |
| | 17 | | /// @returns one of @ref AutorunModes |
| 0 | 18 | | Function GetAutorunMode() |
| 0 | 19 | | string path |
| | 20 | |
|
| 0 | 21 | | path = IUTF_Utils_Paths#AtHome("DO_AUTORUN.TXT") |
| 0 | 22 | | GetFileFolderInfo/Q/Z path |
| | 23 | |
|
| 0 | 24 | | if(!V_flag) |
| 0 | 25 | | return AUTORUN_FULL |
| 0 | 26 | | endif |
| | 27 | |
|
| 0 | 28 | | path = IUTF_Utils_Paths#AtHome("DO_AUTORUN_PLAIN.TXT") |
| 0 | 29 | | GetFileFolderInfo/Q/Z path |
| | 30 | |
|
| 0 | 31 | | if(!V_flag) |
| 0 | 32 | | return AUTORUN_PLAIN |
| 0 | 33 | | endif |
| | 34 | |
|
| 0 | 35 | | return AUTORUN_OFF |
| 0 | 36 | | End |
| | 37 | |
|
| | 38 | | /// Hook function which is executed after opening a file |
| | 39 | | /// |
| | 40 | | /// This function calls the user supplied run routine if |
| | 41 | | /// - the opened file is an igor experiment |
| | 42 | | /// - the file DO_AUTORUN.TXT exists in the igor home path |
| 0 | 43 | | static Function AfterFileOpenHook(refNum, file, pathName, type, creator, kind) |
| | 44 | | variable refNum, kind |
| | 45 | | string file, pathName, type, creator |
| | 46 | |
|
| 0 | 47 | | string funcList, cmd |
| 0 | 48 | | variable autorunMode, err |
| | 49 | |
|
| 0 | 50 | | string context = GetIndependentModuleName() |
| | 51 | |
|
| 0 | 52 | | // do nothing if the opened file was not an Igor packed/unpacked experiment |
| 0 | 53 | | if(kind != 1 && kind != 2) |
| 0 | 54 | | return 0 |
| 0 | 55 | | endif |
| | 56 | |
|
| 0 | 57 | | // Clear previous stored home path before checking the autorun mode. The previous stored home |
| 0 | 58 | | // path is unlikely the current home path of this experiment and it won't hurt to clear it at |
| 0 | 59 | | // load time of this experiment. |
| 0 | 60 | | IUTF_Utils_Paths#ClearHomePath() |
| | 61 | |
|
| 0 | 62 | | autorunMode = GetAutorunMode() |
| | 63 | |
|
| 0 | 64 | | if(autorunMode == AUTORUN_OFF) |
| 0 | 65 | | return 0 |
| 0 | 66 | | endif |
| | 67 | |
|
| 0 | 68 | | if(autorunMode == AUTORUN_FULL) |
| 0 | 69 | | CreateHistoryLog() |
| 0 | 70 | | endif |
| | 71 | |
|
| 0 | 72 | | if(CmpStr("ProcGlobal", context, 1)) |
| 0 | 73 | | Execute "SetIgorOption IndependentModuleDev=1" |
| 0 | 74 | | endif |
| | 75 | |
|
| 0 | 76 | | funcList = FunctionList("run", ";", "KIND:2,NPARAMS:0,WIN:[" + context + "]") |
| 0 | 77 | | if(ItemsInList(funcList) >= 1) |
| 0 | 78 | | FUNCREF AUTORUN_MODE_PROTO f = $StringFromList(0, funcList) |
| | 79 | |
|
| 0 | 80 | | if(IUTF_FuncRefIsAssigned(FuncRefInfo(f))) |
| 0 | 81 | | try |
| 0 | 82 | | err = GetRTError(1) |
| 0 | 83 | | f(); AbortOnRTE |
| 0 | 84 | | catch |
| 0 | 85 | | err = GetRTError(1) |
| 0 | 86 | | print "The run() function aborted with an RTE and this can not be handled." |
| 0 | 87 | | QuitOnAutoRunFull() |
| 0 | 88 | | endtry |
| 0 | 89 | | else |
| 0 | 90 | | print "The run() function has an invalid signature." |
| 0 | 91 | | endif |
| 0 | 92 | | else |
| 0 | 93 | | print "The requested autorun mode is not possible because the function run() does not exist in " + context + " conte |
| 0 | 94 | | endif |
| 0 | 95 | | End |
| | 96 | |
|
| 0 | 97 | | Function QuitOnAutoRunFull() |
| | 98 | |
|
| 0 | 99 | | string tmpStr |
| | 100 | |
|
| 0 | 101 | | if(GetAutorunMode() == AUTORUN_FULL) |
| 0 | 102 | | sprintf tmpStr, "%s#SaveHistoryLog(); Quit/N", GetIndependentModuleName() |
| 0 | 103 | | Execute/P tmpStr |
| 0 | 104 | | endif |
| 0 | 105 | | End |
| | 106 | |
|
| | 107 | | /// resets a global filename template string for output |
| 11 | 108 | | Function ClearBaseFilename() |
| 11 | 109 | | DFREF dfr = GetPackageFolder() |
| 11 | 110 | | string/G dfr:baseFilename = "" |
| 11 | 111 | | End |
| | 112 | |
|
| | 113 | | /// creates a new filename template, if template already present return current |
| 5 | 114 | | Function/S GetBaseFilename() |
| 5 | 115 | | DFREF dfr = GetPackageFolder() |
| 5 | 116 | | SVAR/Z/SDFR=dfr baseFilename |
| 5 | 117 | | SVAR/Z/SDFR=dfr baseFilenameOverwrite |
| | 118 | |
|
| 5 | 119 | | if(!SVAR_Exists(baseFilename)) |
| 0 | 120 | | string/G dfr:baseFilename = "" |
| 0 | 121 | | SVAR/SDFR=dfr baseFilename |
| 5 | 122 | | endif |
| | 123 | |
|
| 5 | 124 | | if(!IUTF_Utils#IsEmpty(baseFilename)) |
| 0 | 125 | | return baseFilename |
| 5 | 126 | | endif |
| | 127 | |
|
| 5 | 128 | | if(SVAR_Exists(baseFilenameOverwrite) && !IUTF_Utils#IsEmpty(baseFilenameOverwrite)) |
| 0 | 129 | | baseFilename = baseFilenameOverwrite |
| 5 | 130 | | else |
| 5 | 131 | | sprintf baseFilename, "%s_%s_%s", IgorInfo(1), Secs2Date(DateTime, -2), ReplaceString(":", Secs2Time(DateTime, 3), " |
| 5 | 132 | | endif |
| | 133 | |
|
| 5 | 134 | | return baseFilename |
| 5 | 135 | | End |
| | 136 | |
|
| | 137 | | /// Save the contents of the history notebook on disk |
| | 138 | | /// in the same folder as this experiment as timestamped file "run_*_*.log" |
| 0 | 139 | | Function SaveHistoryLog() |
| | 140 | |
|
| 0 | 141 | | string historyLog, msg |
| 0 | 142 | | historyLog = GetBaseFilename() + ".log" |
| | 143 | |
|
| 0 | 144 | | DoWindow HistoryCarbonCopy |
| 0 | 145 | | if(V_flag == 0) |
| 0 | 146 | | IUTF_Reporting#IUTF_PrintStatusMessage("No log notebook found, please call CreateHistoryLog() before.") |
| 0 | 147 | | return NaN |
| 0 | 148 | | endif |
| | 149 | |
|
| 0 | 150 | | historyLog = IUTF_Utils_Paths#AtHome(historyLog, unusedName = 1) |
| | 151 | |
|
| 0 | 152 | | SaveNoteBook/S=3 HistoryCarbonCopy as historyLog |
| 0 | 153 | | End |