Pass parameters to an application - syntax

I'm attempting to use ghostscript to set a specific page size for scanned PDF files. I can run the script when it resides in the same folder as the executable but when I move the .vbs file out of the directory I can't get it to work. The .run line is where I'm having problems. The triple quotes for exe path without arguments opens the exe but i can't figure out how to pass the parameters.
I'm sure you can see that I'm pretty new to this.
strInput = InputBox ("Enter 1 for Landscape or 2 for portrait:")
Set objShell = WScript.CreateObject("WScript.Shell")
If strInput=1 then
Call LandScape
ElseIf strInput=2 Then
Call Portrait
Else
MsgBox "Your entry is invalid. Click OK to exit"
End If
Landscape sub-routine:
Sub LandScape
MsgBox "Your images are Landscape"
objShell.Run """c:\Program Files\gs\gs9.04\bin\gswin64c.exe""& "-dQUIET"&" -dNOPAUSE"&" -dBATCH"&" -dDEVICEWIDTHPOINTS=2592"&" -dDEVICEHEIGHTPOINTS=1728"&" -dFIXEDMEDIA"&" -sDEVICE=pdfwrite"&" -sOutputFile=OUTPUT.pdf"&" INPUT.pdf""
'This line works when in same directory: gswin64c -dQUIET -dNOPAUSE -dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf INPUT.pdf
End Sub

The 1st parameter to WScript.Shell.Run() is a full command line. So you need to
Add spaces between arguments;
Quote arguments that contain embedded spaces. In VB/VBScript, two consecutive double quotes inside a string literal are interpreted as a literal quote.
Can you now deduce the correct syntax for the objShell.Run line?

The quotes can get confusing so it sometimes helps to use Chr(34) instead and to keep the executable separate from its params.
strExe = "c:\Program Files\gs\gs9.04\bin\gswin64c.exe"
strParams = "-dQUIET -dNOPAUSE -dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf INPUT.pdf"
objShell.Run Chr(34) & strExe & Chr(34) & " " & strParams

Related

.VBS called by .BAT to create .zip - run silently (without interface)?

I have a .bat file which I use to back up files, which calls a .vbs and passes it two parameters, as follows:
...
ZipCMD.vbs "C:\Source" "C:\Destination\Data.zip"
...
ZipCMD.vbs contains the following code (Credit to garbb):
Set objArgs = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
InputFolder = FS.GetAbsolutePathName(objArgs(0))
ZipFile = FS.GetAbsolutePathName(objArgs(1))
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
numfolderitems = objShell.NameSpace(InputFolder).Items.count
objShell.NameSpace(ZipFile).CopyHere(source)
' wait until number of items in zip file is the same as in the folder we are zipping up
' also sometimes gets errors when getting folder object for zip file, probably because it is in use? so ignore these
On Error Resume Next
Do while True
numitemsinzip = objShell.NameSpace(ZipFile).Items.count
If Err.Number = 0 and numitemsinzip = numfolderitems Then
Exit Do
ElseIf Err.Number <> 0 then
Err.Clear
End If
wScript.Sleep 10
Loop
On Error Goto 0
When the zipping is occurring, the usual windows 'Compressing files' interface appears, and shows the progress bar ticking along for a few minutes, before closing and disappearing.
Question: Can vbs run a compression silently (i.e. without interface)? -- I've read this article, which shows a flag, however this doesn't appear to work with copying to .zip, for some reason.
Follow-up question: If it's not possible for the .vbs which I'm using to achieve this, then is there an alternative way, which still utilises calling another file/process(?) (.vbs / .js, or other?) and feeding it the two paths from cmd?
Edit: I'm trying to achieve this without the use of third-party software (e.g. 7zip), and simply using native windows code.
Suppose I am almost 3 months late on this, but if you have powershell version 5 or later, you can simply create a powershell script:
Compress-Archive "C:\Source" "C:\Destination\Data.zip"
or from a batch file:
powershell Compress-Archive "C:\Source" "C:\Destination\Data.zip"
Also see this option

Batch convert Powerpoint single-slide presntations to JPEG

In search of Automator or Applescript to batch convert Powerpoint Presentations with a single slide to an image file (preferably JPEG).
I am using Powerpoint 2011 for Mac and I am running OS X Yosemite. A
You could use unoconv which is part of LibreOffice to convert to PDF and then ImageMagick to convert from PDF to JPG.
Both these tools can be installed on OSX using homebrew, i.e.
brew install imagemagick
The command would then be
unoconv someFile.ppt someFile.pdf
convert -density 144 someFile.pdf someFile.jpg
I wouldn't use AppleScript to batch this, I would use find from the bash shell in Terminal like this
find . -iname "*.ppt" -exec unoconv "{}" "{}.pdf" \;
After weeks trying to, I find a way to batch convert an entire folder of pptx files into jpg (one slide each) with the same name as original each file. ITs windows using VBA. He goes, don't mind the non-code part I just changed the code and mix many that I find out there.
Sub BatchSave()
' Opens each PPT in the target folder and saves as PPT97-2003 format
Dim sFolder As String
Dim sPresentationName As String
Dim oPresentation As Presentation
Dim osld As Slide
' Get the foldername:
sFolder = InputBox("Folder containing PPT files to process", "Folder")
If sFolder = "" Then
Exit Sub
End If
' Make sure the folder name has a trailing backslash
If Right$(sFolder, 1) <> "\" Then
sFolder = sFolder & "\"
End If
' Are there PPT files there?
If Len(Dir$(sFolder & "*.PPTX")) = 0 Then
MsgBox "Bad folder name or no PPT files in folder."
Exit Sub
End If
' Open and save the presentations
sPresentationName = Dir$("*.PPTX")
While sPresentationName <> ""
Set osld = ActiveWindow.View.Slide
osld.Export sPresentationName & ".jpg", "jpg"
' New presentation is now saved as N_originalname.ppt
' Now let's rename them - comment out the next couple lines
' if you don't want to do this
' Original.PPT to Original.PPT.OLD
'Name sFolder & sPresentationName As sFolder & sPresentationName & ".OLD"
' N_Original.PPT to Original.PPT
'Name sFolder & "N_" & sPresentationName As sFolder & sPresentationName
sPresentationName = Dir$()
Wend
MsgBox "DONE"
End Sub

VBS WScript.Run fails after passing Exists test

In a couple of place in my code I check if the file exists (it does) then I try to Run the file as above, or get the DateLastModified, and get errors about file not found or invalid path. How can the script NOT see a file after confirming it exists?
I'm working up a .vbs script that tries to run an Access .mdb file. The WScript.Run command seems to choke on the filename, but putting a MsgBox() before that call to display the path allows Run to work properly. I don't want to display a popup.
Error is:
The directory name is invalid.
How is this possible and how can I get around it?
Here is code:
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
SET ws = WScript.CreateObject("WScript.Shell")
path = Chr(34) & LocalPath & AccessFileName & Chr(34)
if (fso.FileExists(LocalPath & AccessFileName)) THEN
'MsgBox(path) 'Uncommenting this line removes the error
ws.Run path 'This line errors
End If
Try to open your file with shell .InvokeVerb method:
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
If CreateObject("Scripting.FileSystemObject").FileExists(LocalPath & AccessFileName) Then
CreateObject("Shell.Application").Namespace(LocalPath).ParseName(AccessFileName).InvokeVerb
End If
UPD: Both ActiveX WScript.Shell and Shell.Application uses native windows shell to perform a file execution.The first one launches new process via WSH core located in wscript.exe, cscript.exe, wshom.ocx, jscript.dll, vbscript.dll, ets, .Run and .Exec methods of WsShell object provides wide control on the launched process, and second one located in Shell32.dll, uses .InvokeVerb method of IShellDispatch object, called without name, runs default verb equals to the windows explorer "open" command.In case of any issues connected to WSH, explorer might still works without any proplems. If it does, that is just a work-around, I can't say what's wrong definetely without close look.
Hello the following code worked for me.
Basically this code gets a folder object and loops through all files in a folder and checks if its the one that you named. This it runs the application.
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = Wscript.CreateObject("Wscript.Shell")
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
Set myFolder = fso.GetFolder(LocalPath)
For each myFile in myFolder.Files
If myFile.Name = AccessFileName Then
'Wscript.Echo myFile.Name
ws.Run myFolder.Path & "\" & myFile.Name
End If
Next
You can give this a shot. You probably do not need the quotes around the path, but I included it as a comment if you want to give it a shot. You just put quotes twice if you need to include a quote character in a string:
Set fso = CreateObject("Scripting.FileSystemObject")
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
Set ws = WScript.CreateObject("WScript.Shell")
' path = """" & LocalPath & AccessFileName & """" <-- probably unnecessary
path = LocalPath & AccessFileName
If (fso.FileExists(path)) Then
Set file = fso.GetFile(path)
'MsgBox(path) 'Uncommenting this line removes the error
ws.Run file.Path 'This line errors
End If
This does not make any sense. Having a MsgBox line is altering the behavior of the program!!!
I feel it is probably some weird invisible character somewhere which is getting activated when you comment the line.
Try retyping the If block without the MsgBox in between.

How can I convert .eps file to .pdf in Mathematica?

How can I convert .eps to .pdf inside Mathematica (perhaps using GhostScript?)?
After installing GhostScript and setting appropriate environment variables (for Windows you should add gs\bin and gs\lib to the PATH, where gs is the top-level Ghostscript directory) you can use Jens Nöckel's method for converting .eps to .pdf (all the glyphs will be outlined):
gsEPS2PDF[epsPath_String, pdfPath_String] :=
Run["gswin64c.exe -sDEVICE=pdfwrite -dNOCACHE -sOutputFile=\"" <>
pdfPath <> "\" -q -dbatch -dNOPAUSE \"" <> epsPath <> "\" -c quit"]
Here gswin64c.exe is the name of GhostScript executable for 64bit Windows systems, for Linux replace it with gs.
Another method based on Kurt Pfeifle' code (without font outlining):
gsEPS2PDFEmbedFonts[epsPath_String, pdfOutputPath_String] :=
Run["gswin64c.exe -sFONTPATH=c:/windows/fonts -o \"" <>
pdfOutputPath <>
"\" -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress \"" <> epsPath <>
"\""]
Here c:/windows/fonts is the directory where fonts are located. See also here for information about GhostScript command line parameters.
gr = Import["file.eps", "eps"]
Export["file.pdf", gr, "pdf"]

Run a vbscript from another vbscript

How do I get a vbscript to run another vbscript?
Id imagine its only a few lines of code but not tried doing this before, nothing is passed between the 2, one just needs to call/run the other.
For examples the script being run is called TestScript.vbs, the other script for it to call/run would be called Secondscript.vbs, both of which are located in C:\Temp.
Thanks
Mark
See if the following works
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "TestScript.vbs"
' Using Set is mandatory
Set objShell = Nothing
You can try using the Wshshell.Run method which gives you little control of the process you start with it. Or you could use the WshShell.Exec method which will give you control to terminate it, get a response, pass more parameters (other than commandline args), get status, and others
To use Run (Simple Method)
Dim ProgramPath, WshShell, ProgramArgs, WaitOnReturn,intWindowStyle
Set WshShell=CreateObject ("WScript.Shell")
ProgramPath="c:\test run script.vbs"
ProgramArgs="/hello /world"
intWindowStyle=1
WaitOnReturn=True
WshShell.Run Chr (34) & ProgramPath & Chr (34) & Space (1) & ProgramArgs,intWindowStyle, WaitOnReturn
ProgramPath is the full path to your script you want to run
ProgramArgs is the arguments you want to pass to the script. (NOTE: the arguments are separated by a space, if you want to use an argument that contains a space then you will have to enclose that argument in quotes [Safe way to do this is use CHR (34) Example ProgramArgs= chr (34) & "/Hello World" & chr (34)])
IntWindowStyle is the integer that determines how the window will be displayed. More info on this and WaitOnReturn can be found here WshShell.Run Method
WaitOnReturn if true then the script will pause until the command has terminated, if false then the script will continue right after starting command.
NOTE: The Run method can return the exit code but you must set WaitOnReturn to True, and assign the 'WshShell.Run' to a variable. (EX: ExitCode=WshShell.Run (Command,intWindowStyle,True))
To Use EXEC (Advanced Method)
Dim ProgramPath, WshShell, ProgramArgs, Process, ScriptEngine
Set WshShell=CreateObject ("WScript.Shell")
ProgramPath="c:\test run script.vbs"
ProgramArgs="/hello /world"
ScriptEngine="CScript.exe"
Set Process=WshShell.Exec (ScriptEngine & space (1) & Chr(34) & ProgramPath & Chr (34) & Space (1) & ProgramArgs)
Do While Process.Status=0
'Currently Waiting on the program to finish execution.
WScript.Sleep 300
Loop
ProgramPath same as Run READ RUN'S DESCRIPTION
ProgramArgs DITTO
ScriptEngine The Engine you will be using for executing the script. since the exec method requires a win32 application, you need to specify this. Usually either "WScript.exe" or "CScript.exe". Note that in order to use stdin and stdout (we'll cover what these are a bit further down) you must choose "CScript.exe".
Process this is the Object that references to the program the script will start. It has several members and they are: ExitCode, ProcessID, Status, StdErr, StdIn, StdOut, Terminate.
More Details about the members of Process Object
ExitCode This is the exit code that is returned when the process terminates.
ProcessID This is the ID that is assigned to the process, every process has an unique processID.
Status This is a code number that indicates the status of the process, it get set to '-1' when the process terminates.
StdErr This is the object that represents the Standard Error Stream
StdIn This is the Object that represents the Standard Input Stream, use it to write additional parameters or anything you want to pass to the script you are calling. (Process.StdIn.WriteLine "Hello Other Worlds")
StdOut This is the Object that represents the Standard Output Stream, It is READONLY so you can use Process.StdOut.ReadLine. This is the stream that the called script will receive any information sent by the calling script's stdin. If you used the stdin's example then StdOut.Readline will return "Hello Other Worlds". If there is nothing to read then the script will hang while waiting for an output. meaning the script will appear to be Not Responding Note: you can use Read or ReadAll instead of ReadLine if you want. Use Read (X) if you want to read X amount of characters. Or ReadAll if you want the rest of the stream.
Terminate Call this method to force terminate the process.
For more information about WshShell.Exec go to Exec Method Windows Scripting Host
Just to complete, you could send 3 arguments like this:
objShell.Run "TestScript.vbs 42 ""an arg containing spaces"" foo"
You can also load the body of the script and execute it within the same process:
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile("script2.vbs")
body = ts.ReadAll
ts.Close
Execute body
In case you don't want to get mad with spaces in arguments and want to use variables try this:
objshell.run "cscript ""99 Writelog.vbs"" /r:" & r & " /f:""" & wscript.scriptname & """ /c:""" & c & ""
where
r=123
c="Whatever comment you like"
I saw the below code working.
Simple, but I guess not documented.
Anyone else used the 'Execute' command ?
Dim body, my_script_file
Set Fso = CreateObject("Scripting.FileSystemObject")
Set my_script_file = fso.OpenTextFile(FILE)
body = my_script_file.ReadAll
my_script_file.Close
Execute body
Try this.
Option Explicit
On error resume next
Dim Shellobj
Set Shellobj = CreateObject("WScript.Shell")
Shellobj.Run "Test.vbs"
Set Shellobj = Nothing
As Martin's Answer didn't work at all for me ("File not found") and atesio's Answer does not allow to call two scripts which include repeating variable definitions, here is another alternative which finally worked for me:
filepath = Chr(34) & "C:\...\helloworld.vbs" & Chr(34)
Set objshell= CreateObject("WScript.Shell")
objshell.Run "wscript " & filepath, , True
Set objshell= Nothing
(Windows 8.1)

Resources