I have some simple Excel VBA code that opens non-Excel files like:
Sub scriptTest()
Set objshell = CreateObject("Wscript.Shell")
objshell.Run ("C:\TestFolder\Book1.pdf")
Set objshell = Nothing
End Sub
Running this opens the file in the Acrobat Reader. However if I try to open a file whose name contains a space character like:
Sub scriptTest()
Set objshell = CreateObject("Wscript.Shell")
objshell.Run ("C:\TestFolder\Bo ok1.pdf")
Set objshell = Nothing
End Sub
I get:
Both files open fine if I use the Run command from the Windows Start menu. How can I overcome this problem ??
When executing the statement objshell.Run ("C:\TestFolder\Bo ok1.pdf"), you are asking the shell to execute the command
C:\TestFolder\Bo ok1.pdf
This is interpreted as being a request to execute the program C:\TestFolder\Bo.exe with a parameter of ok1.pdf.
You actually want the shell to execute the command
"C:\TestFolder\Bo ok1.pdf"
where the quotation marks are used by the command interpreter to "group" parts of the command together.
To obtain that command, you need to execute the statement
objshell.Run """C:\TestFolder\Bo ok1.pdf"""
Related
I've been using the Open method when dealing with files. I just found out about OpenTextFile and CreateTextFile. What is the difference between them and the Open method? Is one faster then the other? Or which is better?
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt", 1, TristateFalse)
f.Close
Dim line as String
Open "c:\testfile.txt" For Input as #1
Line Input #1, line
Close #1
Overall, Open is faster. However, it can only read files up to ~2gb and cannot read Linux EOL indicators. OpenTextFile on the other hand, creates a textstream that can read bigger files and read Linux EOL indicators, but, is approximatly 4 times slower than Open.
For years I've been using the following AppleScript to open txt files in vim in iTerm2, but since iTerm 2.9.2 (aka iTerm3) it's broken. Could anyone advise how to update this AppleScript so it works again?
on run {input, parameters}
if (count of input) > 0 then
tell application "System Events"
set runs to false
try
set p to application process "iTerm"
set runs to true
end try
end tell
tell application "iTerm"
activate
if (count of terminals) = 0 then
set t to (make new terminal)
else
set t to current terminal
end if
tell t
tell (make new session at the end of sessions)
exec command ("vim \"" & POSIX path of first item of input as text) & "\""
end tell
if not runs then
terminate first session
end if
end tell
end tell
end if
end run
I originally copied the script from http://earthwithsun.com/questions/283418/how-can-i-make-terminal-vim-my-default-editor-application-in-mac-os-x but I have no AppleScript experience whatsoever.
Any help most appreciated!
B
Using #dusty's link, I recommend that you change the entire code section of your Run AppleScript Action to this:
on run {input, parameters}
tell application "iTerm"
activate
if (count of windows) = 0 then
set t to (create window with default profile)
else
set t to current window
end if
tell t
tell current session
write text ("vim \"" & POSIX path of first item of input as text) & "\""
end tell
end tell
end tell
end run
It seems to work on my machine, but I never use vim, so I am not sure if this will give you precisely what you want.
Good luck,
The new Applescript syntax doesn't have terminal as a top-level object like before. It has been changed to reflect more of the common pattern used throughout the OS for other scriptable applications: the top-level objects of the application are called windows, not terminals.
So the hierarchy is now something like this:
the application containing one or more
windows containing one or more
tabs containing a
session
https://iterm2.com/applescript.html has been updated with examples of the new syntax.
I adopted the code snippet of Craig Smith (great answer!) and tweaked it a little to open a new tab if there is already an open window. This way, you don't get any trouble if you have already something like vim opened in your iTerm. Also, this code changes the directory to the directory of the file to make NerdTREE and fuzzy finders like fzf.vim happy.
on run {input, parameters}
set filename to POSIX path of input
set cmd to "clear; pushd " & quote & "$(dirname " & filename & ")" & quote & " > /dev/null; nvim " & quote & "$(basename " & filename & ")" & quote & "; popd > /dev/null"
tell application "iTerm"
activate
if (count of windows) = 0 then
set t to (create window with default profile)
else
set t to current window
tell t
create tab with default profile
end tell
end if
tell t
tell current session
write text cmd
end tell
end tell
end tell
end run
I took inspiration in the previous anwers and made a few extensions.
My solution accepts multiple input files (e.g. from a selection).
If there is an active window, the script searches for open vim sessions in any of the tabs and opens the files inside of that vim instance. If there are none, it creates a new iTerm tab or window.
on run {input, parameters}
set vimCommand to "nvim -p "
tell application "iTerm"
activate
if (count of windows) = 0 then
set w to (create window with default profile)
else
set w to current window
try
# raises error when all windows are minimized
tell w
# look for tab with open vim session
repeat with t in tabs
tell t
if name of current session contains "vim" then
# open files in current vim session
set esc to character id 27
tell current session
write text (esc & esc & ":silent! tablast")
repeat with filename in input
set filePath to quoted form of POSIX path of filename
write text (":execute 'tabedit '.fnameescape(" & filePath & ")")
end repeat
select
end tell
select
return
end if
end tell
end repeat
# no existing session
create tab with default profile
end tell
on error msg
set w to (create window with default profile)
end try
end if
# open in new tab or window
tell w
tell current session
set launchPaths to ""
repeat with filename in input
set filePath to quoted form of POSIX path of filename
set launchPaths to launchPaths & " " & filePath
end repeat
write text (vimCommand & launchPaths)
end tell
end tell
end tell
end run
Something like this in Automator:
on run {input, parameters}
set vim_par to POSIX path of input
set cmd to "/usr/local/bin/vim " & quote & vim_par & quote
tell application "iTerm"
tell current window
create tab with default profile command cmd
end tell
end tell
end run
Save as .app, and then open files via it -- probably, make that .app a default "Open with" app.
I'm really confused with this one. All I want is the files in the list to open. Here's my codes
set FilesList to {"Users/XXXXXX/Documents/07PictureArt-Related/THINGS THAT HELP/Tutorials/artNotesfromFeng.rtf", "Users/XXXXXXXX/Documents/07PictureArt-Related/THINGS THAT HELP"}
repeat with theFiles in FilesList
delay 0.1
tell application "Finder" to open POSIX file (theFiles)
end repeat
So, how come THAT won't work, but this will???
tell application "Finder" to open POSIX file "Users/XXXXXX/Documents/07PictureArt-Related/THINGS THAT HELP/Tutorials/artNotesfromFeng.rtf"
I'm thinking it might have to do with maybe the list is making it a string, and when I plug it directly into the open command, it LOOKS like a string, but it's not really...I don't know
For now I guess I just have to brute force it, and make a new statement for each file.
Thanks
Not sure what is going on there, i agree it's confusing.
An alternate is to use the shell 'open' command instead.
repeat with filePath in FilesList
do shell script "open " & quoted form of filePath
end repeat
The shell seems more happy with POSIX paths, the trick is to send in the 'quoted form' of your POSIX paths.
--
EDIT:
Putting into a var first works too.
repeat with theFiles in FilesList
set f to POSIX file theFiles
tell application "Finder" to open f
end repeat
It seems the Finder is causing the coercion to POSIX file problem.
Either of these works:
set l to {"/bin", "/etc"}
repeat with f in l
tell application "Finder" to open (POSIX file (contents of f) as alias)
end repeat
set l to {"/bin", "/etc"}
repeat with f in l
POSIX file f
tell application "Finder" to open result
end repeat
Try running this script:
set l to {"/bin", "/etc"}
repeat with f in l
f
end repeat
The result at the end is item 2 of {"/bin", "/etc"}. contents of returns the target of the reference object.
I don't know why POSIX file f doesn't work inside a tell application "Finder" block, or why POSIX file f as alias does work.
I have a VBA userform to open a text file in TextPad with the following:
Call Shell("C:\Program Files\TextPad 5\TextPad.exe " & "C:\testfile.txt", vbNormalFocus)
I'd like the text file to be displayed at a specific line number.
Is there a way to pass the line number as an argument to this Shell call?
You can accomplish this using WScript.Shell.SendKeys to trigger the goto line shortcut within TextPad (ctrl-G)
Dim wshshell
Set wshshell = CreateObject("WScript.Shell")
Call Shell("C:\Program Files\TextPad 5\TextPad.exe " & "C:\testfile.txt", vbNormalFocus)
Application.Wait (10)
wshshell.SendKeys "^g" 'ctrl-G
Application.Wait (10)
wshshell.SendKeys "15" 'desired line number
Application.Wait (10)
wshshell.SendKeys "{ENTER}" 'enter
You might have to mess with the Wait lines to add or remove delays between commands, but I tested this from within a VBA module in Excel and it worked.
I am aware that SendKeys that can be called directly from VBA, but when I tried to use that, it seems to be bound to the vba editor window.
You can also call notepad++ with the "-n" argument for the same effect,
Sending keys (specially with delays) might be a dangerous thing if you switch screens or a popup window appears while the command is executing you might lose data or inadvertly execute a dangerous action in that other application.
I am totally new to Vim and just two days into learning it. Like every Vim user I am deeply impressed by the speed boost it has given me.
I create Windows batch(.bat) files with numerous call statements. Usually I run a single call statement with Run prompt and use a .bat file for a set of statements. This is done to ensure that the statements provide desired results. I read that it is possible to run similar scenarios from within Vim. Here are some sample statements which form a part of my batch file:
A single line statement:
call %VT_BATCH_ROOT%\Menu\Sub-menu\Name "Line Geometry" "" "" "OK"
A multi-line scenario:
call %VT_BATCH_ROOT%\Menu\Sub-menu\Command "Yes" "c:\temp\folder\file.ext"
call %VT_BATCH_ROOT%\Menu\Sub-menu\Command "No"
call %VT_BATCH_ROOT%\Menu\Sub-menu\Command "Yes" "" "c:\temp\folder\file.ext" "" "" ""
Can someone suggest me how can my requirements be achieved?
I'm not sure if this is what you're asking, but you can use :! to execute a shell command. For example, type :!dir to open a shell and list the current directory. If you already have the command you want to run open as part of the file you are editing, you could copy and paste it in this way.
Edit: To execute the current line as a shell command in Windows, you can do :.w !cmd -. For multiple lines, you could record a macro that did this and then moved to the next line, and execute the macro as many times as necessary.