1

I have installed chrome-cli in my Mac. I'm trying to execute chrome-cli commands from Swift with following code:

    import Foundation

func shell(_ command: String) -> String {
    let task = Process()
    let pipe = Pipe()

    task.standardOutput = pipe
    task.arguments = ["-c", command]
    task.launchPath = "/bin/bash"
    task.launch()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = String(data: data, encoding: .utf8)!

    return output
}

// Example 1: 
let example1 = shell("/usr/local/bin/chrome-cli --help") //returns expected result
let example2 = shell("/usr/local/bin/chrome-cli list tabs") // return ""

Here, example 1 is calling with single argument and works well, i.e it print same result like we execute the command in Terminal.

but for example 2, we have more than 1 argument and it always returns empty result. While the same command on Terminal print list of tab currently opened.

I think the process doesn't wait for command to return the result as it takes 1-2 seconds to list tabs in Terminal app. or its some other issue?

Please help. Thanks.

6
  • It could be indeed an async issue : stackoverflow.com/questions/28590701/… Else, check the task.errorOutput, check if it's not in the error one (do the same as the standard one for the error. Commented Nov 13, 2020 at 16:46
  • errorOutput property doesn't exist for Process. I only see task.standardError and attached a pipe to it which also returns empty result. Commented Nov 14, 2020 at 7:10
  • That’s what I meant, sorry. I didn’t check the doc. Loin for the async management. Commented Nov 14, 2020 at 11:02
  • Thanks for reference. I'm already looking into this. Commented Nov 18, 2020 at 7:05
  • I tested, and got It work, by using it as a script. Commented Nov 18, 2020 at 13:57

1 Answer 1

1
+50

Since chrome-cli 1.6.0 was released in 2018 but new Chrome versions with new features appear regularly I suggest to use Chrome directly via SBApplication to access to any needed info e.g.:

import ScriptingBridge

@objc protocol ChromeTab {
    @objc optional var id: Int {get}
    @objc optional var title: String {get}
}
extension SBObject: ChromeTab {}

@objc protocol ChromeWindow {
    @objc optional var tabs: [ChromeTab] {get}
}
extension SBObject: ChromeWindow {}

@objc protocol ChromeApplication {
    @objc optional var windows: [ChromeWindow] {get}
}
extension SBApplication : ChromeApplication {}

// Print all tabs
if let chrome = SBApplication(bundleIdentifier: "com.google.Chrome") as ChromeApplication? {
    if let window = chrome.windows?.first {
        window.tabs?.forEach {
            if let id = $0.id, let title = $0.title {
                print("[\(id)] -", title)
            }
        }
    }
}

For more info about Chrome Extensions look at https://developer.chrome.com/extensions/devguide

Sign up to request clarification or add additional context in comments.

7 Comments

That seems cool alternative but I chrome.windows return 0 element while I have 2 windows open. Do I need some permission? Moreover, I also need to bring a selected tab to front from swift. If you can help about that, it would be awesome. I'm also checking referenced documentation and seems very helpful.
@ZahidUsmanCheema On macos Catalina the system asks to grant access from my app to Chrome and this settings are in System Preferences > Security & Privacy > Automation and after I have access to all data. Maybe it's permission with your user account because both scripts yours and my work OK.
@ZahidUsmanCheema try to add NSAppleEventsUsageDescription in your Info.plist (stackoverflow.com/questions/61445371/…`)
I did. Still no. Should I try with Sandbox mode on or off?
This worked: "After adding NSAppleEventsUsageDescription to info.plist and enabling events in hardended runtime it works now. Thank you."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.