//
// A s s o c i a t i o n s . s w i f t
// R S w i t c h
//
// C r e a t e d b y h r b r m s t r o n 2 / 1 1 / 2 0 .
// C o p y r i g h t © 2 0 2 0 B o b R u d i s . A l l r i g h t s r e s e r v e d .
//
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 ? ? ? " " ;
// N S L o g ( " U T I o f . R e x t e n s i o n : " + s e t R e s R ) ;
LSSetDefaultRoleHandlerForContentType ( setResR as CFString , LSRolesMask . all , " org.rstudio.RStudio " as CFString ) ;
let setResRmd : String = UTTypeCreatePreferredIdentifierForTag ( kUTTagClassFilenameExtension , " Rmd " as CFString , nil ) ? . takeRetainedValue ( ) as String ? ? ? " " ;
// N S L o g ( " U T I o f . R m d e x t e n s i o n : " + s e t R e s R m d ) ;
LSSetDefaultRoleHandlerForContentType ( setResRmd as CFString , LSRolesMask . all , " org.rstudio.RStudio " as CFString ) ;
}
}