从Shell执行AppleScript时最后一行在Jamf日志中回显的问题
问题:Jamf策略中运行AppleScript时最后一行代码回显到日志的原因及解决办法
问题描述
通过Jamf策略中的bash脚本函数运行AppleScript(osascript)为控制台用户配置Finder边栏选项,脚本功能正常,但最后一行代码总会在Jamf日志中回显,本地Terminal运行时无此现象。想明确这是Jamf、Shell还是AppleScript导致的问题?
示例简化代码
function configure_sidebar() { echo "Configuring Finder Sidebar settings for console user..." sudo -u "$CONSOLE_USER_NAME" osascript -e ' activate application "Finder" tell application "System Events" tell process "Finder" delay 1.0 select menu bar 1 click menu bar item "Finder" of menu bar 1 delay 0.5 click menu 1 of menu bar item "Finder" of menu bar 1 click menu item "Settings…" of menu 1 of menu bar item "Finder" of menu bar 1 repeat until exists window "Finder Settings" end repeat click button "Sidebar" of toolbar 1 of window "Finder Settings" if (value of checkbox 1 of scroll area 1 of window "Finder Settings" as boolean) then click checkbox 1 of scroll area 1 of window "Finder Settings" end if if (value of checkbox 2 of scroll area 1 of window "Finder Settings" as boolean) then click checkbox 2 of scroll area 1 of window "Finder Settings" end if if not (value of checkbox 3 of scroll area 1 of window "Finder Settings" as boolean) then click checkbox 3 of scroll area 1 of window "Finder Settings" end if end tell end tell ' }
日志回显内容
Jamf日志中会回显:checkbox 3 of scroll area 1 of window Finder Settings of application process Finder,且无论最后一行执行的是什么命令,都会被回显,类似未正确关闭osascript或函数嵌套导致的现象。
原因分析
这是AppleScript的默认行为,和Jamf、Shell无关:
- AppleScript会自动将脚本最后一条执行语句的结果返回至标准输出(stdout);
- 本地Terminal运行时,可能因环境输出缓冲或终端配置未显示该返回值,但Jamf会捕获所有stdout内容并写入日志。
解决办法
方法1:在AppleScript末尾添加无输出语句
在AppleScript的最后(end tell之后)添加return或其他无输出的语句,覆盖默认的返回值:
end tell end tell return
方法2:重定向osascript输出到/dev/null
如果不需要捕获AppleScript的任何输出(包括错误信息),可以将osascript的输出重定向到空设备:
sudo -u "$CONSOLE_USER_NAME" osascript -e '...' > /dev/null 2>&1
注:此方法会屏蔽所有输出,如需保留错误日志,可仅重定向标准输出: > /dev/null
内容的提问来源于stack exchange,提问作者Daniel Stranathan
相关产品推荐
相关产品推荐

