Force Finder识别更新的plist?

时间:2013-11-14 20:03:42

标签: macos bash plist osx-mountain-lion osx-mavericks

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

# ....

2 个答案:

答案 0 :(得分:1)

您显然已经了解defaults命令。那你为什么要用它来操作偏好plist文件呢?你不应该这样做。使用defaults命令的方式只是用来修改缓存数据库中的首选项,让cfprefsd在闲暇时将更新的缓存写入文件。

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

也就是说,Finder仍然不会立即接收更改,但一旦重新启动它就会恢复。执行killall -HUP Finder或类似的事情可能就足够了。 (这可能只会杀死Finder,此时它会自动重启。)你也可以这样做:

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

请注意,只有在用户允许的情况下才能这样做。当用户不期待它时杀死Finder会非常粗鲁并且有可能导致数据丢失。

答案 1 :(得分:0)

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

~/Library/Caches/com.apple.finder