Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A bash script to do the same thing (better?)' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A bash script to do the same thing (better?)
Authored by: tpavlic on Jul 11, '07 01:54:41PM

This might be a better idea. Create a file (e.g., /usr/local/bin/gvim-pdfsync) with these contents:

#!/bin/sh

filename="$1"
filebasename=`basename "$1"`
lineNum="$2"

exec osascript \
 -e "set CR to ASCII character of 13" \
 -e "set ESC to ASCII character of 27" \
 -e "tell application \"Vim\" to activate" \
 -e "tell application \"System Events\"" \
   -e "tell process \"Vim\"" \
   -e "keystroke ESC & \":set hidden\" & CR " \
   -e "keystroke \":if bufexists('$filename')\" & CR " \
   -e "keystroke \":    buffer $filebasename\"  & CR " \
   -e "keystroke \":else \" & CR " \
   -e "keystroke \":    edit ${filename// /\\ }\" & CR " \
   -e "keystroke \":endif\" & CR " \
   -e "keystroke \":$lineNum\" & CR " \
   -e "keystroke \"zO\" " \
   -e "end tell" \
 -e "end tell"
Also make sure to chmod it 0755. You can then execute gvim-pdfsync "%file" %line to open Vim at that line %line of file %file.

This prevents an error on my system that occurs with the "buffer" line. It also handles files with spaces a little better. Finally, it is a little easier to implement in your PDFSync-compatible viewers. In fact, I have tested it in Skim and it works great. Just do:

Command: gvim-pdfsync
Arguments: "%file" %line

Hope that helps.



[ Reply to This | # ]
Update: A bash script to do the same thing (better?)
Authored by: tpavlic on Jul 11, '07 03:17:29PM
This is a better version.
filename="$1"
lineNum="$2"

[ "${filename:0:1}" == "/" ] || filename="${PWD}/$filename"  

exec osascript 
 -e "set CR to ASCII character of 13" 
 -e "set ESC to ASCII character of 27" 
 -e "tell application "Vim" to activate" 
 -e "tell application "System Events"" 
   -e "tell process "Vim"" 
   -e "keystroke ESC & ":set hidden" & CR " 
   -e "keystroke ":if bufexists('$filename')" & CR " 
   -e "keystroke ":exe \":buffer \" . bufnr('$filename')"  & CR " 
   -e "keystroke ":else " & CR " 
   -e "keystroke ":    edit ${filename// /\ }" & CR " 
   -e "keystroke ":endif" & CR " 
   -e "keystroke ":$lineNum" & CR " 
   -e "keystroke "zO" " 
   -e "end tell" 
 -e "end tell"
This will fix problems when you have multiple files open with the same base name.

[ Reply to This | # ]
Update (be sure to add /bin/sh): A bash script to do the same thing (better?)
Authored by: tpavlic on Jul 11, '07 03:18:51PM
Of course, be sure to add the #!/bin/bash at the top of the script.

[ Reply to This | # ]
Bugfix (A bash script to do the same thing (better?))
Authored by: tpavlic on Jul 11, '07 05:38:21PM
macosxhints.com escaped things that it shouldn't have. Now I'll use a code environment. You should use THIS SCRIPT. Alternatively, just use the script I've linked here.
#!/bin/bash

filename="$1"
lineNum="$2"

[ "${filename:0:1}" == "/" ] || filename="${PWD}/$filename"  

exec osascript \
 -e "set ESC to ASCII character of 27" \
 -e "tell application \"Vim\" to activate" \
 -e "tell application \"System Events\"" \
   -e "tell process \"Vim\"" \
   -e "keystroke ESC & \":set hidden\" & return " \
   -e "keystroke \":if bufexists('$filename')\" & return " \
   -e "keystroke \":exe \\\":buffer \\\" . bufnr('$filename')\"  & return " \
   -e "keystroke \":else \" & return " \
   -e "keystroke \":    edit ${filename// /\\\\ }\" & return " \
   -e "keystroke \":endif\" & return " \
   -e "keystroke \":$lineNum\" & return " \
   -e "keystroke \"zO\" " \
   -e "end tell" \
 -e "end tell"


[ Reply to This | # ]
A few remarks
Authored by: Soliman on Jul 12, '07 03:17:21PM
I modified a bit your script for my setup : since I don't like the error message of z0 if there are no folds under the cursor, I removed that line.

Since I usually run vim in a Terminal with the corresponding title set I had to change the tell application "Vim" to activate into something much more complex:

 exec osascript \
   -e "set ESC to ASCII character of 27" \
   -e "set vimFound to false" \
   -e "tell application \"Terminal\"" \
   -e "   activate" \
   -e "   repeat with theWindow in windows" \
   -e "      if name of the theWindow is \"vim\" then" \
   -e "         set frontmost of theWindow to true" \
   -e "         set vimFound to true" \
   -e "      end if" \
   -e "   end repeat" \
   -e "end tell" \
   -e "if not vimFound then" \
   -e "   tell application \"Finder\" to open POSIX file \"$filename\"" \
   -e "   tell application \"System Events\" to delay 5" \
   -e "end if" \
   -e "tell application \"System Events\"" \
   -e "   tell process \"Vim\"" \
   -e "      if vimFound then" \
   -e "         keystroke ESC & \":set hidden\" & return " \
   -e "         keystroke \":if bufexists('$filename')\" & return " \
   -e "         keystroke \":exe \\\":buffer \\\" . bufnr('$filename')\"  & return " \
   -e "         keystroke \":else \" & return " \
   -e "         keystroke \":    edit ${filename// /\\\\ }\" & return " \
   -e "         keystroke \":endif\" & return " \
   -e "      end if" \
   -e "      keystroke \":$lineNum\" & return " \
   -e "   end tell" \
   -e "end tell"
Just in case somebody else has a setup as weird as I do :)

---
Sylvain

[ Reply to This | # ]