Calling a VBScript using arguments - vbscript

I am trying to get a VBScript to launch another VBScript while passing an argument from the first one to the second one. I got the part of how to receive the argument on the second script, however I have no clue how to call it within the first VBScript. Here is what I currently have in the first one:
arg1 = "MyArgument"
objShell.Run "ArgumentTest2.vbs arg1"
When I run the this script, it gives me the error message:
Script: C:\Argument Test 1.vbs
Line: 2
Char: 1
Error: Object required 'objShell'
Code: 800A01A8
Source: Microsoft VBScript runtime error
Any help would be greatly appreciated! Thanks!

You need to initialize objShell with
Set objShell = CreateObject("WScript.Shell")
before you can use its .Run method.
Also, VBScript doesn't expand variables inside strings, so you'll need to concatenate your argument to the rest of the command string:
objShell.Run "ArgumentTest2.vbs " & arg1
Note that you'll need to put tokens in double quotes if they contain spaces:
arg1 = "My Argument"
objShell.Run "ArgumentTest2.vbs """ & arg1 & """"

Related

WshShell.Run with spaces in command line params

From a vbscript I'm trying to get the user profile directory to expand and also run an executable with command line arguments. It is the spaces in the command line args that seem to be causing an issue. I'm close but no matter what I try I can't seem to get it.
This works:
WshShell.Run """%UserProfile%\test.exe"""
What I'm trying to do that does not work:
WshShell.Run """%UserProfile%\test.exe 8.8.8.8 8989 -e cmd.exe"""
I get an error of "The system cannot find the file specified."
As already mentioned in the comments the reason for the error in the second example is because the literal quotes are only needed to wrap a file path containing spaces, so you line should be;
WshShell.Run """%UserProfile%\test.exe"" 8.8.8.8 8989 -e cmd.exe"
It's the equivalent of writing
"%UserProfile%\test.exe" 8.8.8.8 8989 -e cmd.exe
at the command prompt in a console window (note, the position of the literal quotes).
Useful Links
Answer to: About using Double quotes in Vbscript
You can debug your variable before any execution with Wscript.Echo or MsgBox
Wscript.Echo chr(34) & CreateObject("wscript.shell").ExpandEnvironmentStrings("%UserProfile%\test.exe") & chr(34) & " 8.8.8.8 8989 -e cmd.exe"

How to pass several variables from Shell Script to AppleScript in Automator?

There is an excellent answer for the reverse, pass several variables from AppleScript to Shell Script but I can't find a comprehensive answer for the opposite when there are two or more variables/arguments and or a bash function.
In Automator I am trying to pass variables like so: Run AppleScript > Run Shell Script > Run AppleScript.
Run AppleScript: which passes a URL as an argument
Run Shell Script: which uses "$#" for that argument
/bin/bash serial=$(($RANDOM % 10000)) /usr/local/bin/ffmpeg -i "$#" -c copy bsf:a aac_adtstoasc "/Path/to/file/movie_$serial.mp4" 2>&1 $! exit 0
Run AppleScript: This is where I need to pick up stdout, and the PID of the last executed process ffmpeg from Run Shell Script above. I can't seem to get anything. I have tried adding an automator "Storage Variable" but it's not receiving.
Using AppleScript's Do Shell Script command I couldn't get serial=$(($RANDOM % 10000)) to actually put a serial number in the file name movie_$serial.mp4. The file name was literally output as "movie_$serial.mp4", instead of "movie_1234.mp4".
serial=$(($RANDOM % 10000)) works perfectly in Terminal and in Run Shell Script. Not sure what I am missing to make it work with "Do Shell Script".
do shell script "/bin/bash serial=$(($RANDOM % 10000)); /usr/local/bin/ffmpeg -i " & link_ & ffmpegOpt & "'" & sPath & "$serial.mp4" & "'"
Which returns the following for the "do shell script" call:
"/bin/bash serial=$(($RANDOM % 10000)); /usr/local/bin/ffmpeg -i urlofmovie -c copy -bsf:a aac_adtstoasc '/Path/to/file/movie_$serial.mp4'"
When using ffmpeg the path on the command line the save path has to be in quotes.
If I read your OP correctly, you actually have two different issue here.
Not knowing how to provide input to a Run AppleScript action from a Run Shell Script action.
Variable parameter expansion is not occurring for you with: $serial
Issue 1:
To return something from a Run Shell Script action to another action. e.g. a Run AppleScript action, set the last line of the Run Shell Script action to, e.g.:
echo "foobar"
Or:
printf "foobar"
For multiple items use, e.g.:
echo "foobar
barfoo"
Or:
printf "foobar\nbarfoo"
Issue 2:
I am not in the position to replicate your do shell script command at the moment; however, the reason variable parameter expansion is not occurring is because the variable has single-quotes around it.
... '/Path/to/file/movie_$serial.mp4'"
Expansion will not take place when a variable has single-quotes around it, so you need to formulate your command so it can be expanded. Or in a separate step, process what's necessary to to accomplish the goal.
For example:
set sPath to "/path/to/file/movie_"
set serial to ((random number from 0 to 32727) mod 10000) as string
set pathFilename to sPath & serial & ".mp4"
Then you can use, e.g.:
... & pathFilename's quoted form
In your do shell script command while adjusting the entire command to work for you.
In other words, you can get rid of, e.g.:
/bin/bash serial=$(($RANDOM % 10000));
And:
& "'" & sPath & "$serial.mp4" & "'"
When running a shell script from Script Editor and wanting to return more than one argument as input; and assign those arguments to variables in your Apple Script:
One method I discovered:
Example shell script:
SHELL_VAR1=$(date)
SHELL_VAR2=$(whoami)
echo "$SHELL_VAR1","$SHELL_VAR2"
The echo command at the end, with a comma for delimiter, will output to Apple Script in this format:
{"January 21, 2022", "john"}
In the Apple Script:
set input to (do shell script "script.sh")
set the text item delimiters to ","
set {var1, var2} to {text item 1, text item 2} of the input
{var1, var2}
If there is another, simpler, method I would love to learn it.
Is there a special notation for multiple arguments that Apple Script can use for input?
i.e. $1 $2 or something similar

How to deal with white spaces in osascript [duplicate]

This question already has answers here:
Combining variables in Bash to form a command sent to AppleScript using the osascript command
(2 answers)
Closed 5 years ago.
I'm in need of your assistance on how to deal with white space in a path.
In my example below, I'm setting some variables in a Terminal bash shell and executing, "osascript" from command line, utilizing these variables to run ['do shell script'] & ['display dialog']. When I run these from command line, I'm getting different results. My ultimate goal here is to be able to execute ['do shell script'] and it execute the bash shell script based on the path. In this case, the path has spaces in it.
NOTE: The INSTALLER_PATH variable defined below is set like this because the path is generated from an Apple Script I wrote that basically takes a path [ with white spaces in it ] and combines that path with another variable. Because this path has spaces in the name, I'm using the [ to quoted form of ] setting that puts the path in quotes.
APPLE SCRIPT EXAMPLE:
set pathToApp to POSIX path of ((path to me) as text)
set dragonFrame to ("_DRAGONFRAME/")
set INSTALLER_PATH to quoted form of pathToApp & dragonFrame
display dialog INSTALLER_PATH
GENERATES THIS PATH {notice the ticks}:
'/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/'_DRAGONFRAME/
Instead of testing this through the Apple Script to find the exact syntax I would need, I figured it would be easier to test this from command line. This is exactly what I'm setting and executing from my Terminal command line. In the examples below, executing [ 'display dialog' ] works and [ 'do shell script' ] fails. I think if there's a way to enclose the entire path in double quotation marks, my problem would be solved although all attempts at getting the path enclosed in double quotation marks has failed:
%> INSTALLER_PATH='/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/'_DRAGONFRAME/
%> ADMIN_USER_PROC="_Python_PySide_QT_Installer/Scripts/AdminUserProcesses.sh"
%> osascript -e 'do shell script ("'"${INSTALLER_PATH}"'" & "'"${ADMIN_USER_PROC}"'")'
GENERATES THIS ERROR:
0:217: execution error: sh: /Volumes/Free.Space/Shotgun: No such file or directory (127)
%> osascript -e 'display dialog ("'"${INSTALLER_PATH}"'" & "'"${ADMIN_USER_PROC}"'")'
GENERATES THIS PATH:
/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/_DRAGONFRAME/_Python_PySide_QT_Installer/Scripts/AdminUserProcesses.sh
Thank you in advance for any help you can offer.
I think you're looking for something similar to this syntax:
set dragonFrame to ("/_DRAGONFRAME/")
set INSTALLER_PATH to path of (pathToApp & dragonFrame)
Result:
'/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/_DRAGONFRAME/'
If you need double-quotes around the path you can use:
("\"" & pathToApp & dragonFrame & "\"")
and get rid of the quoted form argument leaving you with:
set INSTALLER_PATH to ("\"" & pathToApp & dragonFrame & "\"")

using SET in VBscript command

I'm trying to run an VBscript which runs a command line statement that contains the word set in the cmd line command.
The VBscript is recognizing it as a reserved word and giving the error:
Expected ')'
The command I'm trying to run in VBScript is:
objShell.Run(appcmd set config /section:isapiFilters /-[name='JakartaXAPI'])
How can I escape the reserved word?
Microsoft VBScript compilation error: Expected ')' is simply a syntax error.
Run method requires its first argument to be a string subtype and given space-delimited word sequence can't be reckoned for a string. You need to enclose it in quotation marks (" ").
Run Method (Windows Script Host)
Runs a program in a new process.
Syntax
object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
Arguments
object WshShell object.
strCommand String value indicating the command line you want to run. You must include any parameters you want to pass to the
executable file.
intWindowStyle Optional. Integer value indicating the appearance of the program's window. Note that not all programs make use of this
information.
bWaitOnReturn Optional. Boolean value indicating whether the script should wait for the program to finish executing before
continuing to the next statement in your script. If set to true,
script execution halts until the program finishes, and Run returns any
error code returned by the program. If set to false (the default), the
Run method returns immediately after starting the program,
automatically returning 0 (not to be interpreted as an error code).
Use
objShell.Run "appcmd set config /section:isapiFilters /-[name='JakartaXAPI']"
or
appcmd = "path\to\someapp.exe"
objShell.Run appcmd & " set config /section:isapiFilters /-[name='JakartaXAPI']"

How can I run a cscript as administrator with another vbscript?

I can use CreateObject("shell.application").ShellExecute but can I run a cscript as administrator? using that? I'm kind of a newbie vbscripter. Thanks!
Have you tried the RunAs command? I posted a solution recently that walks you through getting the proper syntax for executing the RunAs command from within VBScript.
Take a look and see if it helps.
The scriptPath is the path to your script you want to run as administrator
The Arguments is the commandline arguments you want to pass to the script. NOTE the Arguments are space separated, if you want to pass an argument that contains spaces you will need to enclose that arguments in quotes [Chr (34) is the quotes]. to do that you will need to write for example. Arguments= chr (34) & "Hello World" & chr(34)
ScriptPath="Path to your script"
Arguments="Any arguments to pass to the script"
CreateObject("Shell.Application").ShellExecute "cscript.exe",_
Chr(34) & ScriptPath & Chr(34) & _
" " & Arguments, _
"", "runas", 1

Resources