如何将多个变量从 AppleScript 显示对话框传递到 bash

发布于 2025-01-09 23:14:54 字数 1412 浏览 1 评论 0 原文

我遇到了在 AppleScript 和 bash 之间传递多个变量的问题。

我可以使用类似这样的方法获取一个变量

updatedName="$(osascript -e 'set the answer to text returned of (display dialog "What is your name" with title "Question" default answer buttons {"Cancel", "Save"} default button "Save")' return answer)"

How do I get the answer to the text and which button was selected? 我基本上想在选择取消按钮时退出整个脚本,但我似乎无法同时输入输入的文本和按下的按钮。

我尝试过类似的操作,但“取消”按钮返回“26:246:执行错误:用户已取消。(-128)”,而不是 if 语句中的退出 0。

question="$(osascript -e 'set theResultReturned to (display dialog "Enter your nane" with title "Question" default answer "" buttons {"Cancel", "Rename"} default button "Rename")' return answer)
set theTextReturned to the text returned of theResultReturned
set theButtonReturned to the button returned of theResultReturned
if theButtonReturned is "Cancel" then
    exit 0
end if" || exit

我还尝试在问题脚本后添加一个额外的 if 语句,但我也无法让它工作。

if [ "$question" = "theButtonReturned:Cancel" ];
then
    exit 0
fi

如果我打印 $question,我会得到完整的输出,

button returned:Rename, text returned:Test
set theTextReturned to the text returned of theResultReturned
set theButtonReturned to the button returned of theResultReturned
if theButtonReturned is Cancel then
    exit 0
end if

它为我提供了返回的文本和按钮输出,但它也打印出了整个 osascript。

我确信我错过了一些简单的事情。我查遍了谷歌和堆栈溢出,但所有示例都仅针对一个变量。

I am running into issues passing multiple variables between AppleScript and bash.

I can get one variable using something like this

updatedName="$(osascript -e 'set the answer to text returned of (display dialog "What is your name" with title "Question" default answer buttons {"Cancel", "Save"} default button "Save")' return answer)"

How do I get both the answer to the text and which button was selected?
I basically want to exit the entire script when the cancel button is selected but I can't seem to get both the text entered and which button was pressed.

I have tried something like this but the Cancel button returns "26:246: execution error: User canceled. (-128)" not the exit 0 from the if statement.

question="$(osascript -e 'set theResultReturned to (display dialog "Enter your nane" with title "Question" default answer "" buttons {"Cancel", "Rename"} default button "Rename")' return answer)
set theTextReturned to the text returned of theResultReturned
set theButtonReturned to the button returned of theResultReturned
if theButtonReturned is "Cancel" then
    exit 0
end if" || exit

I've also tried adding an additional if statement after the question script but I can't get that to work either.

if [ "$question" = "theButtonReturned:Cancel" ];
then
    exit 0
fi

If I print $question I get a full output of

button returned:Rename, text returned:Test
set theTextReturned to the text returned of theResultReturned
set theButtonReturned to the button returned of theResultReturned
if theButtonReturned is Cancel then
    exit 0
end if

Which is giving me the returned text and the button output but then it is also printing out the entire osascript.

I'm sure I'm missing something simple. I've looked all over google and stack overflow but all the examples are for only one variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2025-01-16 23:14:54

你说:

“我基本上想在选择取消按钮时退出整个脚本,但我似乎无法同时获取输入的文本和按下的按钮。”

首先,不可能同时返回在用户单击“取消”按钮的情况下输入的文本以及按下的按钮。这只是因为取消对话框时 AppleScript 会产生您提到的 -128 错误。

考虑采用不同的方法,使用以下 shell 脚本。如果用户选择“取消”按钮,它实际上会提前退出 bash 脚本,退出代码为 1。但是,当单击“确定”按钮时,输入的任何文本(即用户名)都会分配给username变量。

#!/usr/bin/env bash

if ! username=$(osascript 2> /dev/null <<-EOF
  return text returned of (display dialog "Enter your name" with title ¬
    "Question" default answer "" buttons {"Cancel", "Rename"} default button "Rename")
EOF
); then
  exit 1
fi

# Continue to do something with $username...
echo "$username"

解释

  • $(...) 部分(命令替换) 用于分配 osascript 命令到用户名
    变量。

  • 2> /dev/null 部分将 stderr 重定向到 /dev/null。这基本上可以防止用户单击“取消”按钮时将 AppleScript -128 错误消息打印到控制台。

  • <<-EOF 部分(此处文档)用于将AppleScript代码传递给osascript命令。将多行代码(本例中为 AppleScript)传递到 shell 时,使用 Here 文档特别有用。

  • 条件 if< /code> 语句以撇号(! 表达式)开头,如果表达式为 false,则表示 true。本质上,在这种情况下,这意味着如果 osascript 命令的结果为 false(即用户单击了“取消”),则执行 exit 1 声明。


附加说明:

上述示例代码在 macOS Monterey (12.2.1) 上成功运行,但是在 OSX 上的某些旧版本上,您可能需要包含 AppleScript“系统事件” tell 块和 activate 语句。当我在运行 OSX (10.6.8) 的旧计算机上测试脚本(上面)时,如果没有它们,对话框就不会出现。例如,您可能需要执行以下操作,该操作在 OSX (10.6.8) 上成功运行:

#!/usr/bin/env bash

if ! username=$(osascript 2> /dev/null <<-EOF
tell application "System Events"
  activate
  return text returned of (display dialog "Enter your name" with title ¬
    "Question" default answer "" buttons {"Cancel", "Rename"} default button "Rename")
end tell
EOF
); then
  exit 1
fi

# Continue to do something with $username...
echo "$username"

You say:

"I basically want to exit the entire script when the cancel button is selected but I can't seem to get both the text entered and which button was pressed."

Firstly, it's not possible to return both the text entered and which button was pressed in a scenario whereby the user has clicked the "Cancel" button. This is simply because when cancelling a dialog AppleScript produces the -128 error that you have mentioned.

Consider taking a different approach by utilizing the following shell script instead. It essentailly exits the bash script early with an exit code of 1 if the user selects the "Cancel" button. However, when the "OK" button is clicked any text entered (i.e. the user name) is assigned to the username variable.

#!/usr/bin/env bash

if ! username=$(osascript 2> /dev/null <<-EOF
  return text returned of (display dialog "Enter your name" with title ¬
    "Question" default answer "" buttons {"Cancel", "Rename"} default button "Rename")
EOF
); then
  exit 1
fi

# Continue to do something with $username...
echo "$username"

Explanation

  • The $(...) part (command substitution) is utilized to assign the result from the osascript command to the username
    variable.

  • The 2> /dev/null part redirects stderr to /dev/null. This is what essentially prevents the AppleScript -128 error message from being printed to the console when the user clicks the "Cancel" button.

  • The <<-EOF part (a Here document) is utilized to pass the AppleScript code to the osascript command. It's particularly useful to use a Here document when passing multiple lines of code, (AppleScript in this insance) to the shell.

  • The conditional if statement begins with an apostrophe, (a ! expression), which means true if the expression is false. Essentially in this scenario it means if the result of the osascript command is false (i.e. the user has clicked "Cancel") then excute the exit 1 statement.

Additional Note:

The aforementioned example code runs succesfully on macOS Monterey (12.2.1), however on some older versions on OSX you may need to include an AppleScript "System Events" tell block and activate statement. When I tested the script (above) on an old machine running OSX (10.6.8) the dialog did not appear without them. For example you may need to do the following which did work successfully on OSX (10.6.8):

#!/usr/bin/env bash

if ! username=$(osascript 2> /dev/null <<-EOF
tell application "System Events"
  activate
  return text returned of (display dialog "Enter your name" with title ¬
    "Question" default answer "" buttons {"Cancel", "Rename"} default button "Rename")
end tell
EOF
); then
  exit 1
fi

# Continue to do something with $username...
echo "$username"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文