Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 631
divmermarlav Avatar asked May 31 '14 23:05

divmermarlav


People also ask

How do I run an AppleScript on a Mac?

In the Script Editor app on your Mac, click the Run button in the toolbar, or press Command-R, to execute the commands in your script.

How do I open the terminal menu?

Open TerminalClick the Launchpad icon in the Dock, type Terminal in the search field, then click Terminal. In the Finder , open the /Applications/Utilities folder, then double-click Terminal.

How do I open an AppleScript from the terminal?

View Terminal's AppleScript dictionaryIn the Finder on your Mac, open the /Applications/Utilities folder. Drag the Terminal app icon onto the Script Editor app icon .


1 Answers

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
like image 180
user309603 Avatar answered Sep 28 '22 01:09

user309603