首页 文章

Applescript打开终端,运行命令和显示 - 不工作

提问于
浏览
5

我正在尝试创建一个keyhortcut来打开当前文件夹中的终端 . 环顾四周,我发现这个代码创建了一个服务(添加了这个服务的快捷方式的部分已经解决了),只有添加的东西是“; clear”和一些“激活”所以它显示

on run {input, parameters}

tell application "Finder"
    activate

    set myWin to window 1

    set theWin to (quoted form of POSIX path of (target of myWin as alias))

    tell application "Terminal"

        activate
        tell window 1
            activate
            do script "cd " & theWin & ";clear"
        end tell

    end tell

end tell


return input

end run

它没有像我想的那样工作 .

烦恼:

  • 它在终端打开两个窗口,不知道为什么 . 它与添加的"activate"无关......它一直都是这样做的

  • 如果我在finder(文件夹)上选择一个项目,它会打开它的父目录,我希望它打开所选的文件夹

这是我对Applescript的第一次尝试,所以如果错误很明显我就是看不到它

提前致谢

2 回答

  • 9

    do script 命令已在终端中打开一个窗口 . 试试这种方式:

    tell application "Finder" to set theSel to selection
    
    tell application "Terminal"
     set theFol to POSIX path of ((item 1 of theSel) as text)
     if (count of windows) is not 0 then
      do script "cd " & quoted form of theFol & ";clear" in window 1
     else
      do script "cd " & quoted form of theFol & ";clear"
     end if
     activate
    end tell
    
  • 8

    我更喜欢重新开放的方法......

    tell application "Finder" to set currentFolder to target of front Finder window as text
    set theWin to currentFolder's POSIX path
    
    tell application "Terminal"
        if not (exists window 1) then reopen
        activate
        do script "cd " & quoted form of theWin & ";clear" in window 1
    end tell
    

相关问题