Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm running a terminal script and I would really like to send option choices to the OS X GUI, similar to how choice.com works for Windows / DOS, but a GUI version.

I know I can use applescript's choose from list command, but I would prefer a more native app because I think it would run quicker, as well as being more stable and more straight forward to interface with.

I have installed Platypus, and I like how it has the option to output data to a GUI window - but I would like the functionality of passing data back to the script from such a window.

One example scenario might be an easy way to select from a list of folders. This is what it looks like when I do it with Applescript:

enter image description here

set listOfNames to {}
tell application "Finder"
    set filelist to every window
    repeat with currentFile in filelist
        set currentFileName to (POSIX path of (target of currentFile as alias)) as string
        copy currentFileName to the end of listOfNames
    end repeat
end tell
set mySelection to choose from list listOfNames

However, I would really prefer not to use Applescript.

Resources

Until I can find (or build) the perfect app to do this, I might have to use Applescript. I'm going to list a few resources I've found:

A clever way to do multi-line applescript from the terminal:

Code from here:

#!/bin/sh
# filename: find_me 
# usage from Terminal prompt: find_me "Last First"

osascript -e "set the_name to \"$1\"" -e 'on find_me (the_name)
   tell application "Address Book"
     set the_people to every person
     repeat with this_name in the_people
          if name of this_name contains the_name then 
             set result to name of this_name & "\n" 
             repeat with e_info in emails of this_name
               set result to result & value of e_info & " "
             end repeat
               set result to result & "\n"
             repeat with p_info in phones of this_name
               set result to result & value of p_info & " "
             end repeat 
             return result
         end if
     end repeat
   end tell
end find_me' -e 'find_me(the_name)'

Also noteworthy from that page: "osascript does not yet provide any way to pass arguments to the script"

"But there is a workaround:"

call osascript -e 'set thename to '$1 -e 'load script "/path/to/script"' -e 'look_up_name(thename)'

And my contribution, a way to force the dialog to be in focus:

#!/bin/sh
osascript -e "set front_app_name to short name of (info for (path to frontmost application))
    set listOfNames to {"a", "b", "c"}
    tell application front_app_name to choose from list listOfNames"

It is also good to note that you can use multiple -e commands to pass multiple strings to be run.

share|improve this question
1  
Using AppleScript is probably your best option. –  Daniel Beck Jun 28 '11 at 19:07
    
Also, osascript -e 'tell application "Terminal" to choose from list { "a", "b" }' is instantaneous on my machine. –  Daniel Beck Jun 28 '11 at 19:11
    
I haven't learned much about ApplesScipt, but indeed, what I've seen, makes it seem like a great solution. With my skill set, I'd try to use a Python driven GUI, probably through tcl/tk. But that's probably not what you mean by native app. If you were to write a Native App for this, you can call it from the terminal using the "open" command. I can imagine horrifying ways of interfacing that gui to a backend, but I will not share those as they're pretty horrid. –  Doc Jun 28 '11 at 19:45
    
I'm trying to call the script with Quicksilver, so I'm not sure Daniel's oascript, tell application "Terminal" is the best solution because it launches Terminal if it is not running. If I try and use tell application "Quicksilver" the dialog is not focused when it appears (keyboard nav not possible without a click). Seems like these problems would go away if there was a native app that could accept a list of choices and a callback - or even better if there was a Quciksilver plugin that allowed the results to be returned and displayed as a menu instead of just a block of text. –  cwd Jun 28 '11 at 19:55
    
If this is the only issue you face, just use Finder and activate it. I used Terminal as example, as it's the front application when using osascript from within it. I didn't expect you to actually have all the other code in AppleScript, since you don't want to actually use it. –  Daniel Beck Jun 28 '11 at 20:32
show 2 more comments

3 Answers

up vote 2 down vote accepted

CocoaDialog

/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog dropdown --title "Titlebar" --text "Prompt" --items A B --button1 OK --button2 Cancel

share|improve this answer
    
yes, yes yes! Thanks! Exactly what I was asking for!! –  cwd Jun 30 '11 at 1:35
add comment

Like Daniel Beck says in the comments: AppleScript is your best option. It's just as "native" as anything else (Cocoa, Carbon), if not more so (Python, Perl, tcl/tk, etc.).

share|improve this answer
    
I think of "Native" as meaning software that was compiled for a particular OS (unfocus.com/2011/03/25/what-is-a-native-app). Since AppleScript is not compiled, but rather requires the AppleScript engine to load in order to parse it, I would say that it is less native than Cocoa and Carbon apps. I don't think you're going to visit the App Store and buy anything written in AppleScript. –  cwd Jun 28 '11 at 20:00
3  
@cwd You can, in fact, get AppleScript apps on the App Store. While Cocoa apps are most often written in Objective-C, they can be written in AppleScript, too. Here is AppleScript app on the App Store: itunes.apple.com/us/app/wifi-analyzer/id422302958?mt=12 –  CajunLuke Jun 28 '11 at 20:07
    
Just out of curiosity, how did you know that one was written in Applescript? –  cwd Jun 28 '11 at 21:34
    
@cwd The author has ranted about things related to AppleScript and the App Store (separately) in relation to the app, both on Twitter and his blog. –  CajunLuke Jun 28 '11 at 21:43
    
I would like to mention that application is actually written in AppleScriptObjC and not just Applescript. –  cwd Jun 28 '11 at 22:09
add comment

This is not a "not AppleScript" answer, but there is a better way to get arguments into an AppleScript:

osascript -e '
  on run(args)
    ... choose from list args ...
  end
' 1 2 3

This avoids quoting issues, so you can pass arbitrary strings (don't forget to "$quote" your shell variables!)

share|improve this answer
    
Thanks, Kevin. This is good to know. –  cwd Jun 28 '11 at 22:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.