Bash-带有对话框确认的卸载应用程序

时间:2018-07-20 19:45:13

标签: xcode bash macos installation applescript

我有一个示例,希望我的对话框与Applescript相似,但无法弄清楚如何在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

Applescript:

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

2 个答案:

答案 0 :(得分:1)

这是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以使其静音。这只是一种替代方法,而不是在选择删除文件之前先检查确认文件是否存在

答案 1 :(得分:0)

这是我想出的解决方案。很棒!感谢您的帮助!

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