tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[bug] OSX - Open .app binary using command vs shell

Open Kottakji opened this issue 1 year ago • 1 comments

Describe the bug

I'm trying to open a .app binary on OSX.

This works:

    await open ("bin/osx/example.app");

But I need:

  • The pid
  • To add some arguments

So, I would like to use Command + spawn. But I can't get it to work.

    await new Command("example-app").spawn();
{
      "shell": {
        "all": false,
        "open": ".*",
        "execute": true,
        "scope": [
          {
            "name": "example-app",
            "cmd": "bin/osx/example.app"
          }
        ]
      }
}

It's giving me:

image

Any help is appreciated.

Reproduction

I've created a repo with a minimum project, having 2 buttons. One tries to open the .app with a shell (that works), another using new command (I can't get that to work).

https://github.com/Kottakji/tauri_osx_question

Expected behavior

No response

Full tauri info output

> [email protected] tauri
> tauri "info"


[✔] Environment
    - OS: Mac OS 13.3.1 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.74.1 (a28077b28 2023-12-04)
    ✔ cargo: 1.74.1 (ecb9851af 2023-10-18)
    ✔ rustup: 1.26.0 (5af9b9484 2023-04-05)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 18.2.0
    - yarn: 1.9.4
    - npm: 8.9.0

[-] Packages
    - tauri [RUST]: 1.6.0
    - tauri-build [RUST]: 1.5.1
    - wry [RUST]: 0.24.7
    - tao [RUST]: 0.16.7
    - @tauri-apps/api [NPM]: 1.5.3
    - @tauri-apps/cli [NPM]: 1.5.10

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist
    - devPath: http://localhost:1420/
    - framework: React
    - bundler: Vite

Stack trace

No response

Additional context

No response

Kottakji avatar Feb 20 '24 19:02 Kottakji

@Kottakji I use opener for my app

https://docs.rs/opener/0.6.1/opener/#

use opener; opener::open(path).map_err(|e| format!("Failed to open path: {}", e));

kurdin avatar Feb 21 '24 22:02 kurdin

@Kottakji The open function you're using does not support passing arguments as it is intended to open directories and URLs.

You are halfway to your solution using Command; however, you cannot spawn a .app; you need to point to the app's executable. i.e, /Applications/Example.app/Contents/MacOS/example

For example, say you want to launch the Firefox app with a URL to open as an argument; you will do the following:

tauri.conf.json

"execute": true,
"scope": [
     {
        "name": "open-new-tab-firefox",
        "cmd": "/Applications/Firefox Developer Edition.app/Contents/MacOS/firefox",
        "args": [
            {
                "validator": "\\S+"
            }
        ]
    }
]

In your frontend, App.tsx for example:

import { Command } from "@tauri-apps/api/shell";

async function openNewTab(url: string) {
    console.log("Opening new tab with URL:", url);

    const command = new Command("open-new-tab-firefox", [url]);

    return command.spawn();
}

// then call the function...
openNewTab("https://tauri.app");

Let me know if you have any other questions.

ahkohd avatar Feb 24 '24 21:02 ahkohd

@Kottakji The open function you're using does not support passing arguments as it is intended to open directories and URLs.

You are halfway to your solution using Command; however, you cannot spawn a .app; you need to point to the app's executable. i.e, /Applications/Example.app/Contents/MacOS/example

For example, say you want to launch the Firefox app with a URL to open as an argument; you will do the following:

tauri.conf.json

"execute": true,
"scope": [
     {
        "name": "open-new-tab-firefox",
        "cmd": "/Applications/Firefox Developer Edition.app/Contents/MacOS/firefox",
        "args": [
            {
                "validator": "\\S+"
            }
        ]
    }
]

In your frontend, App.tsx for example:

import { Command } from "@tauri-apps/api/shell";

async function openNewTab(url: string) {
    console.log("Opening new tab with URL:", url);

    const command = new Command("open-new-tab-firefox", [url]);

    return command.spawn();
}

// then call the function...
openNewTab("https://tauri.app");

Let me know if you have any other questions.

Thank you very much for your reply. Hereby verified that this works. ❤️

Kottakji avatar Feb 25 '24 20:02 Kottakji