0

I want to send a selected group of lines to my current ipython window from a texteditor (It's TextMate in this case, but that's largely irrelevant.) The script uses a bash call so it can accept the variable and then an Applescript call to push the code to the window.

This current script works, but it can only send a single non-nested line at a time. Is there a way to fix this so I can send multiple non-nested lines of code at once?

#!/bin/bash

QUOTED_TEXT=${TM_SELECTED_TEXT//\"/\\\"}
echo "$QUOTED_TEXT"

osascript <<- APPLESCRIPT
    tell application "Terminal"
        set currentTab to (selected tab of (get first window))
        set tabProcs to processes of currentTab
        set theProc to (end of tabProcs)
        if theProc is not "Python" then
            set currentTab to (do script "ipython")
        end if

        do script "$QUOTED_TEXT \n" in currentTab
    end tell
APPLESCRIPT
WildGunman
  • 393
  • 4
  • 13

1 Answers1

1

I don't use either TM or ipython myself so can't provide an immediate answer to your exact problem, but here's some general thoughts on calling AppleScript from Terminal:

  1. Never pass arguments to AS like that: it's a mis-quoting accident just waiting to happen. Wrap your AS code in an on run argv ... end run handler, then append your extra arguments to the osascript command when calling it in bash. osascript will then pass those arguments directly to AppleScript as a list of strings assigned to the argv variable. Safe and simple.

  2. Rather than wrap your AS code in a bash script, just add #!/usr/bin/osascript at the top of your AS code, save it as a plain text file in an appropriate location (e.g. somewhere on your shell's $PATH, such as /usr/local/bin), then do chmod +x /path/to/script to make it executable. This will allow you to run it directly from Terminal.

  3. If you want to access STDIN or environment variables directly within an AppleScript-based shell script, use the AppleScript-ObjC bridge to call NSFileHandle's fileHandleWithStandardInput()'s readDataToEndOfFile() and NSProcessInfo's processInfo()'s environment() respectively. To access ARGV, use an explicit run handler as described above.

  4. By default, osascript automatically writes the value returned by the run handler to STDOUT; alternatively, you can write directly to STDOUT at any time via NSFileHandler (you can put a plain return statement at the end of run handler to ensure it returns nothing else). And osascript automatically writes the results of log commands to STDERR, and sets the return code to non-zero when your script throws an uncaught exception (e.g. use an error ERROR_STRING number ERROR_NUMBER statement to raise an exception directly in your AS code).

(BTW, I wrote a File library not long ago that includes a bunch of very nice handlers for writing AS-based shell scripts. I no longer develop or support it myself; however, various folks have already forked it, so if you do much AS+shell work you may find it a helpful source of AS code to cut-and-paste or even to use as-is.)

foo
  • 3,171
  • 17
  • 18
  • Regarding point 2, I know you can invoke Applescript directly, but as far as I understand, TextMate can only pass it's environmental variables to Bash, Perl, PHP, Python, or Ruby. – WildGunman Aug 03 '16 at 20:26
  • Environment variables aren't language-specific. See #3. – foo Aug 04 '16 at 01:39
  • Not to be obtuse here, but the Objective-C bridge within Applescript is completely opaque to a non-developer. Are there any good examples of its use for simple workflow based Applescripts. – WildGunman Aug 05 '16 at 16:19
  • Yeah, that's why I wrote the aforementioned libraries, because AS users who have better things to do with their time should _never_ have to wade through the vast, sprawling complexities of Apple's Cocoa APIs just so they can do simple everyday tasks that AS should already support out of the box. (e.g. When writing AS-based shell scripts, take a look at the File library's dictionary in Script Editor to see what commands it provides, add `use script "File"` to the top of your script, then use those commands in your code as and when you need them.) – foo Aug 05 '16 at 17:41
  • If/When you ever get sufficiently ambitious/bored that you want to learn how to use the [[AppleScript-ObjC]] bridge yourself, Shane Stanley's [Everyday AppleScript-ObjC Explored](http://www.macosxautomation.com/applescript/apps/index.html) would be the best way to go as Apple's own site is utterly useless on the subject. (The 10.6 AS release notes contain a brief summary of available ASOC features, but nothing on how to actually use Cocoa in AS.) – foo Aug 05 '16 at 19:35