General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › Run batch file from macro
-
clloyd November 5, 2010 at 4:34 pm
Run batch file from macro
November 5, 2010 at 4:34 pmParticipants 6Replies 7Last Activity 12 years, 2 months agoHi, we are using XM 5.5 with SharePoint adapter. We have a batch file I stuck in Assets directory to clear the xm cache and I wanted to make it accessible from within the XM ui via a macro.
So using this code:// XMetal Author JScript Macro File
//Generated code. Do not edit. This is needed for IntelliSense to display properly for certain objects.
var Application = new ActiveXObject(“XMWrap.XMApplication”);
var Documents = new ActiveXObject(“XMWrap.XMDocuments”);
var ActiveDocument = new ActiveXObject(“XMWrap.XMActiveDocument”);
var Selection = new ActiveXObject(“XMWrap.XMSelection”);
var ResourceManager = new ActiveXObject(“XMWrap.XMResourceManager”);
//End generated code.var appAssetPath = Application.AssetsPath;
appAssetPath = appAssetPath.replace(/\/g,”\\”);
appAssetPath = “”\”” +appAssetPathvar runCmd = appAssetPath+”\\ClearXMetaLCache.bat\”””;
Application.Alert(runCmd);
var wsShell = null;
wsShell = new ActiveXObject(“WScript.Shell”);
wsShell.Run(runCmd);runCmd = null;
wsShell = null;————————————
Running this code, all that happens is Windows Explorer opens. If I run the same code in a simple .js file, it works (all the quoting is necessary due to space in path).
Any advice on this?Thanks,
Charles Lloyd
Epicor Software
Portland, ORclloyd November 5, 2010 at 10:22 pm
Reply to: Run batch file from macro
November 5, 2010 at 10:22 pmI ended up writing line in cmd window which worked.
var runCmd = “cmd /k xcopy /R /Y /I /E %ALLUSERSPROFILE%\XMetaL\X etc.
Derek Read November 5, 2010 at 11:52 pm
Reply to: Run batch file from macro
November 5, 2010 at 11:52 pmI think you may need to pass an 8.3 file path to WScript.Shell to do what you want. FSO exposes a property that lets you get the 8.3 path that is called “ShortPath”.
Here's an example that should work (I've removed all the space and JScript backslash munging for clarity and just hardcoded double backslashes as per JScript string requirements).
//JScript:
function shortPath(path) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var shortPath = fso.GetFile(path).ShortPath;
fso = null;
return (shortPath);
}
var runCmd = shortPath("C:\folder name with spaces\myfile.bat");
Application.Alert(runCmd);
var wsShell = new ActiveXObject("WScript.Shell");
wsShell.Run(runCmd);
wsShell = null;And here's the content of myfile.bat that I'm testing with:
rem this is a test
pauseclloyd November 8, 2010 at 5:41 pm
Reply to: Run batch file from macro
November 8, 2010 at 5:41 pmThanks Derek, I'll give that a try.
Stoobie April 5, 2011 at 11:41 am
Reply to: Run batch file from macro
April 5, 2011 at 11:41 amThis was helpful.
What syntax would I use if the batch file is in the same directory as the .mcr file calling it?
How would I write a relative path?
Derek Read April 5, 2011 at 5:16 pm
Reply to: Run batch file from macro
April 5, 2011 at 5:16 pmWhere is your MCR file normally located? My answer will vary depending on its actual location. The path I'm looking for is not necessarily the full path but a path relative to other files your customization depends on, specifically the DTD/XSD, or if you distribute your customization inside an XAC file. Or if it is an application-level MCR file then I would expect it to be located in the Startup folder.
We are using part of the Windows system to run the bat file (WScript.Shell) and it requires a full path in this case, so that path would be built based on this other information. There are various APIs that allow you to find the XMetaL installation location as well as some that allow you to easily convert between full and relative paths, locate the currently loaded “rules file” (DTD/XSD) etc and one or more of these will likely come into play.
Stoobie April 6, 2011 at 9:00 am
Reply to: Run batch file from macro
April 6, 2011 at 9:00 amWhere is your MCR file normally located?
C:Program Files (x86)XMetaL 6.0AuthorMacrosxmetal.mcr
The path I'm looking for is not necessarily the full path but a path relative to other files your customization depends on, specifically the DTD/XSD, or if you distribute your customization inside an XAC file. Or if it is an application-level MCR file then I would expect it to be located in the Startup folder.
I actually described what I want to do in another thread, as I didn't think it was relevant to this thread.
[url=http://forums.xmetal.com/index.php/topic,1268.msg4082.html#msg4082]http://forums.xmetal.com/index.php/topic,1268.msg4082.html#msg4082[/url]I don't actually do the customizations myself, though I'm trying to help with this little bit. We have XAC files, and we have files in the DITA OT directory, including specialized dtds.
Derek Read April 6, 2011 at 9:35 pm
Reply to: Run batch file from macro
April 6, 2011 at 9:35 pmSorry, wasted your time. I actually needed to know where the .bat file is (my question should have replaced MCR with BAT).
Various solutions:
1.
var batFileName = "myfile.bat";
var batFileSubfolder = "\SubFolder\";
var batFilePath = Application.Path + batFileSubFolder + batFileName;Then replace “myfile.bat” with your filename (obviously) and “\SubFolder\” with something like “\Macros\“.
That will work if you are putting the .bat file inside any subfolder in the XMetaL installation path, the most important bit being that Application.Path gives you the folder for the currently running instance of xmetal60.exe (or other version).
2.
If you are putting the .bat file in the same folder as xmetal.mcr then you may use the API Application.AllUsersMacroFile, however, that gives you the full path to that MCR file so you would need to strip off the filename and append your .bat file name. It would be easier to use the method above with the folder “\Macros\“.Note that there are various other APIs that give you paths for files included in a document-level customization, not useful in this case (as your customization uses xmetal.mcr it is application-level) so this is just an FYI (check the Programmers Guide for more info if you are curious, searching for “path” will turn up most of them).
3.
If you are putting the .bat file outside the XMetaL install folders and you always install it to the same location (and therefore know that it must be there) you might just hard code it:
var batFilePath = "C:\XMetaLCustomizationFilesAlwaysHereForEachUser\myfile.bat";4.
If it is outside the XMetaL install folders but in a Windows “special” folder (such as the users “Documents and Settings” folder for example) that location might change on a per-user basis and you may wish to read this so you can find the user-specific location: http://forums.xmetal.com/index.php/topic,27.0.htmlAnticipating Missing Files:
In all cases you may wish to wrap this code in a try...catch block, or use the API Application.FileExists() before attempting to run the .bat file so you can display a nicer error message if the file is not found for whatever reason. -
AuthorPosts
- You must be logged in to reply to this topic.