-->

Why does this preinstall script just partly run?

2019-08-19 03:32发布

问题:

I use PackageMaker to create an installation package. The following is my preinstall script to be called by Installer:

#!/bin/sh
/usr/bin/osascript <<EOF
tell application "System Events"
if exists (application process "Dictionary") then
    tell application "Dictionary" to quit
end if
end tell
set theFolder to (path to library folder as text) & "Dictionaries:"
set fileNames to {"dict1.dictionary", "dict2.dictionary", "dict3.dictionary", "dict_n.dictionary"}
set dict to {}
repeat with aFile in fileNames
tell application "Finder"
    if exists file (theFolder & aFile as text) then set end of dict to aFile & return
end tell
end repeat
try
tell application "System Events"
    if dict ≠ {} then display alert "You have XYZ installed" message "Choose 'Upgrade' to install the new version or 'Cancel' if you want to stay with the current version." & return & dict buttons {"Cancel", "Upgrade"} default button "Upgrade"
    if the button returned of the result is "Cancel" then
        tell current application
            set app_name to "Installer"
            set the_pid to (do shell script "ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}'")
            if the_pid is not "" then do shell script ("kill -9 " & the_pid)
        end tell
    end if

end tell
end try
EOF

This script works well in AppleScript Editor as well as in Terminal, i.e. it closes Dictionary app if it's running and force quits Installer if user chooses Cancel.

However, when called during the installation process, it just partly runs: it closes Dictionary app but bypasses force quitting Installer when Cancel button is chosen. Note that I have done chmod 755 the preinstall file.

What have I missed? What have I done wrongly? Can you please give a little help?

Thank you very much.