Applescript打开终端、运行命令和显示-无法工作

7

我正在尝试创建一个快捷键,以打开当前文件夹中的终端。查看过一些资料后,我发现了以下代码用于创建服务(已经解决了将快捷键添加到此服务的部分)。唯一添加的内容是“; clear”,以及一些“activate”,这样它就可以显示。

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个回答

12
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

9

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

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

当所选项目为文件时,此代码按照我期望的方式工作(它会在父文件夹中打开终端窗口),但是如果所选项目是文件夹,则会完全相同。 - divmermarlav
我没有完全阅读你的问题。在这种情况下,获取选择是更好的选择。从那里开始,我仍然会使用重新打开命令。 - adayzdone
确实,重新打开是更好的解决方案。 - user309603
我更喜欢重新打开的方法,为什么呢? - ian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接