Pages: 1
Print
Author Topic: Run batch file from macro  (Read 12149 times)
clloyd
Member

Posts: 6


« on: November 05, 2010, 10:34:26 AM »

Hi, 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 = "\"\\\"" +appAssetPath

var 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, OR
Logged
clloyd
Member

Posts: 6


« Reply #1 on: November 05, 2010, 04:22:04 PM »

I ended up writing line in cmd window which worked.

var runCmd = "cmd /k xcopy /R /Y /I /E %ALLUSERSPROFILE%\\XMetaL\\X etc.
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2621



WWW
« Reply #2 on: November 05, 2010, 05:52:27 PM »

I 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
pause

« Last Edit: November 05, 2010, 05:56:11 PM by Derek Read » Logged
clloyd
Member

Posts: 6


« Reply #3 on: November 08, 2010, 11:41:15 AM »

Thanks Derek, I'll give that a try.
Logged
Stoobie
Member

Posts: 13


« Reply #4 on: April 05, 2011, 05:41:05 AM »

This 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?

Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2621



WWW
« Reply #5 on: April 05, 2011, 11:16:16 AM »

Where 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.
« Last Edit: April 05, 2011, 11:35:55 AM by Derek Read » Logged
Stoobie
Member

Posts: 13


« Reply #6 on: April 06, 2011, 03:00:56 AM »

Quote
Where is your MCR file normally located?

C:\Program Files (x86)\XMetaL 6.0\Author\Macros\xmetal.mcr

Quote
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.
http://forums.xmetal.com/index.php/topic,1268.msg4082.html#msg4082

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.
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2621



WWW
« Reply #7 on: April 06, 2011, 03:35:46 PM »

Sorry, 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.html

Anticipating 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.
Logged
Pages: 1
Print
Jump to:  

email us