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

时间:2014-05-31 23:02:15

标签: applescript automator

我正在尝试创建一个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

这不符合我的意愿。

的烦恼:

  • 它在终端打开两个窗口,不知道为什么。它什么都没有 使用添加的“激活”...它总是提供
  • 如果我在finder(文件夹)上选择一个项目,它会打开它的父目录,我会的 喜欢打开所选文件夹

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

提前致谢

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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
相关问题