我遇到了在 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.
发布评论
评论(1)
你说:
首先,不可能同时返回在用户单击“取消”按钮的情况下输入的文本以及按下的按钮。这只是因为取消对话框时 AppleScript 会产生您提到的
-128
错误。考虑采用不同的方法,使用以下 shell 脚本。如果用户选择“取消”按钮,它实际上会提前退出 bash 脚本,退出代码为
1
。但是,当单击“确定”按钮时,输入的任何文本(即用户名)都会分配给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) 上成功运行:You say:
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 theusername
variable.Explanation
The
$(...)
part (command substitution) is utilized to assign the result from theosascript
command to theusername
variable.
The
2> /dev/null
part redirectsstderr
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 theosascript
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 theosascript
command is false (i.e. the user has clicked "Cancel") then excute theexit 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 andactivate
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):