Add a custom icon to a folder / DMG?

Hi Folks,

I have a situation where I need to assign a custom .icns file to a folder that will be converted into a DMG file once the user finishes filling it. For environments where Xcode is installed, this is easily accomplished with SetFile. However, on a non-Xcode system, there’s no such tool available.

Creating the DMG is easy enough with hdiutil, but I can’t find a way to set the custom icon flag for the DMG once the .VolumeIcon.icns file is copied to the DMG.

I know that MBS’ FolderItem.AddCustomIconMBS will set a file’s icon from a graphic-generated IconFamilyMBS, but I’m trying to not recreate the icon info since I already have the .icns file available.

Can anyone share a Xojo mechanism for doing this?

Here is the code from my FlashDMG app

    //
    // Custom_Icon
    //
    If custom_icon<>Nil And Not err_flag Then
      bg_path="/Volumes/"+Volume_name+"/.VolumeIcon.icns"
      cmd ="cp -R -P "+custom_icon.shellpath+" "+bg_path
      If Not Shell_Execute("Copying Custom Icon",cmd,retcode) Then
        err_flag=True
      End If
    End If
  End If
  //
  // Make it Fancy 
  //
  If Not err_flag Then 
    s=""
    if option_bg_picture<>nil then s=".background/"+option_bg_picture.displayname
    cmd=Make_Fancy(Volume_name,s)
    cmd="OSASCRIPT "+cmd
    Call Shell_Execute("Applying Visual Attributes",cmd,retcode) 
  End If
  // set volume icon
  If Not err_flag And Custom_Icon<>Nil Then 
    cmd="/Developer/Tools/SetFile -a C "+dquote+"/Volumes/"+volume_name+"/"+dquote
    If Not Shell_Execute("Activate Custom Icon",cmd,retcode) Then
      err_flag=True
    End If
  End If

Note this code is many years old… so just take it as an illustration…

also code is out of context… so there are variables and flags you will have to infer

Pretty handy Dave. Thanks for sharing

Thanks Dave, but that’s the problem. That code depends on SetFile, and SetFile isn’t installed without Xcode…

opps… didn’t see that you had in fact mentioned that

and FlashDMG actually has this code in APP.OPEN

  //
  // Look for SETFILE [used to apply Custom_Icon]
  //
  f=Volume(0).child("developer")
  If f<>Nil Then 
    f=f.child("usr")
    If f<>Nil Then 
      f=f.child("bin")
      If f<>Nil Then 
        f=f.child("setfile")
        Custom_Icon_SetFile=f
        If Custom_Icon_SetFile<>Nil Then 
          If Not Custom_Icon_SetFile.Exists Then Custom_Icon_SetFile=Nil
        End If
      End If
    End If
  End If
  //
  If Custom_Icon_SetFile=Nil Then 
    f=f.child("usr")
    If f<>Nil Then
      f=f.child("bin")
      If f<>Nil Then
        f=f.child("setfile")
        Custom_Icon_SetFile=f
      End If
    End If
  End If
  //
  If Custom_Icon_SetFile<>Nil Then
    If Not Custom_Icon_SetFile.Exists Then Custom_Icon_SetFile=Nil
  End If