You are not logged in.

#1 2015-09-08 11:34:03

karlch
Member
Registered: 2015-06-14
Posts: 105

[SOLVED] Shell, run cmd when prog has started up

Is there a way to run a shell command as soon as a program has started up?

At the moment I use this workaround, but it obviously isn't very elegant...

programcmd &
while true; do
    if (~/.i3/py-scripts/wincurrent.py | grep -i "programclass"); then
        # do something
        break
    fi
done

wincurrent.py simply prints the class of the currently focused program.

An example of my usage (but there are more):
Working on a latex document in vim I want to preview it with my favourite pdf reader. I have a keybinding which opens the pdf reader and then resizes it so both code and pdf are well aligned (no text wrapping in vim).

Last edited by karlch (2015-09-08 12:13:58)

Offline

#2 2015-09-08 12:00:07

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] Shell, run cmd when prog has started up

man xdotool wrote:
      search [options] pattern
           Search for windows with titles, names, or classes with a regular
           expression pattern.

           [snip]

           --sync
               Block until there are results. This is useful when you are
               launching an application want want to wait until the
               application window is visible.  For example:

                google-chrome &
                xdotool search --sync --onlyvisible --class "google-chrome"

Doesn't seem to cover the "currently focused" part, though, but do you need that?

Offline

#3 2015-09-08 12:11:59

karlch
Member
Registered: 2015-06-14
Posts: 105

Re: [SOLVED] Shell, run cmd when prog has started up

Awesome, thank you! Seems to react a littlle bit slower than the workaround but definitely solves the problem as the nicer solution.

I don't, it was just the only way I could think of to solve the issue.

Offline

#4 2015-09-08 12:16:27

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] Shell, run cmd when prog has started up

karlch wrote:

Working on a latex document in vim I want to preview it with my favourite pdf reader. I have a keybinding which opens the pdf reader and then resizes it so both code and pdf are well aligned (no text wrapping in vim).

I don't know how this fits with the goal you described in your post.  What program/script do you need to have wait for what other program/script to finish starting up?

Have you looked into latexmk and it's (pre)view options?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2015-09-08 12:34:55

karlch
Member
Registered: 2015-06-14
Posts: 105

Re: [SOLVED] Shell, run cmd when prog has started up

I simply want to resize zathura and re-focus vim as soon as zathura has finished loading, it's probably easier if I just share what I use to explain.
This is in my ~/.vim/ftplugin/tex/tex_settings.vim

if len(serverlist()) > 1
    nnoremap <leader>d :AsyncCommand(~/.vim/ftplugin/tex/start_zathura.sh %:t:r.pdf)<CR>:redraw<CR>
else
    nnoremap <leader>d :!~/.vim/ftplugin/tex/start_zathura.sh %:t:r.pdf<CR><CR>:redraw<CR>
endif

And the now modified start_zathura.sh is the following:

#! /bin/bash

zathura &>/dev/null >/dev/null $1 &
xdotool search --sync --onlyvisible --class "Zathura"
i3-msg -q resize shrink width 50 px or 10 ppt
i3-msg -q focus left

I remember looking into it a while ago, but I shall do so again, thanks for the recommendation.
Currently I compile with rubber and a slightly modified tex_pdf.vim http://www.vim.org/scripts/script.php?script_id=3230

-- EDIT --
The viewing capabilites of latex-mk gave me the idea of implementing my viewing script into the BuildAndViewTexPdf function of tex_pdf.vim.
Thanks again! (:

Last edited by karlch (2015-09-08 13:12:44)

Offline

#6 2015-09-08 13:37:43

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] Shell, run cmd when prog has started up

If it's working, that's great.  But note that latex-mk != latexmk.  The latter is part of texlive-core and seems much smoother to me.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2015-09-08 13:57:16

karlch
Member
Registered: 2015-06-14
Posts: 105

Re: [SOLVED] Shell, run cmd when prog has started up

Uhh, now how could i miss that? yikes
I should really look into latexmk then although I don't think I will leave rubber anytime soon.

Offline

Board footer

Powered by FluxBB