Python から MacOS X で dmg ディスク イメージを作成することに興味があり、次の解決策を見つけました。
ただし、パスの長さに関連する奇妙な問題が発生しています。次のスクリプトは、私の問題を示しています。
import os
import Image
for NAME in ['works', 'doesnotwork']:
if os.path.exists(NAME + '.dmg'):
os.remove(NAME + '.dmg')
if os.path.exists('/Volumes/' + NAME):
raise Exception("Need to eject /Volumes/%s first" % NAME)
nx = 256
ny = 256
image = Image.new("RGB", (nx, ny))
for i in range(nx):
for j in range(ny):
image.putpixel((i, j), (i, 0, j))
os.system('hdiutil create -volname %s -fs HFS+ -size 10m %s.dmg' % (NAME, NAME))
os.system('hdiutil attach -readwrite -noverify -noautoopen %s.dmg' % NAME)
os.mkdir('/Volumes/%s/.background' % NAME)
image.save('/Volumes/%s/.background/background.png' % NAME, 'PNG')
apple_script = """osascript<<END
tell application "Finder"
tell disk "%s"
open
set current view of container window to icon view
set toolbar visible of container window to false
set statusbar visible of container window to false
set the bounds of container window to {{100, 100, 355, 355}}
set theViewOptions to the icon view options of container window
set the background picture of theViewOptions to file ".background:background.png"
close
open
end tell
end tell
END""" % NAME
os.system(apple_script)
実行すると、背景は「doesnotwork」ではなく、「works」と呼ばれるディスク イメージに正しく設定されます。ボリューム名は5文字までに制限されているようです。ただし、背景を保存するために使用するフォルダーの名前を短くすると (たとえば、.bkg
の代わりに に.background
)、より長いボリューム名を使用できます。これは、これがパス全体の長さに関連する問題であることを示唆しています。パスの長さに制限があるレベルを知っている人はいますか? 任意の長いパスを許可する回避策はありますか?
編集: 私は MacOS 10.6 を使用しています - スクリプトは 10.7 で正しく動作するようです