2

我编写了一个脚本来将用户的公司网络共享添加到他们在 Mac OS X 中的“连接到服务器”列表中(Finder --> Go --> Connect to Server...)。此脚本直接写入 com.apple.sidebarlists.plist 的备份,然后在使用 mv 验证后替换它。我无法让 Finder 认识到已对此 plist 进行了更改。重新启动系统会很好地显示列表中的新服务器。但是注销/登录和重新启动(杀死)Finder 不会。我也试过 killall SystemUIServer 和 killall cfprefsd ( http://blog.design79.co.uk/?p=1761 ) 没有运气。

有没有人遇到过这种情况,是否有解决方案可以在不重新启动的情况下更新列表?

谢谢!

这是我的脚本的一部分,它执行写入/移动/等以供参考。

# ...
    cp "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist" "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: The users com.apple.sidebarlists.plist could not be backed up. Aborting...."
        exitfunction
    else
        echo "$(date): com.apple.sidebarlists.plist was backed up."
    fi  

    defaults delete "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist" favoriteservers
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not remove favoriteservers from com.apple.sidebarlists.mod.plist. Aborting...."
        exitfunction
    else
        echo "$(date): Favoriteservers was removed from com.apple.sidebarlists.plist."
    fi  

    defaults write "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist" favoriteservers -dict Controller CustomListItems CustomListItems REPLACEME
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not add favoriteservers to com.apple.sidebarlists.mod.plist. Aborting...."
        exitfunction
    else
        echo "$(date): Favoriteservers was re-added to com.apple.sidebarlists.plist with placeholder."
    fi  

    plutil -convert xml1 "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not convert com.apple.sidebarlists.mod.plist to XML1 format. Aborting...."
        exitfunction
    else
        echo "$(date): com.apple.sidebarlists.mod.plist was successfully converted to XML1."
    fi  

    sed -i "" "/ *<string>REPLACEME<\/string>/r $tempfile5" "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"

    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not inject new server list. Aborting...."
        exitfunction
    else
        echo "$(date): New server list was injected into com.apple.sidebarlists.mod.plist."
    fi  

    sed -i "" 's/ *<string>REPLACEME<\/string>//g' "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not remove placeholder tag in com.apple.sidebarlists.mod.plist. Aborting...."
        exitfunction
    else
        echo "$(date): Placeholder tag was removed from com.apple.sidebarlists.mod.plist."
    fi  

    plutil -convert binary1 "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not convert com.apple.sidebarlists.mod.plist into binary format. Aborting...."
        exitfunction
    else
        echo "$(date):com.apple.sidebarlists.mod.plist was successfully converted into binary1."
    fi

    plutil -lint "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR: Could not validate com.apple.sidebarlists.mod.plist. Aborting...."
        exitfunction
    else
        echo "$(date):com.apple.sidebarlists.mod.plist was successfully verified as a valid XML file."
    fi  

    mv "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist" "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist.old"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR:  com.apple.sidebarlists.plist could not be moved. Aborting...."
        exitfunction
    else
        echo "$(date):com.apple.sidebarlists.mod.plist was successfully renamed to com.apple.sidebarlists.plist.old."
    fi  

    mv "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.mod.plist" "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist"
    if [[ "$?"  != 0 ]]; then
        echo "$(date):ERROR:  apple.sidebar.mod.plist could not be moved. Aborting...."
        mv "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist.old"  "$enduserhomefolder/Library/Preferences/com.apple.sidebarlists.plist"
        echo "$(date):ERROR:  com.apple.sidebarlists.plist plist restored from backup"
        exitfunction
    else
        echo "$(date):com.apple.sidebarlists.mod.plist was successfully renamed to com.apple.sidebarlists.plist.old."
    fi  

# ....
4

2 回答 2

1

You evidently already know about the defaults command. So why are you using it as a way to manipulate preference plist files? You shouldn't do that. Use the defaults command the way it's meant to be used to just modify the preferences in the cached database and let cfprefsd write the updated cache to the file at its leisure.

defaults write com.apple.sidebarlists favoriteservers -dict-add CustomListItems '( { Name = "whatever"; URL = "smb://server.example.com"; } )'

That said, the Finder still won't pick up the changes immediately, but it will once it's restarted. It might be sufficient to do killall -HUP Finder or something like that. (That will probably just kill the Finder at which point it will be automatically restarted.) You could also do something like:

osascript -e 'tell app "Finder" to quit'
sleep 1
osascript -e 'tell app "Finder" to launch'

Mind you, only do this with the permission of the user. Killing the Finder when the user isn't expecting it would be very rude and has the potential for data loss.

于 2014-11-22T05:08:19.160 回答
0

您可以在更改受到影响后和重新启动 Finder 之前尝试删除缓存:

~/Library/Caches/com.apple.finder
于 2014-11-22T05:21:16.787 回答