Reputation: 935
I wish to call an executable file from Julia via Base.run
(documented here) and pass command line options to that executable, but I can't figure out how to do that. In my specific example the executable is Notepad++ and the command line options are
-alwaysOnTop -nosession
This example code works, but doesn't pass the command line options:
function open_file_in_notepadpp()
exepath = "C:/Program Files (x86)/notepad++/notepad++.exe" #Default location on 64 bit Windows
command_line_options = "-alwaysOnTop -nosession "
filetoopen = "c:/temp/foo.txt"
Base.run(`$exepath $filetoopen`, wait = false)
end
I've tried incorporating command_line_options
a fair number of ways using backticks, double quotes etc. to no avail, so for example neither of the lines below work:
Base.run(`$exepath $filetoopen`, `$command_line_options`,wait = false)
Base.run(`$exepath $command_line_options $filetoopen`,wait = false)
In the Windows Command Prompt the following works correctly:
"C:/Program Files (x86)/notepad++/notepad++.exe" -alwaysOnTop -nosession "c:/temp/foo.txt"
Could someone explain what I'm missing?
Upvotes: 1
Views: 336
Reputation: 935
crstnbr's answer was correct, but he was unable to test on his machine. Here is the corrected code:
function open_file_in_notepadpp()
exepath = "C:/Program Files (x86)/notepad++/notepad++.exe" #Location if one follows the defaults in the notepad++ installer on 64 bit Wndows
command_line_options = ["-alwaysOnTop", "-nosession"] #Use an array to prevent the options being quoted
filetoopen = "c:/temp/foo.txt"
Base.run(`$exepath $filetoopen $command_line_options`,wait = false)
end
Upvotes: 0
Reputation: 10147
If you substitute a string that contains spaces to a command it will get quoted. Hence, your command line arguments will be quoted and you get
julia> `$exepath $filetoopen $command_line_options`
`'C:/Program Files (x86)/notepad++/notepad++.exe' c:/temp/foo.txt '-alwaysOnTop -nosession '`
I guess what you really need is
julia> command_line_options = ["-alwaysOnTop", "-nosession"]
2-element Array{String,1}:
"-alwaysOnTop"
"-nosession"
julia> `$exepath $filetoopen $command_line_options`
`'C:/Program Files (x86)/notepad++/notepad++.exe' c:/temp/foo.txt -alwaysOnTop -nosession`
Running the latter with run
should work. Unfortunately I can't test it on my machine.
Upvotes: 2