Skip to content

Instantly share code, notes, and snippets.

View stormychel's full-sized avatar
🍏

Michel Storms stormychel

🍏
View GitHub Profile
@stormychel
stormychel / create-folder.py
Created May 7, 2021 19:55 — forked from keithweaver/create-folder.py
Create a folder with Python
import os
def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print ('Error: Creating directory. ' + directory)
@stormychel
stormychel / gist:e6683e6b6c35e88cea6534c755be65d1
Created November 20, 2022 21:21
CoreData: NSManagedObject extension that prints all data to console. Usage: nsManagedObjectName.printContent()
extension NSManagedObject {
/// Print content of object to console.
func printContent() {
print("\nNSManagedObjectID: \(self.objectID) - key/value dump:")
for key in self.entity.attributesByName.keys {
print("\(key): \(self.value(forKey: key) ?? "nil")")
}
print("\n")
}
}
@stormychel
stormychel / PreviousViewController.swift
Created November 27, 2022 23:29 — forked from susieyy/PreviousViewController.swift
Access previous view controller in navigation stack
extension UIViewController {
var previousViewController: UIViewController? {
guard let navigationController = navigationController else { return nil }
let count = navigationController.viewControllers.count
return count < 2 ? nil : navigationController.viewControllers[count - 2]
}
}
@stormychel
stormychel / SwiftExtensions.swift
Created December 6, 2022 22:19
Return UIHostingController for a SwiftUI View, ready to use with UIKit.
extension AnyView {
/// Return UIHostingController for a SwiftUI View.
/// - Usage: let vc = AnyView( YourSwiftUIViewHere() ).asUIHostingController()
/// - Created by Michel Storms.
/// - Returns: UIHostingController ready to use with UIKit.
func asUIHostingController() -> UIHostingController<AnyView> {
return UIHostingController(rootView: self)
}
}
@stormychel
stormychel / gist:a372bc4ea4acf015dad21546829adf18
Last active March 5, 2023 01:01
Check whether active window is in fullscreen mode on macOS / Cocoa
/// Check whether active window is in fullscreen mode.
/// - Parameter processIdentifier: the pid given by "NSWorkspace.shared.frontmostApplication?.processIdentifier"
/// - Returns: true if fullscreen, false if windowed
func isFullScreen(processIdentifier: pid_t) -> Bool {
if let winArray: CFArray = CGWindowListCopyWindowInfo(.excludeDesktopElements, kCGNullWindowID) {
for i in 0..<CFArrayGetCount(winArray) {
if let window: [String : Any] = unsafeBitCast(CFArrayGetValueAtIndex(winArray, i), to: CFDictionary.self) as? [String : Any] {
if let pid = window["kCGWindowOwnerPID"] as? Int32 {
if pid == processIdentifier {
if let bounds: [String : Any] = window["kCGWindowBounds"] as? [String : Any], let boundsWidth = bounds["Width"] as? CGFloat, let boundsHeight = bounds["Height"] as? CGFloat {
@stormychel
stormychel / Iso369_1.swift
Created June 22, 2023 15:18 — forked from proxpero/Iso369_1.swift
A Swift 4.0 enum representing ISO 639-1 codes (used to classify languages)
// taken 2018-03-19 from wikipedia. https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
public enum Iso639_1: String {
case ab = "AB"
case aa = "AA"
case af = "AF"
case ak = "AK"
case sq = "SQ"
case am = "AM"
case ar = "AR"