get document dir
1 |
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) |
Browse File Dialog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
@IBAction func browseFile(sender: AnyObject) { let dialog = NSOpenPanel(); dialog.title = "Choose a .txt file"; dialog.showsResizeIndicator = true; dialog.showsHiddenFiles = false; dialog.canChooseDirectories = true; dialog.canCreateDirectories = true; dialog.allowsMultipleSelection = false; // dialog.allowedFileTypes = ["txt"]; if (dialog.runModal() == NSApplication.ModalResponse.OK) { let result = dialog.url // Pathname of the file if (result != nil) { let path = result!.path let fm = FileManager.default //Bundle.main.resourcePath! do { let items = try fm.contentsOfDirectory(atPath: path) for item in items { if(item.lowercased().contains(".jpg") || item.lowercased().contains(".png")){ print("Found \(path)/\(item)") } } } catch let ex{ print(ex) } } } else { // User clicked on "Cancel" return } } |
Read Dir
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var imagesList:[String] = [] let fm = FileManager.default let path = Utility.getMediaUrlVideo() do { let items = try fm.contentsOfDirectory(atPath: path) for item in items { if(item.lowercased().contains(".jpg") || item.lowercased().contains(".png")){ imagesList.append("\(path)/\(item)") print("Found \(path)/\(item)") } } } catch let ex{ print(ex) } |
Add action to a button
1 2 3 4 5 6 |
Btn.target = self Btn.action = #selector( didTap ) @objc func didTap(sender: Any){ } |
Grab Thubnail from video
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
do { let asset = AVURLAsset(url: URL(fileURLWithPath: "/Users/shubham/Biblingo/media/videos/HebrewApplicationDataHLLUSA001-VIDEO01S_Videos12122018144440.mp4"), options: nil) let imgGenerator = AVAssetImageGenerator(asset: asset) let cgImage:CGImage = try imgGenerator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil) let destinationURL = URL(fileURLWithPath: "/Users/shubham/Biblingo/media/videos/HebrewApplicationDataHLLUSA001-VIDEO01S_Videos12122018144440.png") let nsImage = NSImage(cgImage: cgImage, size: NSSize(width: cgImage.width, height: cgImage.height)) if nsImage.pngWrite(to: destinationURL, options: .withoutOverwriting) { print("File saved") } }catch let ex { print(ex) } |
Get all supported screen resolutions on mac OSX Swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
var displayConfig: CGDisplayConfigRef = nil let mainDisplayID = CGMainDisplayID() var displayMode = CGDisplayCopyDisplayMode(mainDisplayID).takeRetainedValue() var width = CGDisplayModeGetWidth(displayMode) var height = CGDisplayModeGetHeight(displayMode) print("current size: \(width)x\(height)\n") print("available sizes:\n") var modes = CGDisplayCopyAllDisplayModes(mainDisplayID, nil).takeRetainedValue() let modesCount = CFArrayGetCount(modes) - 1 for i in 0...modesCount { var mode: CGDisplayModeRef = unsafeBitCast(CFArrayGetValueAtIndex(modes, i), CGDisplayModeRef.self) var width = CGDisplayModeGetWidth(mode) var height = CGDisplayModeGetHeight(mode) print("\t\(width)x\(height)\n") } |