1

I want to send commands from TextWrangler to the terminal. Even if I found this, I don't understand how that works since I'm brand new to Unix and running Applescripts.

tell application "TextWrangler"
    set the_selection to (selection of front window as string)
    if (the_selection) is "" then
        set the_selection to line (get startLine of selection) of front window as string
    end if
end tell

tell application "Terminal"
    do script with command the_selection in window 1
end tell

I saved an Applescript that looks like this: enter image description here

Now, what I think is to put the script in the TextWrangler Scripts Folder: enter image description here

  1. But then, how do I send commands to the Terminal.app?
  2. How can I press cmd + enter to send a line from TextWrangler to the Terminal?
  • I want to run my file line by line. If I want to record all of what I'm doing to test different functions without running the whole script – M. Beausoleil Jul 23 '16 at 4:44
2

You must put the script in the "/Users/yourName/Library/Application Support/TextWrangler/Scripts" folder

After that:

  • Open the preferences of the TextWrangler
  • Select "Menus & ShortCuts" --> "Scripts" --> "your script" to add your shortcut to the script
  • TextWrangler accept the (cmd + enter) keys as shortcut enter image description here
  • This is exactly what I needed! I guess we can now use autocompletion in some way... I going to find out. Thanks! – M. Beausoleil Jul 23 '16 at 15:16
  • @jackjr300, What application did you process that Preferences image in to add the arrows, particularly the curved arrow? – user3439894 Jul 23 '16 at 16:06
  • 1
    @user3439894, I use the Preview application on El Capitan, menu "Tools" --> "Annotate" --> "Arrow".- Then drag one of the blue handles to move the arrow or resize it, drag the green handle to make a curve.- You can change the shape style, you can add text at the beginning or at the end of the arrow. – jackjr300 Jul 23 '16 at 22:14
  • 1
    @jackjr300, Thanks for the info on the image annotation. Also thanks for the tip on assigning command-enter from TextWrangler Preferences. I had tried assigning in the Apple's System Preferences > Keyboard > Keyboard Shortcuts > Application Shortcuts and on OS X 10.8.5 and while I can technically assign command-enter for the script menu command in TextWrangler nonetheless it doesn't work. It deletes what's selected and inserts a newline, which of course defeats the purpose. However when doing it from TextWrangler Preferences it works. So thanks for everything. +1 – user3439894 Jul 23 '16 at 23:39
1

In order to use an AppleScript script in TextWrangler's Script menu, it (or a link to it) must be in TextWrangler's Script folder located at, ~/Library/Application Support/TextWrangler/Scripts in order for it to appear on the menu. Note: The "~" in that path is your Home Folder and the Library folder may not be visible. You can open it from TextWrangler's Script menu, Open Scripts Folder command and place the script or link to the script from wherever you saved it.

For the purpose of answering this question I created an AppleScript named Run Selected Line(s) in Terminal.scpt and placed it in TextWrangler's Script folder and is now available on the Script menu in TextWrangle as show in the image below.

TW_Script_Menu.png

That said, the script you have, as written may fail as Terminal can be a bit finicky. In other words, if Terminal is closed when executing the script, then the code as written can produce an error, e.g.: "A scripting error has occurred: Terminal got an error: Can’t get window 1."

Even telling Terminal to activate didn't open a window like it does when opening Terminal from the Dock Tile I have set to "Keep in Dock" and I'd get the aforementioned error. So a Google query yielded much useful information. So I experimented with a few different solutions I saw and the code I'm using is shown below.


tell application "TextWrangler"
    activate
    set the_selection to (selection of front window as string)
    if (the_selection) is "" then
        set the_selection to line (get startLine of selection) of front window as string
    end if
end tell

tell application "Terminal"
    if not (exists window 1) then reopen
    activate
    -- delay 0.5
    do script with command the_selection in front window
end tell

Note that I've commented out the delay command with -- and may or may not be needed depending on if Terminal is initially closed and or how slow your system responds. The value is in seconds expressed decimally. You can uncomment and modify if/as necessary.

Image of code showing syntactical highlighting:

code showing syntactical highlighting

Note: This was tested under OS X 10.8.5 and TextWrangler v4.5.9 (3390) and may react differently in other versions of OS X and or TextWrangler.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .