macosiconsavaloniauidock

Avalonia is not showing correct app icon on MacOS


I have a problem with AvaloniaUI on MacOS, where the dock icon isn't what I have specified, it's always an exec icon like that.

exec icon

Changing the icon file doesn't help at all. I checked it and it's a MacOS problem, cause on Windows it's fine. It is possible to do it manually, but I would have to do it every time I build the app :(


Solution

  • You have 2 options, manually -> every time you make a new build, or automatically:

    It can be manually changed but you have to do it every time you build the app. To do that:

    1. Run the app
    2. Double finger click on the icon in dock -> Options -> Show in Finder
    3. Double finger click on the icon in finder and choose Get Info / click the icon and do Cmd+I
    4. Drag&Drop the correct icon file onto the icon in the Top Left Corner of the Info window (to the left of the bold file name)

    Or better way of doing that is to do it automatically: Go inside your project, go to .csproj file, which contains all necessary build info for your app after </PropertyGroup> tag , add this code:

    <Target Name="OnlyMacOSPostBuildChangeIcon" AfterTargets="PostBuildEvent">
          <Exec Command="$(ProjectDir)changeIcon.command  $(ProjectDir)" Condition=" '$(OS)' == 'Unix' Or '$(OS)' == 'Darwin' " />
          <!--<Message Importance="High" Text="The Icon of the app is changed now!" />-->
    </Target>
    

    It basically checks if the build machine is on MacOS, and runs the changeIcon.command file, which changes the icon, just after the app finishes building.

    After that, create somewhere a file changeIcon.command and put inside this code:

    #!/bin/bash
    
    GLOB_PATH="$1"
    
    ICON_PATH="$GLOB_PATH/path_to_your_icon.icns"
    TARGET_PATH="$GLOB_PATH/path_to_the_executable"
    
    cp "$ICON_PATH" /tmp/tempicon.icns
    sips -i /tmp/tempicon.icns
    DeRez -only icns /tmp/tempicon.icns > /tmp/tempicns.rsrc
    Rez -append /tmp/tempicns.rsrc -o "$TARGET_PATH"
    SetFile -a C "$TARGET_PATH"
    
    echo "Icon changed successfully!"
    

    This script basically copies the .icns icon file into temporary directory, makes sure that the icon has a proper resource fork, extracts icon data, and finally tells MacOS to use the custom icon. Remember to change the paths for files: path_to_your_icon - path to icon file with .icns extension,
    path_to_the_executable - path to the exec file without extension, (usually something like /bin/Debug/netX.X/...) The paths should be from location where the .command file is