0

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.

Fredo
  • 153
  • 2
  • 13

2 Answers2

0
var ligne = "username\n"
print(ligne,0)
/*
username
0
*/
let newLine = ligne.removeAtIndex(ligne.endIndex.predecessor()) // newLine is "\n"
print(ligne,0)
/*
username 0
*/
user3441734
  • 16,722
  • 2
  • 40
  • 59
  • I'll try this. I'll let you know. Thank U. – Fredo Nov 06 '15 at 17:44
  • removeAtIndex is working but the result is made of type Character and I need the type String. Downcast is refused (always failed). Any idea how to convert it to String? – Fredo Nov 06 '15 at 19:23
  • you don't have to use newLine (in newLine is just \n). ligne is now without \n and has String type .... so use it like before. – user3441734 Nov 06 '15 at 19:38
  • The result is now like this : – Fredo Nov 06 '15 at 20:17
  • var ligne : String = Sortie! print (ligne) let line = ligne.removeAtIndex(ligne.endIndex.predecessor()) 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) – Fredo Nov 06 '15 at 20:17
  • Thanks a lot for your help. Long live StackOverflow! – Fredo Nov 06 '15 at 20:18
  • Due to the new syntax, there is a little difference (to avoid warning, the line must be changed to : _ = ligne.removeAtIndex(ligne.endIndex.predecessor()) – Fredo Nov 06 '15 at 20:22
  • _ = ligne.removeAtIndex(ligne.endIndex.predecessor()) – Fredo Nov 06 '15 at 20:22
  • ligne.removeAtIndex(ligne.endIndex.predecessor()) is enough! I just gave you 'more complete' example. the removeAtIndex return what did you remove from string. – user3441734 Nov 06 '15 at 21:16
0
import Foundation

func execute(command: String, args: [String]) -> String {

    let task = NSTask()

    task.launchPath = command
    task.arguments = args

    let pipe = NSPipe()
    task.standardOutput = pipe
    task.launch()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String? = String(data: data, encoding: NSUTF8StringEncoding)

    task.waitUntilExit()

    return output ?? ""

}


print(execute("/bin/ls", args: ["-alsp"]))
print(execute("/bin/ls", args: []))


execute("/usr/bin/say", args: ["Hello World!"])

if you find how to process exceptions ( try some stupid command like /bin/lss ), let me know! I need to check the documentation first ...

user3441734
  • 16,722
  • 2
  • 40
  • 59
  • Is the exemple above a better way to use NSTask? – Fredo Nov 07 '15 at 13:23
  • I 'll need some time to implement it but in a Playground it is doing the job. It might also solve the error I get when using "cp" (cp: : No such file or directory)? Thank you. – Fredo Nov 07 '15 at 13:24
  • I made some test inside a playground but commands are not working properly (cp, or even ls which sometimes work, sometimes don't (depending on the path chosen)). Inside a project, all the command are working but cp is reporting : " cp: : No such file or directory" (although it is doing the copy job!). Can I ignore this error? – Fredo Nov 07 '15 at 15:29