是否有适用于 OS X 的 GUI 类型的“选择”应用程序?(类似于 PC 上的 choice.com)

是否有适用于 OS X 的 GUI 类型的“选择”应用程序?(类似于 PC 上的 choice.com)

我正在运行一个终端脚本,我真的很想将选项选择发送到 OS X GUI,类似于choice.comWindows/DOS 的工作方式,但是一个 GUI 版本。

我知道我可以使用 applescript 的choose from list命令,但我更喜欢原生应用程序,因为我认为它运行速度更快,而且更稳定、界面更直接。

我已经安装了 Platypus,我喜欢它将数据输出到 GUI 窗口的选项 - 但我希望能够从这样的窗口将数据传回脚本。

一个示例场景可能是从文件夹列表中进行选择的简单方法。当我使用 Applescript 执行此操作时,它看起来是这样的:

在此处输入图片描述

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

但是,我真的不想使用 Applescript。

资源

在我找到(或构建)完美的应用程序来实现这一点之前,我可能不得不使用 Applescript。我将列出我找到的一些资源:

从终端执行多行 applescript 的一个巧妙方法:

代码从这里

#!/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)'

该页面还值得注意的是:“osascript 尚未提供任何向脚本传递参数的方法”

“但有一个解决方法:”

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

我的贡献是一种强制对话成为焦点的方法:

#!/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"

还值得注意的是,您可以使用多个 -e 命令来传递多个要运行的字符串。

答案1

CocoaDialog

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

答案2

就像 Daniel Beck 在评论中说的:AppleScript 是你的最佳选择。它和其他语言(Cocoa、Carbon)一样“原生”,甚至比 Python、Perl、tcl/tk 等更原生。

答案3

这不是一个“非 AppleScript”的答案,但是有更好的方法将参数放入 AppleScript:

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

这避免了引用问题,因此您可以传递任意字符串(不要忘记"$quote"您的 shell 变量!)

相关内容