Applescript:应用程序始终在输入的情况下运行

Applescript:应用程序始终在输入的情况下运行,applescript,automator,Applescript,Automator,非常相似,但不等于: 我想要一个automator应用程序,它启动iterm并调用vim中的输入文件。 如果iterm是allready open,则不需要检查其vim;那就太好了!我想拆分窗口并在拆分窗口中打开文件 我的尝试: on run {input} -- get the file path incl extention set inputFilePath to POSIX path of input display dialog appIsRunning("iTerm") tel

非常相似,但不等于:

我想要一个automator应用程序,它启动iterm并调用vim中的输入文件。 如果iterm是allready open,则不需要检查其vim;那就太好了!我想拆分窗口并在拆分窗口中打开文件

我的尝试:

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
通过just run调用,两个显示都能正确回答天气状况,无论应用程序是否正在运行,一切就绪! 如果我使用该应用程序,但把一个文件放在它似乎,它首先启动iTerm,因为一些告诉声明,然后总是说它的allready开放

运行脚本并将所有内容都放入其中是行不通的。
关于为什么会发生这种情况,或者如何获得预期的行为,有什么建议吗?

我通过shell脚本找到了grep的解决方法。询问系统进程(如vim)未列出。现在,这将查找vim的开放进程,而不是无论如何都要打开的iTerm。如果它找到了vim,它将分割一个窗口更改您的命令!如果没有,它将启动一个新的vim,其中文件夹为路径,vim为该文件:

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
由来自美国的阿兰马库斯提供