Getting the popupbuttons' values as Var and send to a shell script as variable params

Hello everyone. I am very new to swift coding and I have a question below. Hope to find some helps here. Thank you very much!



My xcode version is 10 and I am using MacOS 10.13.6.



I am using 3 popupbuttons to select 3 Corporate App versions to write to a location with a shell script. I want the 3 App versions to be variable as can be selected from the popupbuttons menus. The 3 popupbuttons menus are all numbers from 1 to 9.



My code in the ViewController.swift as follow. How should I catch the variables selected from the 3 popupbutton menus and pass them to the shell script as variable params? Thank you.



The line " let arguments = ["/WriteAppToShare/WriteAppToShare.sh","varAppVer1","varAppVer2","varAppVer3" " will be run as /WriteAppToShare/WriteAppToShare.sh varOSVer1 varOSVer2 varOSVer3.

I am expecting it to be run as /WriteAppToShare/WriteAppToShare.sh 2 6 8 if I select 2 6 8 from the 3 corresponding popup buttons.





=====Code=========================



@IBOutlet weak var AppVer1: NSPopUpButton!


@IBOutlet weak var AppVer2: NSPopUpButton!


@IBOutlet weak var AppVer3: NSPopUpButton!



@IBAction func WritingAppNo1No2No3(_ sender: NSButton) {

let varAppVer1 = AppVer1no.selectedItem?.accessibilityAllowedValues()

let varAppVer2 = AppVer2no.selectedItem?.accessibilityAllowedValues()

let varAppVer3 = AppVer3no.selectedItem?.accessibilityAllowedValues()

let path = "/bin/bash"


// let arguments = ["/WriteAppToShare/WriteAppToShare.sh","2","6","8"]

// The above line of code works if I just write the number 2 6 8 directly. The 2 6 8 are the params to pass to the shell script. But I want these 3 values to be variables by selected them from the popupbuttons.

let arguments = ["/WriteAppToShare/WriteAppToShare.sh","varOSVer1","varOSVer2","varOSVer3"


// The above line does not work.

// It will be run and output as /WriteAppToShare/WriteAppToShare.sh varOSVer1 varOSVer2 varOSVer3.

// I am expecting it to be run and output as /WriteAppToShare/WriteAppToShare.sh 2 6 8 if I select 2 6 8 from the 3 corresponding popup buttons.





sender.isEnabled = false

let task = Process.launchedProcess(launchPath: path, arguments: arguments)

task.waitUntilExit()

sender.isEnabled = true

}






=====Code=========================

Replies

It’s hard to answer this without knowing how your popup button is configured. However, if you only need to deal with numerical values then one way to approach this would be to assign a tag to each of the items in your popup button. You can then get that value via the

tag
property, as shown below:
let v1 = appVer1.selectedItem!.tag

Some things to note:

  • I’m force unwrapping

    selectedItem
    because, for a true popup that’s not empty,
    selectedItem
    will never be
    nil
    .
  • I recommend that you follow Swift style for your identifiers, meaning that types start with an uppercase letter (

    String
    ) and everything else stars with a lowercase letter (
    selectedItem
    ). That means that
    AppVer1
    should be
    appVer1
    , as I’ve shown above.
  • Once you have the tag you can convert it to

    String
    using interpolation:
    let arguments = ["/WriteAppToShare/WriteAppToShare.sh", "\(v1)", "6", "8"]

    .

  • Your current code is running the tool synchronously (by virtue of the call to

    waitUntilExit
    ). That’s not a great idea, because the entire UI of your app will lock up while the tool is running. It’s better to run the tool asynchronously. You can use the
    terminationHandler
    property to be called when it completes.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"