Skip to content

Instantly share code, notes, and snippets.

@delasign
delasign / createTemporaryFile.swift
Created October 24, 2024 19:02
Create Temporary MacOS File Swift Function
import Foundation
// Create a temporary file URL
func createTemporaryFile(fileTitle: String, fileExtension: String) throws -> URL {
// Create the file and ensure it exists at the location
let tempDirectory = try FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: .desktopDirectory, create: true)
let tempFileURL = tempDirectory.appendingPathComponent(fileTitle).appendingPathExtension(fileExtension)
@delasign
delasign / sampleContextMenu.swift
Created October 23, 2024 15:08
Sample SwiftUI Context Menu Modifier.
.contextMenu(menuItems: {
Button("Delete", action: {
// TO DO: Create Delete Action
})
.keyboardShortcut(.delete, modifiers: .command)
})
@delasign
delasign / addCommandsToASwiftUIApp.swift
Created October 23, 2024 13:37
Sample commands modifier for adding commands to a SwiftUI MacOS app
.commands {
CommandGroup(after: CommandGroupPlacement.newItem) {
Button("New Command + S Command") {
// TO DO: Add Action
}
.keyboardShortcut("S", modifiers: [.command])
}
}
@delasign
delasign / replaceOrRemoveNewWindowCommand.swift
Created October 23, 2024 13:29
Sample commands modifier for replacing or removing the new window command in a SwiftUI MacOS app
.commands {
CommandGroup(replacing: CommandGroupPlacement.newItem) {
// Leave this blank to remove the new window functionality
// Or add the element below to replace the New Window command.
Button("New Command + N Command") {
objectCaptureCoordinator.showModal = true
}
.keyboardShortcut("N", modifiers: [.command])
}
}
@delasign
delasign / focusStateSample.swift
Created October 20, 2024 15:22
Sample SwiftUI Focus State Property Wrapper and Use
import SwiftUI
@main
struct Application: App {
@FocusState var focused: Bool?
// App
var body: some Scene {
WindowGroup {
View()
@delasign
delasign / keyPressSample.swift
Created October 20, 2024 14:34
Sample SwiftUI KeyPress Modifiers
.focusable()
.onKeyPress(action: { key in
debugPrint("key: \(key.characters)")
return .handled
})
.onKeyPress(.upArrow, action: {
debugPrint("Up Arrow Key")
return .handled
})
.onKeyPress(.leftArrow, action: {
@delasign
delasign / SwiftDataSingletonSwiftUI.swift
Created October 18, 2024 19:30
Sample SwiftData Singleton Initializer
//
// _SINGLETON_.swift
//
// Created by Oscar de la Hera Gomez on 8/28/24.
//
import Foundation
import SwiftUI
import SwiftData
@delasign
delasign / AppWithScenePhases.swift
Created October 18, 2024 18:08
Sample SwiftUI App code that listens to changes in scene phases.
//
// App.swift
// SwiftUI Starter Project
//
// Created by Oscar de la Hera Gomez on 8/14/24.
//
import SwiftUI
// Please note that "Application" cannot be named App.
@delasign
delasign / AppDelegate-SwiftUI.swift
Created October 18, 2024 17:31
Sample SwiftUI App Delegate
//
// AppDelegate.swift
//
// Created by Oscar de la Hera Gomez on 10/18/24.
//
import AppKit
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
@delasign
delasign / macOS-willTerminateNotification.swift
Created October 18, 2024 16:42
willTerminateNotification Reciever for MacOS SwiftUI apps.
.onReceive(NotificationCenter.default.publisher(for: NSApplication.willTerminateNotification)) { _ in
// tear down
}