【问题标题】:Applescript: App always running with inputApplescript:应用程序始终使用输入运行
【发布时间】:2014-06-12 14:52:08
【问题描述】:

非常相似,但不等于: How to check in AppleScript if an app is running, without launching it - via osascript utility

我想要一个自动化应用程序,它启动 iterm 并在 vim 中调用输入文件。 如果 iterm 已经打开(实际上不需要检查它是否是 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,因为某些告诉语句,然后总是说它已经打开!

运行脚本并将所有内容放入“”中不起作用。 关于为什么会发生这种情况或如何获得预期行为的任何建议?

【问题讨论】:

    标签: applescript automator


    【解决方案1】:

    我通过 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
    

    想法由http://hintsforums.macworld.com/archive/index.php/t-27113.html 的 allanmarcus 提供

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多