2016-02-27 34 views
4

多年來,我一直使用以下AppleScript在iTerm2中的vim中打開txt文件,但自iTerm 2.9.2(又名iTerm3)以來,它已被破壞。任何人都可以建議如何更新這個AppleScript,以便它再次工作?如何更新蘋果腳本以在iTerm3 vim中打開txt文件

on run {input, parameters} 
    if (count of input) > 0 then 
     tell application "System Events" 
      set runs to false 
      try 
       set p to application process "iTerm" 
       set runs to true 
      end try 
     end tell 
     tell application "iTerm" 
      activate 
      if (count of terminals) = 0 then 
       set t to (make new terminal) 
      else  
       set t to current terminal 
      end if 
      tell t 
       tell (make new session at the end of sessions) 
        exec command ("vim \"" & POSIX path of first item of input as text) & "\"" 
       end tell 
       if not runs then 
        terminate first session 
       end if 
      end tell 
     end tell 
    end if 
end run 

我最初複製的腳本http://earthwithsun.com/questions/283418/how-can-i-make-terminal-vim-my-default-editor-application-in-mac-os-x但我沒有任何的AppleScript經驗。

任何幫助最受讚賞! B

+0

此代碼看起來像它來自Automator,是否正確?如果不是,你如何執行腳本? –

+0

是的,在Automator中,將AppleScript保存爲應用程序,然後將該應用程序與txt文件相關聯。在Finder中雙擊txt文件,它將在iTerm的vim中打開。 –

+0

在這個Automator工作流程中還有其他任何操作,或者只有您在此處運行的單個AppleScript? –

回答

6

使用@塵土飛揚的鏈接,我建議你改變你的運行AppleScript行動的整個代碼段這樣的:

on run {input, parameters} 
    tell application "iTerm" 
     activate 
     if (count of windows) = 0 then 
      set t to (create window with default profile) 
     else 
      set t to current window 
     end if 
     tell t 
      tell current session 
       write text ("vim \"" & POSIX path of first item of input as text) & "\"" 
      end tell 
     end tell 
    end tell 
end run 

它似乎在我的機器上工作,但我從來沒有使用vim,所以我不知道這是否會給你正是你想要的。

祝你好運,

+0

這是否需要代碼簽名?我試圖導出這個應用程序並運行它,我看到一個無操作。在控制檯(Mac應用程序)中,只有提示我看到'3/25/16 12:26:36.000 PM內核[0]:[90911 | 0] com.apple.CodeSi異步。掃描:#'/ Applications/VI.app/Contents/Info.plist'act = 0x00000002 rsn ='ETYP1'ln ='604'' –

+0

完美地工作,謝謝@ craig-smith –

2

新的Applescript語法不像以前那樣將terminal作爲頂級對象。它已被更改爲反映整個操作系統中用於其他腳本化應用程序的更多常用模式:應用程序的頂級對象稱爲窗口,而不是終端。

所以層次是現在這樣的:

  • 含含含
    • 會議
    • 一個或多個
      • 標籤的一個或多個
        • 窗口的應用

https://iterm2.com/applescript.html已經更新了新語法的例子。

0

像這樣的東西在Automator中:通過它

on run {input, parameters} 
    set vim_par to POSIX path of input 
    set cmd to "/usr/local/bin/vim " & quote & vim_par & quote 
    tell application "iTerm" 
     tell current window 
      create tab with default profile command cmd 
     end tell 
    end tell 
end run 

另存爲。應用程序,然後打開文件 - 可能,做出的.app一個默認的 「打開方式」 的應用程序。

2

我採用了克雷格史密斯的代碼片段(很好的回答!),並稍微調整它以打開一個新標籤,如果已經有一個打開的窗口。這樣,如果你已經在你的iTerm中打開了vim之類的東西,你就不會遇到任何麻煩。此外,該代碼將目錄更改爲文件的目錄,以使NerdTREE和fzf.vim等模糊查找程序開心。

on run {input, parameters} 
    set filename to POSIX path of input 
    set cmd to "clear; pushd " & quote & "$(dirname " & filename & ")" & quote & " > /dev/null; nvim " & quote & "$(basename " & filename & ")" & quote & "; popd > /dev/null" 

    tell application "iTerm" 
    activate 

    if (count of windows) = 0 then 
     set t to (create window with default profile) 
    else 
     set t to current window 
     tell t 
     create tab with default profile 
     end tell 
    end if 

    tell t 
     tell current session 
     write text cmd 
     end tell 
    end tell 
    end tell 
end run 
+0

請注意,當所有iTerm窗口都是最小化,因爲那時沒有「當前窗口」。用一個簡單的'try'塊修復。 – Huluk

0

我在前面的awers中得到了靈感,並做了一些擴展。 我的解決方案接受多個輸入文件(例如來自選擇)。 如果有活動窗口,腳本會在任何選項卡中搜索打開的vim會話並打開該vim實例內部的文件。如果沒有,則會創建一個新的iTerm選項卡或窗口。

on run {input, parameters} 
    set vimCommand to "nvim -p " 
    tell application "iTerm" 
     activate 

     if (count of windows) = 0 then 
      set w to (create window with default profile) 
     else 
      set w to current window 

      try 
       # raises error when all windows are minimized 
       tell w 
        # look for tab with open vim session 
        repeat with t in tabs 
         tell t 
          if name of current session contains "vim" then 

           # open files in current vim session 
           set esc to character id 27 
           tell current session 
            write text (esc & esc & ":silent! tablast") 
            repeat with filename in input 
             set filePath to quoted form of POSIX path of filename 
             write text (":execute 'tabedit '.fnameescape(" & filePath & ")") 
            end repeat 
            select 
           end tell 
           select 
           return 
          end if 
         end tell 
        end repeat 

        # no existing session 
        create tab with default profile 
       end tell 

      on error msg 
       set w to (create window with default profile) 
      end try 
     end if 

     # open in new tab or window 
     tell w 
      tell current session 
       set launchPaths to "" 
       repeat with filename in input 
        set filePath to quoted form of POSIX path of filename 
        set launchPaths to launchPaths & " " & filePath 
       end repeat 
       write text (vimCommand & launchPaths) 
      end tell 
     end tell 
    end tell 
end run 
相關問題