【发布时间】:2020-10-31 03:44:04
【问题描述】:
有时我的 AppleScript 代码似乎失败了,但在其他机器上可以正常工作。
我在 shell 脚本中将这个 AppleScript 代码作为 heredoc 运行。 shell 脚本是由 pkg 安装程序运行的安装后脚本,并以 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 "................................."
有时目标应用程序包会获得标签,有时不会。如果我取消注释代码示例中的最后一个睡眠,那么一切都会按预期工作。
即使在 2 个不同的虚拟机上,我们也总是在一个虚拟机上创建标签,而在另一个虚拟机上创建失败。
有没有合适的方法等待添加标签操作的结果,然后继续执行脚本?或者 sleep 10 命令是一种可靠的解决方案,可以让它在数十台或数百台 Mac 上运行而不会在几台 Mac 上运行失败?
亲切的问候,
罗伯特
【问题讨论】:
标签: bash macos applescript osascript pkgbuild