首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bash -使用对话框确认卸载应用程序

Bash -使用对话框确认卸载应用程序
EN

Stack Overflow用户
提问于 2018-07-20 19:45:12
回答 2查看 426关注 0票数 0

我有一个用Applescript来显示对话框外观的例子,但是我想不出如何在Bash文件中这样做。

巴什:

代码语言:javascript
复制
#!/bin/bash
app="/Applications/Cisco Spark.app"
FileToDelete=$app
if [ -d "$app" ];  # Remove, if exists.
then
echo ""$FileToDelete""
rm -r "$FileToDelete" #Removing App 
else
echo $app
fi

Applescript:

代码语言:javascript
复制
display dialog "Webex Teams is replacing Cisco Spark! This installation will close and uninstall Cisco Spark." buttons {"Cancel Installation", "Install Teams"} default button "Install Teams"


if result = {button returned:"Install Teams"} then
    tell application "Cisco Spark"
        quit
    end tell
end if
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-26 20:23:17

这就是我想出的解决办法。效果很好!谢谢你的帮助!

代码语言:javascript
复制
#!/bin/bash
response=$(osascript -e 'button returned of (display dialog "Webex Teams is replacing Cisco Spark!\n" & "The full installation will close and uninstall Cisco Spark." buttons {"Cancel Spark Removal", "Remove Spark"} default button "Remove Spark")')

    # Exit script if user cancels
    [[ "$response" = "Cancel Spark Removal" ]] && exit 0

    #If not cancelled, delete application
    FileToDelete="/Applications/Cisco Spark.app"
    if [ -d "$FileToDelete" ];  # Remove, if exists.
    then
    echo "Closing Cisco Spark"
    killall "Cisco Spark" || echo "Spark wasn't open"
    echo "removing Cisco Spark.app"
    rm -r "$FileToDelete"
    else
    echo "Cisco Spark is not installed on this device"
    fi
票数 0
EN

Stack Overflow用户

发布于 2018-07-20 23:38:21

下面是您的bash脚本的修改版本,其中包括允许用户取消或继续的对话框弹出,如您的AppleScript中所示:

代码语言:javascript
复制
    #!/bin/bash
    response=$(osascript -e 'button returned of ¬
              (display dialog "Webex Teams is replacing Cisco Spark!\n" & ¬
              "This installation will close and uninstall Cisco Spark." ¬
              buttons {"Cancel Installation", "Install Teams"} ¬
              default button "Install Teams")')

    # Exit script if user cancels
    [[ "$response" = "Cancel Installation" ]] && exit 1

    # Get PID of "Cisco Spark" application
    pid=$(lsappinfo info -only pid "Cisco Spark" | egrep -o '\d+')
    # If running, quit the application
    [[ -z "$pid" ]] || kill -QUIT $pid

    # Delete the application file
    app="/Applications/Cisco Spark.app"
    rm -R "$app" 2>/dev/null && echo "Done." || echo "$app not found."

在最后一行中,我将stderr重定向到/dev/null,以便在应用程序文件不存在时保持沉默。这只是另一种方法,而不是在选择删除文件之前先检查文件是否存在。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51449414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档