我有一个 Windows 10/Arch Linux 双启动设置。我已将两个系统配置为使用同一个桌面(只是符号链接/home/rawing/Desktop
到D:/Users/Rawing/Desktop
)。它运行正常,我可以访问两个系统上的所有文件,但我想同步桌面上文件的位置。例如,如果我在 Linux 上创建一个新文件并将其移动到桌面的右下角,Windows 仍会在左上角显示新文件,迫使我第二次重新定位文件。
有没有办法同步桌面上文件的位置?
答案1
每个操作系统的图标位置存储方式不同。您无法在 Windows 和 Linux 之间共享图标位置。
答案2
我取得了一些进展,想与大家分享一下。这还不是一个完整、可行的答案,但我已经扫清了一些障碍。
适用于 Linux 的 Python3 脚本:
def get_icon_positions_linux():
desktop= os.path.join(os.path.expanduser('~'), 'Desktop')
icons= {}
#try to find out the screen resolution
resolution= get_screen_resolution()
for name in os.listdir(desktop):
path= os.path.join(desktop, name)
data= subprocess.getoutput("gvfs-info -a 'metadata::nautilus-icon-position' "+shlex.quote(path))
if not data:
continue
match= re.search(r'(\d+),(\d+)\s*$', data)
x, y= int(match.group(1)), int(match.group(2))
if resolution is not None:
x/= resolution[0]
y/= resolution[1]
icons[name]= (x,y)
return icons
def set_icon_positions_linux(icons):
desktop= os.path.join(os.path.expanduser('~'), 'Desktop')
for name,(x,y) in icons.items():
path= os.path.join(desktop, name)
pos= ','.join(map(str, (x,y)))
subprocess.call(['gvfs-set-attribute', '-t', 'string', path, 'metadata::nautilus-icon-position', pos])
#FIXME: refresh desktop
和 Windows:
def get_icon_positions_windows():
# retrieves icon locations from the Windows registry. More info can be found here:
# https://superuser.com/questions/625854/where-does-windows-store-icon-positions
#FIXME: before doing anything, make explorer save the current positions
import winreg
icons= {}
key= winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\Shell\Bags\1\Desktop')
#find the key with the current screen resolution
resolution= get_screen_resolution()
valuename= 'ItemPos'+ 'x'.join(map(str, resolution))
for index in range(1024):
value= winreg.EnumValue(key, index)
if value[0].startswith(valuename):
break
#parse the icon locations
data= value[1]
#first 16 bytes are junk
data= data[0x10:]
format= 'HHIHHH'
name_offset= struct.calcsize(format)
while True:
# What they call "unknown padding" is exactly what we're looking for.
x, y= struct.unpack_from('II', data) # (x,y) in pixels on the Desktop from the top left corner.
data= data[8:]
if len(data)<name_offset:
break
size, flags, filesize, date, time, fileattrs= struct.unpack_from(format, data)
if size==0:
break
chunk= data[:size]
data= data[size:]
if size>=0x15:
chunk= chunk[name_offset:]
name= chunk[:chunk.find(b'\0')].decode()
icons[name]= (x/resolution[0], y/resolution[1])
return icons
def set_icon_positions_windows(icons):
"""
WARNING: This will kill and restart the explorer.exe process!
"""
import winreg
key= winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\Shell\Bags\1\Desktop', 0, winreg.KEY_ALL_ACCESS)
#find the key with the current screen resolution
resolution= get_screen_resolution()
valuename= 'ItemPos'+ 'x'.join(map(str, resolution))
for index in range(1024):
value= winreg.EnumValue(key, index)
if value[0].startswith(valuename):
valuename= value[0]
break
old_data= value[1]
#first 16 bytes are junk
new_data= old_data[:0x10]
old_data= old_data[0x10:]
format= 'HHIHHH'
name_offset= struct.calcsize(format)
while True:
if len(old_data)<8+name_offset:
break
size, flags, filesize, date, time, fileattrs= struct.unpack_from(format, old_data[8:])
if size==0:
break
chunk= old_data[8:8+size]
if size>=0x15:
chunk= chunk[name_offset:]
name= chunk[:chunk.find(b'\0')].decode()
if name in icons:
#update the position
x, y= icons.pop(name)
new_data+= struct.pack('II', x, y)
#copy everything else
new_data+= old_data[8:8+size]
old_data= old_data[8+size:]
continue
#if it's an invalid struct (size<0x15) or we're not supposed to change this file's position, copy the old value
new_data+= old_data[:8+size]
old_data= old_data[8+size:]
#if there are still icons remaining, we must construct their data from scratch
for name,(x,y) in icons.items():
name= name.encode()
if len(name)%2:
name+= b'\0'
size= name_offset + len(name) + 4
chunk= struct.pack('II', x, y)
chunk+= struct.pack(format, size, 0, 0, 0, 0, 0) #FIXME: set sensible values
chunk+= name
chunk+= struct.pack('HH', 0, 0) #FIXME: set sensible values
new_data+= chunk
#whatever data remained, add it.
new_data+= old_data
winreg.SetValueEx(key, valuename, 0, winreg.REG_BINARY, new_data)
#restart explorer for changes to take effect
subprocess.call(['taskkill', '/f', '/im', 'explorer.exe'])
subprocess.Popen(r'start %WINDIR%\explorer.exe"', shell=True)
该代码目前存在以下问题:
get_icon_positions_windows
不强制 Windows 资源管理器保存当前图标位置。它会创建最后保存位置的快照。set_icon_positions_linux
不刷新桌面。您可能需要按 F5 才能使更改生效。
这个想法是,无论用户何时注销或登录,都使用此代码将图标位置写入文件或从文件中加载图标位置。遗憾的是,我还没有找到在 Windows 上注销时运行命令的方法。(参见我的另一个问题)