How to check file under Library in macOS

Issue #200 let home = NSSearchPathForDirectoriesInDomains(.applicationScriptsDirectory, .userDomainMask, true).first! let path = home.appending(".XcodeWayExtensions/XcodeWayScript.scpt") let exists = FileManager.default.fileExists(atPath: path)

April 10, 2019 · 1 min · 17 words · Khoa

How to run ffmpeg in macOS app

Issue #178 Install ffmpeg, which installs ffprobe brew install ffmpeg Find location of installed ffmpeg which ffmpeg Add all executables to project Get error unable to obtain file audio codec with ffprobe Run in verbose mode ffmpeg -v Get [debug] Encodings: locale US-ASCII, fs utf-8, out None, pref US-ASCII [debug] Python version 2.7.10 (CPython) - Darwin-18.2.0-x86_64-i386-64bit [debug] exe versions: ffmpeg present, ffprobe present Run version of ffprobe ffprobe --version Get dylb not found...

March 19, 2019 · 1 min · 85 words · Khoa

How to get path to resource in running macOS app

Issue #177 This is useful to refer to another executable in a running executable in Process Bundle.main.path(forResource: "ffmpeg", ofType: "")!

March 19, 2019 · 1 min · 20 words · Khoa

How to run executable in macOS

Issue #176 Enable executable chmod +x executable Add executable file to target Use Process with correct launchPad import Foundation protocol TaskDelegate: class { func task(task: Task, didOutput string: String) func taskDidComplete(task: Task) } class Task { weak var delegate: TaskDelegate? let process = Process() func run(arguments: [String]) { DispatchQueue.background.async { let launchPath = Bundle.main.path(forResource: "executable", ofType: "")! self.run(launchPath: launchPath, arguments: arguments) } } func stop() { DispatchQueue.background.async { if self.process.isRunning { self....

March 19, 2019 · 1 min · 200 words · Khoa

How to print current directory using Process in macOS

Issue #175 let process = Process() process.launchPath = "/bin/pwd" process.arguments = [] Should be the same as FileManager.default.currentDirectoryPath

March 19, 2019 · 1 min · 18 words · Khoa

How to change NSTextField backgroundColor in NSPopover

Issue #174 Disable vibrancy mode of NSPopover let popover = NSPopover() popover.appearance = NSAppearance(named: NSAppearance.Name.aqua)

March 19, 2019 · 1 min · 15 words · Khoa

How to make scrollable vertical NSStackView

Issue #173 You might need to flip NSClipView import AppKit import Anchors import Omnia final class ScrollableStackView: NSView { final class FlippedClipView: NSClipView { override var isFlipped: Bool { return true } } override init(frame frameRect: NSRect) { super.init(frame: frameRect) setup() } required init?(coder decoder: NSCoder) { fatalError() } let stackView: NSStackView = withObject(NSStackView()) { $0.orientation = .vertical } private let scrollView: NSScrollView = NSScrollView() private func setup() { addSubview(scrollView) scrollView....

March 18, 2019 · 1 min · 109 words · Khoa

How to load top level view from xib in macOS

Issue #171 var views: NSArray? NSNib(nibNamed: NSNib.Name("ProfileView"), bundle: nil)?.instantiate(withOwner: nil, topLevelObjects: &views) let profileView = views!.compactMap({ $0 as? ProfileView }).first!

March 18, 2019 · 1 min · 20 words · Khoa

How to generate QR code in AppKit

Issue #140 I need to generate QR code in https://github.com/onmyway133/AddressGenerator. Fortunately with CoreImage filter, it is very easy. Code is in Swift 4 import AppKit final class QRCodeGenerator { func generate(string: String, size: CGSize) -> NSImage? { guard let data = string.data(using: .utf8) else { return nil } // Filter guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return nil } filter.setValue(data, forKey: "inputMessage") filter.setValue("Q", forKey: "inputCorrectionLevel") // CIImage guard let ciImage = filter....

January 6, 2018 · 1 min · 113 words · Khoa

Learning from Open Source Using dylib in macOS applications

Issue #134 https://github.com/mortenjust/androidtool-mac https://github.com/mortenjust/droptogif

January 3, 2018 · 1 min · 4 words · Khoa

How to make NSCollectionView programatically in Swift

Issue #131 Here’s how to create NSCollectionView programatically. We need to embed it inside NScrollView for scrolling to work. Code is in Swift 4 NSCollectionView let layout = NSCollectionViewFlowLayout() layout.minimumLineSpacing = 4 collectionView = NSCollectionView() collectionView.dataSource = self collectionView.delegate = self collectionView.collectionViewLayout = layout collectionView.allowsMultipleSelection = false collectionView.backgroundColors = [.clear] collectionView.isSelectable = true collectionView.register( Cell.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell") ) NScrollView scrollView = NSScrollView() scrollView.documentView = collectionView view.addSubview(scrollView) NSCollectionViewItem final class Cell: NSCollectionViewItem { let label = Label() let myImageView = NSImageView() override func loadView() { self....

December 21, 2017 · 2 min · 224 words · Khoa

Learning from Open Source Using Playground

Issue #94 One thing I like about kickstarter-ios is how they use Playground to quickly protoyping views. We use Swift Playgrounds for iterative development and styling. Most major screens in the app get a corresponding playground where we can see a wide variety of devices, languages and data in real time. This way we don’t need Injection or using React Native anymore. Take a look at all the pages https://github.com/kickstarter/ios-oss/tree/master/Kickstarter-iOS.playground/Pages...

October 23, 2017 · 1 min · 87 words · Khoa

Learning from Open Source Making macOS app in code

Issue #91 I’m familiar with the whole app structure that Xcode gives me when I’m creating new macOS project, together with Storyboard. The other day I was reading touch-bar-simulator and see how it declares app using only code. See this main.swift let app = NSApplication.shared let delegate = AppDelegate() app.delegate = delegate app.run() final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate { let controller = IDETouchBarSimulatorHostWindowController.simulatorHostWindowController()! lazy var window: NSWindow = self.controller.window! }

October 19, 2017 · 1 min · 71 words · Khoa

Fixing login hanging in macOS High Sierra

Issue #86 Today I met a strange problem. After I enter my password, the progress bar runs to the end, and it is stuck there forever. No matter how many times I try to restart. I finally need to go to Recovery mode by pressing Cmd+R at start up. I then select Get Help Online to open Safari. Strangely enough I wasn’t connected to Internet After select the wifi icon on the status bar to connect internet, I then restart and can login again....

October 16, 2017 · 1 min · 97 words · Khoa

NSApplicationDelegate and notification

Issue #34 In an iOS project, we often see this in AppDelegate @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { return true } } But in a Cocoa project, we see this instead @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(aNotification: NSNotification) { // Insert code here to initialize your application } func applicationWillTerminate(aNotification: NSNotification) { // Insert code here to tear down your application } } In this case the param is of type NSNotification...

May 10, 2017 · 1 min · 172 words · Khoa

How to debug Auto Layout

Issue #23 hasAmbiguousLayout Returns whether the constraints impacting the layout of the view incompletely specify the location of the view. exerciseAmbiguityInLayout This method randomly changes the frame of a view with an ambiguous layout between its different valid values, causing the view to move in the interface. This makes it easy to visually identify what the valid frames are and may enable the developer to discern what constraints need to be added to the layout to fully specify a location for the view....

May 10, 2017 · 1 min · 136 words · Khoa

Markdown editor

Issue #6 I like writing with markdown, it gives me comfortable experience with complete control over what I want to write. I recommend vmd which renders exactly as GitHub. vmd is for rendering only, you need an editor to write, I use Sublime Text because it opens very fast I also recommend using spectacle to easily split and organize windows

April 30, 2017 · 1 min · 60 words · Khoa