Skip to content

Instantly share code, notes, and snippets.

View hechen's full-sized avatar
🎯
Focusing

CHEN hechen

🎯
Focusing
View GitHub Profile
@hechen
hechen / NSStatusItem.swift
Last active May 4, 2019 11:12
NSStatusItem
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
if let button = statusItem.button {
 button.image = NSImage(named:NSImage.Name("ic_dock"))
 button.action = #selector(doWhatYouWantToDo(_:))
}
func toggleDock2(show: Bool) -> Bool {
 return show ? NSApp.setActivationPolicy(.regular) : NSApp.setActivationPolicy(.accessory)
 }
@hechen
hechen / ActivationPolicy.swift
Created May 4, 2019 11:07
ActivationPolicy
/* The following activation policies control whether and how an application may be activated. They are determined by the Info.plist. */
 public enum ActivationPolicy : Int {
 /* The application is an ordinary app that appears in the Dock and may have a user interface. This is the default for bundled apps, unless overridden in the Info.plist. */
 case regular
/* The application does not appear in the Dock and does not have a menu bar, but it may be activated programmatically or by clicking on one of its windows. This corresponds to LSUIElement=1 in the Info.plist. */
 case accessory
 
 /* The application does not appear in the Dock and may not create windows or be activated. This corresponds to LSBackgroundOnly=1 in the Info.plist. This is also the default for unbundled executables that do not have Info.plists. */
 case prohibited
 }
@hechen
hechen / setActivationPolicy.swift
Created May 4, 2019 11:07
setActivationPolicy
/* Attempts to modify the application's activation policy. In OS X 10.9, any policy may be set; prior to 10.9, the activation policy may be changed to NSApplicationActivationPolicyProhibited or NSApplicationActivationPolicyRegular, but may not be changed to NSApplicationActivationPolicyAccessory. This returns YES if setting the activation policy is successful, and NO if not.
 */
 @available(OSX 10.6, *)
 open func setActivationPolicy(_ activationPolicy: NSApplication.ActivationPolicy) -> Bool
<key>LSUIElement</key>
<true/>
@hechen
hechen / toggleDock.swift
Created May 4, 2019 11:02
toggle cocoa App dockless on or off
func toggleDock(show: Bool) -> Bool {
// ProcessApplicationTransformState
  let transformState = show ? 
  // show to foreground application 
  // or not show to background application
  ProcessApplicationTransformState(kProcessTransformToForegroundApplication) 
 : ProcessApplicationTransformState(kProcessTransformToUIElementApplication)
// transform current application type.
 var psn = ProcessSerialNumber(highLongOfPSN: 0, lowLongOfPSN: UInt32(kCurrentProcess))
 return TransformProcessType(&psn, transformState) == 0
@hechen
hechen / TransformProcessType.swift
Last active May 4, 2019 11:27
TransformProcessType
/*
 * TransformProcessType()
 * 
 * Summary:
 * Changes the 'type' of the process specified in the psn parameter.
 * The type is specified in the transformState parameter.
 * 
 * Discussion:
 * Given a psn for an application, this call transforms that
 * application into the given type. Foreground applications have a
@hechen
hechen / NSProgressIndicator+Rx+.swift
Created March 18, 2019 07:34
Bindable sink for `loading` and property.
#if os(macOS)
import Cocoa
import RxSwift
import RxCocoa
extension Reactive where Base: NSProgressIndicator {
/// Bindable sink for `loading` and property.
public var loading: Binder<Bool> {
@hechen
hechen / NSProgressIndicator+Loading.swift
Created March 18, 2019 07:33
NSProgressIndicator loading extension
#if os(macOS)
import Cocoa
/// Add loading property for NSProgressIndicator
extension NSProgressIndicator {
struct Holder {
static var _loading: Bool = false
static var _hideWhenAnimationStop: Bool = false
}
<# button #> = ({
UIButton *b = [[UIButton alloc] init];
<# ... #>
b;
});