1

I am interested in creating a dmg disk image on MacOS X from Python, and came across the following solution: How do I create a nice-looking DMG for Mac OS X using command-line tools?

However, I am running into a strange issue related to path lengths. The following script illustrates my problem:

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)

If run, the background will get correctly set in the disk image called 'works', and not in the one called 'doesnotwork'. It seems that I am limited to 5 characters for the volume name. However, if I shorten the name of the folder use to store the background, e.g. to .bkg instead of .background, then I can use a longer volume name, which suggests this is an issue related to the length of the overall path. Does anyone know at what level there is a limit on the path length? Is there a workaround to allow arbitrarily long paths?

EDIT: I am using MacOS 10.6 - the script seems to work correctly on 10.7

Community
  • 1
  • 1
astrofrog
  • 32,883
  • 32
  • 90
  • 131
  • 10.6.8 here, your script works fine. – nneonneo Oct 14 '12 at 18:10
  • Can you pinpoint the longest volume name this will accept? – nneonneo Oct 14 '12 at 18:11
  • @nneonneo - in the above example, I cannot make ``NAME`` more than 5 characters. If I change ``.background`` to ``.back``, I can make it no more than 8, and if I change to ``.b``, I can make it no more than than 9. Weird! I also cannot reproduce on a different machine with 10.6. What could be causing this? (I use FileVault on this machine, could it be to do with that?) – astrofrog Oct 14 '12 at 18:24
  • Does it give any sort of error? Anything in `console.log`? Does it work if you add `update with necessity` before the `close`? – nneonneo Oct 14 '12 at 18:36
  • Both the one that works and the one that doesn't given an error `557:560: execution error: Can’t get end. (-1728)` so I think it's unrelated. If I put `update with necessity` before close, it just hangs (on the first one). – astrofrog Oct 14 '12 at 19:52
  • It hangs? That's interesting. Do you perhaps have some third-party Finder extensions installed? – nneonneo Oct 14 '12 at 19:53
  • @nneonneo - it must be some extension, I started up in safe boot mode, and it no longer hangs, although it still does not work, and still gives the 'can't get end' error... – astrofrog Oct 14 '12 at 20:18
  • I get "can't get end" too, so I wouldn't worry about it. – nneonneo Oct 14 '12 at 20:19

1 Answers1

0

I believe you have to escape your quotes. You can't have quotes inside of quotes without escaping them. For example it should be, tell application \"Finder\". You have many places where your quotes are not used properly. If you look at the example script you linked to that person used single and double quotes cleverly to avoid this issue. My suggestion is you fix that.

Plus you can't refer to a file in applescript like this... ".background:background.png". Applescript doesn't know what that means when a path begins with a period. A path in applescript begins with the name of the hard drive like Macintosh HD. You need to put the whole path there in a proper applescript format. And that needs to be quoted too with escaped quotes.

Good luck.

regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • If that is true, why does it work if the filename is short enough? – astrofrog Oct 14 '12 at 23:03
  • I can't answer that. When you misuse the quotes it's hard to tell exactly what is happening. It's up to you to decide if my advice is worth taking. – regulus6633 Oct 14 '12 at 23:08
  • I tried explicitly escaping the quotes, but this doesn't change anything - double quotes don't need to be escaped inside triple quotes in Python (i.e. `""" "hello" """` would print/execute as `"hello"`). – astrofrog Oct 15 '12 at 00:03