SendKeys and a Sleep function to the rescue…
function sleep(milliseconds)
{
var currentTime = new Date().getTime();
while (currentTime + milliseconds >= new Date().getTime()) {
// do nothing
}
return milliseconds;
}
function printPDF(f)
{
var tmout = 5;
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var objShell = new ActiveXObject("Shell.Application");
var WshShell = new ActiveXObject("WScript.Shell");
var filename = f;
if(!objFSO.FileExists(filename))
{
return "File does not exist: " + filename;
}
try
{
var PDFCreatorQueue = new ActiveXObject("PDFCreator.JobQueue");
PDFCreatorQueue.Initialize();
//Since we are using the "DefaultGuid" pdf is our output format
var fullname = objFSO.GetFileName(filename);
var name = fullname.replace(objFSO.GetExtensionName(fullname),"pdf");
var fullPath = objFSO.GetParentFolderName(filename) + "/" + name;
// invoke associated program and call it's "print" verb
objShell.ShellExecute(filename,"", "", "print", 2);
// call our sleep function
var sleeping = sleep(500);
// send ENTER key
WshShell.SendKeys('{ENTER}');
if(!PDFCreatorQueue.WaitForJob(tmout))
{
PDFCreatorQueue.ReleaseCom();
return "The print job did not reach the queue within " + tmout + " seconds";
}
else
{
var job = PDFCreatorQueue.NextJob;
job.SetProfileByGuid("DefaultGuid");
//Assuming you want to convert to a .tif-file you can do this by
//simply changing the output format setting of your current profile
//job.SetProfileSetting("OutputFormat", "Tif");
job.SetProfileSetting("OutputFormat", "Pdf");
//Get the guid of the used profile
var guid = job.GetProfileSetting("Guid");
job.ConvertTo(fullPath);
if(!job.IsFinished || !job.IsSuccessful)
{
PDFCreatorQueue.ReleaseCom();
//WScript.Echo("Could not convert the file: " + fullPath);
return "Could not convert the file: " + fullPath;
}
else
{
PDFCreatorQueue.ReleaseCom();
//WScript.Echo("Job finished successfully");
return "Success";
}
}
PDFCreatorQueue.ReleaseCom();
}
catch(e)
{
PDFCreatorQueue.ReleaseCom();
return "ERROR: " + e.message;
}
}
Here is the Rule I use which is fired when I click a button…
This actually uses the Save Report Action, then uses JScript to convert. The reason I like the Save Report Action is that it allows us to specify the FileName, so we have full control over it. You could add one more Action to Send Email with attachment at the end of it all.