Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noitidart/6e39b7a0b2c3419a1165 to your computer and use it in GitHub Desktop.
Save Noitidart/6e39b7a0b2c3419a1165 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-ObjC_SetApplicationIconImage - Change the icon in the dock. (Mac OS X) (js-ctypes)
"use strict";
let { utils: Cu } = Components;
let { ctypes } = Cu.import("resource://gre/modules/ctypes.jsm", {});
let { OS } = Cu.import("resource://gre/modules/osfile.jsm", {});
/* **** please replace following string before run **** */
let IMAGE_PATH = "absolute path to image file";
OS.File.read(IMAGE_PATH).then(function(iconData) {
// NOTE: iconData is Uint8Array
let length = ctypes.unsigned_long(iconData.length);
let bytes = ctypes.uint8_t.array()(iconData);
let objc = ctypes.open(ctypes.libraryName("objc"));
let id = ctypes.voidptr_t;
let SEL = ctypes.voidptr_t;
let objc_getClass = objc.declare("objc_getClass",
ctypes.default_abi,
id,
ctypes.char.ptr);
let sel_registerName = objc.declare("sel_registerName",
ctypes.default_abi,
SEL,
ctypes.char.ptr);
let objc_msgSend = objc.declare("objc_msgSend",
ctypes.default_abi,
id,
id,
SEL,
"...");
// NSApp = [NSApplication sharedApplication];
let NSApplication = objc_getClass("NSApplication");
let sharedApplication = sel_registerName("sharedApplication");
let NSApp = objc_msgSend(NSApplication, sharedApplication);
// data = [NSData dataWithBytes: bytes length: length];
let NSData = objc_getClass("NSData");
let dataWithBytes_length = sel_registerName("dataWithBytes:length:");
let data = objc_msgSend(NSData, dataWithBytes_length, bytes, length);
// icon = [[NSImage alloc] initWithData: data];
let NSImage = objc_getClass("NSImage");
let initWithData = sel_registerName("initWithData:");
let alloc = sel_registerName("alloc");
let icon = objc_msgSend(objc_msgSend(NSImage, alloc), initWithData, data);
if (icon.isNull()) {
throw new Error("Image file is corrupted.");
}
// [NSApp setApplicationIconImage: icon]
let setApplicationIconImage = sel_registerName("setApplicationIconImage:");
objc_msgSend(NSApp, setApplicationIconImage, icon);
// [icon release]
let release = sel_registerName("release");
objc_msgSend(icon, release);
objc.close();
}, function(e) {
console.log("Failed to read from file:", e);
}).catch(function(e) {
console.log(e);
});
@Noitidart
Copy link
Author

README

See Also GitHubGIST :: Noitidart / _ff-addon-snippet-ObjC_SwizzleWithSetImplementation.js
See Also GitHubGIST :: Noitidart / _ff-addon-snippet-ObjC_SwizzleWithExchangeImplementation.js

Rev1

  • Code from @arai-a (so cooool!) from here: http://www.unmht.org/forum/en/4774.html

  • Works! :)

  • To reset applied image send with nil per documentation

  • Image path can be to png or icns, it shows in alt + tab menu, only place the applied image doesn't show is if you minimize a window see red arrow in image, red arrow pointing to where icon is not showing, this image shows the icon is set in alt+tab menu and dock which is awesome:
    red arrow pointing to where icon is not showing, this image shows the icon is set in alt+tab menu and dock which is awesome

  • Also the image in the prompt dialogs is not changed by setApplicationIconImage, this is fixed though by swizzling [[NSImage imageNamed] @'NSApplication'], here is image of icon in dialog:

    • This swizzle also fixes the drawIcon for Firefox when download progress bar is shown on dock icon

Rev2

  • Added "ObjC" to file/gist name and description

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment