Last active
August 29, 2015 14:11
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 documentationAppleDocs: https://developer.apple.com/library/mac/documentation/Carbon/Conceptual/customizing_docktile/docktasks_cocoa/docktasks_cocoa.html#//apple_ref/doc/uid/TP30000986-CH3-SW2
objc_msgSend(NSApp, setApplicationIconImage, ctypes.voidptr_t(0))
instead ofobjc_msgSend(NSApp, setApplicationIconImage, icon)
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:
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:drawIcon
for Firefox when download progress bar is shown on dock iconRev2