13 changed files with 264 additions and 46 deletions
@ -0,0 +1,59 @@ |
|||
// |
|||
// Associations.swift |
|||
// RSwitch |
|||
// |
|||
// Created by hrbrmstr on 2/11/20. |
|||
// Copyright © 2020 Bob Rudis. All rights reserved. |
|||
// |
|||
|
|||
import Foundation |
|||
import AppKit |
|||
import ApplicationServices |
|||
|
|||
class FileAssociationUtils { |
|||
|
|||
public static func getHandlers() { |
|||
|
|||
let workspace = NSWorkspace.shared; |
|||
|
|||
let setResR : String = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, "R" as CFString, nil)?.takeRetainedValue() as String? ?? ""; |
|||
NSLog("UTI of .R extension: " + setResR); |
|||
|
|||
let handlerR : String = LSCopyDefaultRoleHandlerForContentType(setResR as CFString, LSRolesMask.all)?.takeRetainedValue() as String? ?? ""; |
|||
NSLog("Bundle ID of handler for .R files is: [" + handlerR + "]"); |
|||
|
|||
let rAppUrl : URL = (workspace.urlForApplication(withBundleIdentifier: handlerR))!.appendingPathComponent("Contents/Info.plist"); |
|||
NSLog("The Info.plist for the app that handles .R files is: " + rAppUrl.absoluteString); |
|||
|
|||
let rAppDict : NSDictionary = NSDictionary(contentsOf: rAppUrl)!; |
|||
NSLog("The name of the app that handles .R files is: " + (rAppDict.object(forKey: "CFBundleName") as! String)); |
|||
|
|||
let setResRmd : String = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, "Rmd" as CFString, nil)?.takeRetainedValue() as String? ?? ""; |
|||
NSLog("UTI of .Rmd extension: " + setResRmd); |
|||
|
|||
let handlerRmd : String = LSCopyDefaultRoleHandlerForContentType(setResRmd as CFString, LSRolesMask.all)?.takeRetainedValue() as String? ?? ""; |
|||
NSLog("Bundle ID of handler for .Rmd files is: [" + handlerRmd + "]"); |
|||
|
|||
let rmdAppUrl : URL = (workspace.urlForApplication(withBundleIdentifier: handlerRmd))!.appendingPathComponent("Contents/Info.plist"); |
|||
NSLog("The Info.plist for the app that handles .Rmd files is: " + rmdAppUrl.absoluteString); |
|||
|
|||
let rmdAppDict : NSDictionary = NSDictionary(contentsOf: rmdAppUrl)!; |
|||
NSLog("The name of the app that handles .Rmd files is: " + (rmdAppDict.object(forKey: "CFBundleName") as! String)); |
|||
|
|||
} |
|||
|
|||
public static func setHandlers() { |
|||
|
|||
let setResR : String = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, "R" as CFString, nil)?.takeRetainedValue() as String? ?? ""; |
|||
NSLog("UTI of .R extension: " + setResR); |
|||
|
|||
LSSetDefaultRoleHandlerForContentType(setResR as CFString, LSRolesMask.all, "org.rstudio.RStudio" as CFString); |
|||
|
|||
let setResRmd : String = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, "Rmd" as CFString, nil)?.takeRetainedValue() as String? ?? ""; |
|||
NSLog("UTI of .Rmd extension: " + setResRmd); |
|||
|
|||
LSSetDefaultRoleHandlerForContentType(setResRmd as CFString, LSRolesMask.all, "org.rstudio.RStudio" as CFString); |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,51 @@ |
|||
// |
|||
// Reachable.swift |
|||
// RSwitch |
|||
// |
|||
// Created by hrbrmstr on 2/11/20. |
|||
// Copyright © 2020 Bob Rudis. All rights reserved. |
|||
// |
|||
|
|||
import Foundation |
|||
import SystemConfiguration |
|||
|
|||
protocol Utilities {} |
|||
extension NSObject: Utilities { |
|||
enum ReachabilityStatus { |
|||
case notReachable |
|||
case reachableViaWWAN |
|||
case reachableViaWiFi |
|||
} |
|||
|
|||
var currentReachabilityStatus: ReachabilityStatus { |
|||
|
|||
var zeroAddress = sockaddr_in() |
|||
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size) |
|||
zeroAddress.sin_family = sa_family_t(AF_INET) |
|||
guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, { |
|||
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) { |
|||
SCNetworkReachabilityCreateWithAddress(nil, $0) |
|||
} |
|||
}) else { |
|||
return .notReachable |
|||
} |
|||
|
|||
var flags: SCNetworkReachabilityFlags = [] |
|||
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) { |
|||
return .notReachable |
|||
} |
|||
|
|||
if flags.contains(.reachable) == false { |
|||
// The target host is not reachable. |
|||
return .notReachable |
|||
} else if flags.contains(.connectionRequired) == false { |
|||
// If the target host is reachable and no connection is required then we'll assume that you're on Wi-Fi... |
|||
return .reachableViaWiFi |
|||
} else if (flags.contains(.connectionOnDemand) == true || flags.contains(.connectionOnTraffic) == true) && flags.contains(.interventionRequired) == false { |
|||
// The connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs and no [user] intervention is needed |
|||
return .reachableViaWiFi |
|||
} else { |
|||
return .notReachable |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue