Skip to content

Instantly share code, notes, and snippets.

@delasign
delasign / debugBundleResources.swift
Created January 1, 2025 16:55
Sample code for debugging bundle resources
if let resourcePath = BUNDLE_NAME.resourcePath {
let resources = try? FileManager.default.contentsOfDirectory(atPath: resourcePath)
print("Resource path: \(resourcePath)")
print("Resources in bundle: \(resources ?? [])")
} else {
print("Bundle.module.resourcePath is nil")
}
@delasign
delasign / useRealityKitContentInSceneKit.swift
Created January 1, 2025 16:40
Sample code for using RealityKit Content in SceneKit
guard let realityKitFile = BUNDLE_NAME.url(forResource: "FILE_NAME", withExtension: "usdc") else {
fatalError("File doesnt exist")
}
do {
let scene = try SCNScene(url: realityKitFile)
} catch {
// Process Error
}
@delasign
delasign / getAppViewFromAccessibilityIdentifier.swift
Created November 9, 2024 16:19
Sample code for finding a view within an app that matches an accessibility identifier
import XCTest
func getAppView(identifier: String) -> XCUIElement {
return XCUIApplication().descendants(matching: .any).matching(identifier: identifier).element
}
@delasign
delasign / openFolderInOpenPanel.swift
Created November 9, 2024 14:39
Code for automating the opening of a folder in a UI Swift Test
import XCTest
func openFolderInOpenPanel(folderName: String) {
let openPanel = XCUIApplication().dialogs["open-panel"]
XCTAssertTrue(openPanel.exists, "Open Panel is visible.")
XCTAssertTrue(openPanel.isHittable, "Open Panel is hittable.")
let openPanelOpenButton = openPanel.buttons["OKButton"]
XCTAssertTrue(openPanelOpenButton.exists, "Open Panel Open Button is visible.")
XCTAssertTrue(openPanelOpenButton.isHittable, "Open Panel Open Button is hittable.")
@delasign
delasign / AutomatedNSOpenPanelTest.swift
Created November 9, 2024 14:22
Code for automating search in an NSOpenPanel.
let openPanel = XCUIApplication().dialogs["open-panel"]
XCTAssertTrue(openPanel.exists, "Open Panel is visible.")
XCTAssertTrue(openPanel.isHittable, "Open Panel is hittable.")
let openPanelSearchBox = openPanel.searchFields.firstMatch
XCTAssertTrue(openPanelSearchBox.exists, "Open Panel Search Box is visible.")
XCTAssertTrue(openPanelSearchBox.isHittable, "Open Panel Search Box hittable.")
openPanelSearchBox.tap()
openPanelSearchBox.typeText("SEARCH_TEXT")
@delasign
delasign / updatedGatherLanguageContent.swift
Created November 8, 2024 22:08
Updated gatherLanguageContent for our language coordinator to work with Swift Testing
private func gatherLanguageContent(langugeCode: String) -> UIContent? {
debugPrint("\(LanguageCoordinator.identifier) gatherLanguageContent \(DebuggingIdentifiers.actionOrEventInProgress) Generating Content for : \(langugeCode)")
let path: String?
if isTestEnvironment {
path = Bundle(for: type(of: self)).path(forResource: languageCode, ofType: "json")
} else {
path = Bundle.main.path(forResource: langugeCode, ofType: "json")
}
@delasign
delasign / getBookmarkedURL.swift
Created October 25, 2024 18:22
Sample function for converting data into a bookmark with security scope.
import Foundation
func getBookmarkedURL(data: Data) -> URL? {
var isStale = false
do {
let url = try URL(resolvingBookmarkData: data, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
debugPrint("\(DebuggingIdentifiers.actionOrEventInProgress) getBookmarkedURL is Stale ? : \(isStale) | url: \(url).")
guard !isStale, url.startAccessingSecurityScopedResource() else {
debugPrint("\(DebuggingIdentifiers.actionOrEventFailed) getBookmarkedURL - bookmark has expired. Request access again to proceed.")
@delasign
delasign / createSecurityScopedBookmark.swift
Created October 25, 2024 18:11
Sample code for creating a bookmark with a security scope in Swift
import Foundation
func createSecurityScopedBookmark(for url: URL) -> Data? {
do {
// Create a bookmark from the selected URL
let bookmarkData = try url.bookmarkData(options: .securityScopeAllowOnlyReadAccess, includingResourceValuesForKeys: nil, relativeTo: nil)
debugPrint("\(DebuggingIdentifiers.actionOrEventSucceded) createSecurityScopedBookmark - \(bookmarkData)")
return bookmarkData
} catch {
debugPrint("\(DebuggingIdentifiers.actionOrEventFailed) createSecurityScopedBookmark error \(error)")
@delasign
delasign / getFileOrFolderURL.swift
Created October 25, 2024 17:49
Sample get file or folder url Swift function
import Foundation
@MainActor
private func getFolderURL(title: String, message: String, canChooseFiles: Bool, canChooseDirectories: Bool, allowsMultipleSelection: Bool) async -> URL? {
let panel = NSOpenPanel()
panel.title = title
panel.message = message
panel.canChooseFiles = canChooseFiles
panel.canChooseDirectories = canChooseDirectories
panel.allowsMultipleSelection = allowsMultipleSelection
@delasign
delasign / deleteFileAtLocation.swift
Created October 24, 2024 19:23
Sample Delete MacOS File functionality
import Foundation
func deleteFileAtLocation(url: URL) {
do {
try FileManager.default.removeItem(at: url)
debugPrint("\(DebuggingIdentifiers.actionOrEventSucceded) deleteFileAtLocation : \(url).")
} catch {
debugPrint("\(DebuggingIdentifiers.actionOrEventFailed) deleteFileAtLocation : \(error)")
}
}