Bash - 通过对话框确认卸载应用程序

Bash - Uninstall Application with Dialog Box Confirmation

我有一个示例,说明我希望使用 Applescript 的对话框看起来像什么,但无法弄清楚如何在 Bash 文件中执行相同的操作。

Bash:

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

苹果脚本:

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

这是您的 bash 脚本的修改版本,其中包括允许用户取消或继续的对话框弹出窗口,就像在您的 AppleScript 中一样:

    #!/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 以在应用程序文件不存在的情况下将其静音。这只是一种替代方法,而不是先检查以确认文件确实存在,然后再选择删除它。

这是我想到的解决方案。效果很好!感谢您的帮助!

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