sometimes my AppleScript code seems to fail, on other machines is works.
I run this AppleScript code as heredoc from inside a shell script. The shell script is a postinstall script run by a pkg installer and runs as root:
#!/bin/sh
set -x
logfile="/Library/Logs/EZEEP Connector Installer.log"
LogMessage()
{
echo $(date) $1 >> "${logfile}"
}
LogMessage "................................."
LogMessage "Installer postinstall started ..."
LogMessage "................................."
# set file system tag 'Printing' on app bundle:
/usr/bin/osascript <<'EOD'
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
on addTags:tagList forPath:posixPath -- add to existing tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
#display dialog aURL as string
-- get existing tags
set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
if theTags is not missing value then -- add new tags
set tagList to (theTags as list) & tagList
set tagList to (current application's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() -- delete any duplicates
end if
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end addTags:forPath:
tell application "Finder"
set thePath to (POSIX path of (application file id "com.thinprint.ezeep.Connector" as alias))
my addTags:{"Printing"} forPath:thePath
end tell
EOD
#sleep 10
sudo -u ${USER} /usr/bin/osascript -e 'tell application "/Applications/ezeep Connector.app" to launch'
LogMessage "................................."
LogMessage " Installer postinstall finished ."
LogMessage "................................."
Sometimes the targeted app bundle gets a tag, sometimes not. If I uncomment the last sleep in my code sample, then everything works as expected.
Even on 2 different VMs we have the tag always created on one VM and failing to create on the other.
Is there a proper way to wait for the result of the add tag operation and then proceed with the script? Or is the sleep 10 command a reliable solution to have it run on dozens or event hundreds of Macs without failing on even a few?
kind regards,
Robert