我试图创建一个简单的程序,为用户提供运行Ditto终端命令的GUI,但我遇到了麻烦。我允许用户选择自己的路径,然后这些路径的变量进入shell脚本,该脚本将以管理员权限运行ditto。
我发布的代码大多不相关,但对于上下文来说是必要的:
on startDitto_(sender)
try
set dialogResult to display dialog ¬
"Are you ready to start copying files?" buttons {"Cancel", "Yes"} ¬
default button "Yes" cancel button ¬
"Cancel" giving up after 15
on error number -128
set userCanceled to true
end try
try
if userCanceled then
display dialog "User Cancelled"
else if gave up of dialogResult then
display dialog "Timed out"
else if button returned of dialogResult is "Yes" then
do shell script "ditto -V " & sourceFolder & " " & destFolder with administrator privileges
end if
end try
end
问题在于:
else if button returned of dialogResult is "Yes" then
do shell script "ditto -V " & sourceFolder & " " & destFolder with administrator privileges
end if
一切都很有效,直到你点击了#34;是的。"它不会继续,不会要求输入密码,只需坐下即可。我已经阅读了一些关于嵌入脚本的内容,但由于我在脚本中使用变量,因此不一定有效(除非脚本可以使用这些变量,但是当我嵌入script.sh时它没有'似乎工作)然后我领导了NSTask,但我还没有精通Objective-C或Cocoa。
答案 0 :(得分:1)
如果你真的想要抓住所有三个回答,我认为你不得不考虑做这样的事情...
try
set userResponse to button returned of (display dialog "Are you ready to copy?" buttons {"No", "Yes"} default button "Yes" giving up after 15)
if userResponse = "Yes" then
do shell script "ditto -V " & sourceFolder & " " & destFolder with administrator privileges
else if userResponse = "No" then
-- CANCEL
display dialog "Canceled"
else
-- TIMED OUT
display dialog "Timed out"
end if
end try
如果你真的不在乎捕捉"放弃"事件,你可以这样试试......
try
set userResponse to button returned of (display dialog "Are you ready to copy?" buttons {"Cancel", "Yes"} default button "Yes" giving up after 2)
if userResponse = "Yes" then
do shell script "ditto -V " & sourceFolder & " " & destFolder with administrator privileges
end if
on error err
display dialog err buttons {"OK"} default button 1
end try
答案 1 :(得分:0)
所以回答我自己的问题(如果将来有人需要这个)我最终使用了这个:
try
display dialog "Are you ready to copy?" buttons {"Cancel", "Yes"} default button "Yes" cancel button "Cancel" giving up after 15
set userResponse to (button returned of the result)
if userResponse is "Yes" then
do shell script "ditto -V " & sourceFolder & " " & destFolder with administrator privileges
else if button returned is "Cancel" then
return 1
end if
end try
老实说,这不是我想要这样做的方式,但它会暂时起作用。当我得到以前的工作方式(使用额外的取消选项)时,我也会发布它。