admin管理员组

文章数量:1391964

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.

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 :(

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.

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 :(

Share Improve this question asked Mar 13 at 15:28 OstryJROstryJR 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

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)changeIconmand  $(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 changeIconmand file, which changes the icon, just after the app finishes building.

After that, create somewhere a file changeIconmand 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 mand file is

本文标签: Avalonia is not showing correct app icon on MacOSStack Overflow