Applescript to open terminal, run command, and show - Not working
Asked Answered
S

2

7

I´m trying to create a keyshortcut to open terminal in current folder. Looking around, I found this code to create a service (the part of adding the shortcut to this service is solved), only added things are the "; clear" and some of the "activate" so it shows

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

It is not working as i would like.

troubles:

  • it opens two windows in terminal, have no idea why. It has nothing to do with the added "activate"… it has always donde that
  • if I select an item on finder ( a folder ) it opens its parent directory and i would like it to open the selected folder

this is my very first try with Applescript so if the error is obvious i just can't see it

Thanks in advance

Sarsen answered 31/5, 2014 at 23:2 Comment(0)
F
12

The do script command already opens a window in Terminal. Try it this way:

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
Farceur answered 1/6, 2014 at 0:12 Comment(0)
K
9

I like the reopen approach better...

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
Kudu answered 1/6, 2014 at 0:34 Comment(4)
this works as I would like when the selected item is a file ( it opens a terminal window on parent folder ) however, it does exactly the same if it is a folderSarsen
I didn't read through your question completely. In that case, getting the selection is the better option. From there, I would still use the reopen command.Kudu
Indeed, reopen is the nicer solution.Farceur
"I like the reopen approach better..." - why?Prolamine

© 2022 - 2024 — McMap. All rights reserved.