Skip to content

Instantly share code, notes, and snippets.

View usagimaru's full-sized avatar
🐰

usagimaru usagimaru

🐰
View GitHub Profile
@usagimaru
usagimaru / HiddenMacOSDebuggingPanel.md
Last active May 8, 2024 01:19
Enables useful debugging panel in macOS apps

Use _NS_4445425547 or NS🐞 for enables debuggging panel. When enabled it, a ladybug 🐞 menu appears in the app menu bar.

“4445425547” means DEBUG in Unicode table.

0x44=D
0x45=E
0x42=B
0x55=U
0x47=G

@usagimaru
usagimaru / method_copyReturnType.md
Last active April 26, 2024 18:26
About Return Types of `method_copyReturnType()` of the Objective-C Runtime API

I found some documentation of the types returned by method_copyReturnType() in the Objective-C runtime API. These mysterious characters are called Type Encodings in Objective-C and are unique codes represented by one or fewer characters.

I note it here for my own benefit.

  • c = char
  • B = BOOL / bool / _bool
  • s = short
  • i = int
  • I = unsigned int?
  • l = long
@usagimaru
usagimaru / NSTextField+FirstResponderCheck.swift
Last active April 22, 2024 17:33
An extension to check if NSTextField / NSSearchField is the first responder
import Cocoa
extension NSView {
var isFirstResponder: Bool {
window?.firstResponder == self
}
}
@usagimaru
usagimaru / NSImageDimming.swift
Last active April 16, 2024 19:15
Dim NSImage
import Cocoa
extension NSImage {
/// Return dimmed NSImage with level (0.0–1.0)
func dimmed(_ level: CGFloat) -> NSImage {
NSImage(size: self.size, flipped: false) { rect in
let imageRect = NSRect(.zero, self.size)
// Draw image
@usagimaru
usagimaru / amazonurl.sh
Created June 6, 2022 07:17
Simplify Amazon.co.jp URL
#!/bin/sh
echo "$1" | \
sed -E 's/(https:\/\/www\.amazon\.co\.jp)\/.*\/([0-9A-Z]{10})[\/\?]?.*/\1\/dp\/\2/'
# % amazonurl.sh "https://www.amazon.co.jp/構造化ユーザインタフェースの設計と評価/dp/4320029968"
# % https://www.amazon.co.jp/dp/4320029968
@usagimaru
usagimaru / DumpAllMethods.swift
Last active March 28, 2024 19:52
Get all methods of an class or instance in Swift
// Dump all NSApplication’s class methods
let dump = NSApplication.perform(NSSelectorFromString("fp_methodDescription")).takeUnretainedValue() as? String
// Dump all NSApplication’s instance methods
let dump = NSApp.perform(NSSelectorFromString("fp_methodDescription")).takeUnretainedValue() as? String
// or
print(NSApplication.value(forKey: "fp_methodDescription"))
print(NSApp.value(forKey: "fp_methodDescription"))
@usagimaru
usagimaru / ShowAllViewFrames.md
Created March 25, 2024 17:13
Shows all view frames in macOS app

"Arguments Passsed on Launch" on Xcode: -NSShowAllViews YES

@usagimaru
usagimaru / DividerOfSplitView.swift
Last active March 5, 2024 19:15
Get NSSplitView’s divider view from a subclass (customize divider height and color)
import Cocoa
class SplitView: NSSplitView {
override var dividerThickness: CGFloat {
50
}
override var dividerColor: NSColor {
NSColor.red
@usagimaru
usagimaru / IndexPath.swift
Last active March 1, 2024 04:58
Initialize IndexPath with description in Swift
import Foundation
extension IndexPath {
enum IndexPathFromStringError: Error {
case couldNotInitialize(String)
}
/// Initialize with description. e.g. "[2, 3, 12, 0, 4]"
init(from description: String) throws {
@usagimaru
usagimaru / NSWindowDelegate.md
Last active February 26, 2024 23:03
NSWindowController.showWindow(_:)でNSWindowDelegateを再設定しなければならない

NSWindowController.showWindow(_:)でNSWindowDelegateを再設定しなければならない

AppKitのNSWindowControllerをNSWindowDelegateにする場合に、なぜだかデリゲートメソッド一式がコールされない現象に悩まされた。調べても原因がよくわからず、NSWindow.delegateの中身を確認しても正しくNSWindowControllerインスタンスが設定されているので、一体どこで通知不能になっているのかが掴めなかった。

一応解決できた方法としては、NSWindowController.showWindow(_:)をオーバーライドし、superのコール前にデリゲートを再設定してあげることでこのコールされない現象を解消できた。

// MyWindowController.swift

override func showWindow(_ sender: Any?) {