11

Installing a Mac OS X app using cmake environment, I want to set and install the icon in the process of installation.

Therefore, I try to set

set( MACOSX_BUNDLE_ICON_FILE ${CMAKE_CURRENT_SOURCE_DIR}/images/myAopImage.icns )
ADD_EXECUTABLE(MYAPP MACOSX_BUNDLE ${MACOSX_BUNDLE_ICON_FILE} ${allSources})
#(I could set it in the source directory to any other path)

However, this does not work, it writes the complete path into the Info.plist, and installs nothing into the Resources directory (the resources dir even does not get created in the app folder which gets installed)

Anyhow, even if I put by hand into the Info.plist the information

<key>CFBundleIconFile</key>
<string>myAppImage.icns</string>

and create the Resources directory by hand, putting into there the icns File,

the icon appears anyhow for the app.

How do I solve this problem? At first to do it by hand maybe, but also within the cmake context?

pkamb
  • 33,281
  • 23
  • 160
  • 191
user3478292
  • 113
  • 1
  • 5

2 Answers2

20

There are two pieces to bundling the .icns file from CMake: adding the source file itself, and configuring it in the .plist. If you want to specify a path to your .icns file (e.g. because it is in a subdirectory), then these two properties will refer to the .icns file slightly differently:

# NOTE: Don't include the path in MACOSX_BUNDLE_ICON_FILE -- this is
# the property added to Info.plist
set(MACOSX_BUNDLE_ICON_FILE myAppImage.icns)

# And this part tells CMake where to find and install the file itself
set(myApp_ICON ${CMAKE_CURRENT_SOURCE_DIR}/images/myAppImage.icns)
set_source_files_properties(${myApp_ICON} PROPERTIES
       MACOSX_PACKAGE_LOCATION "Resources")

add_executable(myApp MACOSX_BUNDLE ${myApp_ICON} ${allSources})
Zrax
  • 1,641
  • 10
  • 15
  • 1
    I found the second answer better than the first, because second answer addresses the creation of Resources. – Charles Thomas Apr 18 '17 at 18:29
  • Agreed this should be the accepted answer. No need to manually edit the plist file! – melMass Apr 09 '21 at 02:27
  • Would this result in the build file myTestAPp.app having icon set to that icon ? Or just icon set on app when app starts in task bar? I'm failing to get the .app bundle to have it as icon :/ – Dariusz Jun 18 '22 at 09:35
6

To copy the icns file to your Resources folder, you need to set a source property on it.

set_source_files_properties(myAppImage.icns PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")

Also, I read somewhere that the string in the plist does not require the .icns, so

<string>myAppImage</string> 

should be fine.

Mark
  • 7,167
  • 4
  • 44
  • 68
  • thanks once again, my answer to your answer appeared before yours, so maybe you will not read it..... cf above please – user3478292 Apr 02 '14 at 09:54
  • As to your response, can you elaborate on what you mean by the location of the icon? – Mark Apr 02 '14 at 19:45
  • now it should be deleted and just a comment, hope now everything is fine, I am new in this forum.... – user3478292 Apr 04 '14 at 14:28
  • with respect to localization, I mean the directory where I put it in the context of the source code - if I write e.g. in something like set_source_file_properties( ${CMAKE_SOURCE_DIR}/icons/myicon.icns ....) , then cmake will write the complete absolute path into the plist.info file, however if I put the icon directly into the cmake-source-dir, and I write in just set_source_file_properties(myicon.icns....), then just this gets written in and it gets installed correctly.... however this does not allow me to put the icon in an extra directory within the cmake source code.... – user3478292 Apr 04 '14 at 14:31
  • You can specify relative paths from the directory that your cmakelists.txt is in. In your case you could use ./icons/myicon.icns to include that file. Also, if my answer solved your problem, you can accept the answer to mark my answer as the correct one. – Mark Apr 04 '14 at 19:34