I am trying to make a copy of the AddressBook of my Mac to a save place before archiving it. Here is my code :
// whoami
let whoam : NSTask = NSTask()
whoam.launchPath = "/usr/bin/whoami"
// pipe whoami| NSString
let UltimPipe : NSPipe = NSPipe()
let UltimPipeLisible : NSFileHandle = UltimPipe.fileHandleForReading
whoam.standardOutput = UltimPipe
// lancement
whoam.launch()
// lecture de la sortie
let DonSortie : NSData = UltimPipeLisible.readDataToEndOfFile()
let Sortie : String? = String(data: DonSortie, encoding:
NSUTF8StringEncoding)
if Sortie == nil
{
print("Résultat de sortie non-valide")
let MonAlerte:NSAlert = NSAlert()
MonAlerte.messageText = "Aucun élement trouvé."
MonAlerte.informativeText = ""
MonAlerte.runModal()
exit(1)
}
let ligne : String = Sortie!
print (ligne)
let source = "/Users/"+ligne+"/Library/Application Support/AddressBook"
let destin = "/Users/"+ligne+"/Desktop/test/"
//cp
let duTache : NSTask = NSTask()
duTache.launchPath = "/bin/cp"
duTache.arguments = [ "-r", source, " ", destin ]
print (duTache.arguments)
let ifcPipe : NSPipe = NSPipe()
duTache.standardOutput = ifcPipe
// lancement
duTache.launch()
The returned error is :
usage: cp [-R [-H |...]
It look like I have a syntax problem, and, when I do a print of the arguments of duTache (print (duTache.arguments)), the answer is :
Optional(["-r", "/Users/username\n/Library/Application Support/AddressBook", " ", "/Users/username\n/Desktop/test/"])
Maybe if I remove the "\n" present after the username the operation could be completed? Does anyone knows how to do that?
Thank you for reading my post.