0

Pretty simmilar, but not equal to: How to check in AppleScript if an app is running, without launching it - via osascript utility

I want an automator app, which starts iterm and calls the input file in vim. If iterm is allready open (not really required to check if its vim; would be nice though!) I want to split the window and open the file in the split.

My attempt:

on run {input}
-- get the file path incl extention
set inputFilePath to POSIX path of input

display dialog appIsRunning("iTerm")

tell application "iTerm"
    if it is running then
        display dialog "yes"
        tell current terminal
            tell the last session
                write text ",w" -- split window vertically
                write text openInVim
            end tell
        end tell
    else
        display dialog "no"
        set openInVim to "vim " & quoted form of inputFilePath
        set text item delimiters to "/"
        set parentDir to text items 1 thru -2 of inputFilePath

        tell i term application "iTerm"
            activate
            tell current terminal
                tell the last session
                    delay 0.1
                    write text "cd " & parentDir & "; clear"
                    write text openInVim
                end tell
            end tell
        end tell
    end if
   end tell
 end run

on appIsRunning(appName)
   tell application "System Events" to (name of processes) contains appName
end appIsRunning

Invoked with just run, both displays give the right answer to weather or not the app is running allready! If I use the app however (drop a file on it) it seems, that it first starts iTerm because of some tell statement and then always says its allready open!

run script and putting everything into " " doesn't work. Any suggestions to why this may happen or how I can get the expected behaviour?

Community
  • 1
  • 1
mike
  • 791
  • 11
  • 26

1 Answers1

0

I found a workaround with grep via shell script. Asking the system processes like vim are not listed. This now looks for open processes of vim, instead of iTerm (which will be opened anyway). If it finds vim, it splits a window (change your commands!) and if not, it starts a new vim with the folder as path and a vim with that file:

on run {input}
-- "word 1 of myProcessInfo" is the unix id of the process
-- "word 2 of myProcessInfo" is the unix id of the parent process
-- "word 3 of myProcessInfo" is the name of the process
try
    set myProcess to "Vim" -- your process name here
    set myProcessInfo to do shell script ("ps -xco pid,ppid,comm | grep " & myProcess)
    set isProcessRunning to 1
on error
    set isProcessRunning to 0
end try

-- get the file path incl extention
set inputFilePath to POSIX path of input

if isProcessRunning = 1 then
    --display dialog "vim is already running"
    set openInVim to ":e " & inputFilePath as string

    tell application "iTerm"
        activate
        tell current terminal
            tell the last session
                -- split vim window!
                write text ":vsplit"
                tell i term application "System Events" to keystroke return
                write text openInVim
            end tell
        end tell
    end tell
else
    --display dialog "new iterm and vim"
    set openVim to "vim " & quoted form of inputFilePath
    set text item delimiters to "/"
    set parentDir to text items 1 thru -2 of inputFilePath

    tell application "iTerm"
        activate
        tell current terminal
            tell the last session
                delay 0.1
                write text "cd " & parentDir & "; clear"
                write text openVim
            end tell
        end tell
    end tell
end if
end run

idea courtesy of allanmarcus from http://hintsforums.macworld.com/archive/index.php/t-27113.html

mike
  • 791
  • 11
  • 26