1

I'm using Tauri with React JS as frontend in WINDOWS.

And this App i'm trying to make is also for windows for now

I want to write a batch script inside user's computer then run it .

I'm making a small app which will set user's desktop wallpaper, as I came across electron then searched its alternative and choose tauri - https://tauri.app/v1/api/js/

I came to a conclusion on how to make it -

  1. first save the image that is clicked inside user's directory which i achieved .

  2. second was to write a batch file inside the user's directory to change the wallpaper which i half achieved as I wrote the file to selected destination but - the content of batch file is -

@echo off
set "wallpaperPath=C:\Users\<me>\OneDrive\Desktop\image.jpg"
reg add "HKCU\\Control Panel\\Desktop" /v Wallpaper /t REG_SZ /d "%wallpaperPath%" /f
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters

in react is wrote like this using escape sequence -

const scriptContent = `@echo off\nset "wallpaperPath=C:\\Users\\<me>\\OneDrive\\Desktop\\image.jpg"\nreg add "HKCU\\Control Panel\\Desktop" /v Wallpaper /t REG_SZ /d "%wallpaperPath%" /f\nRUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters`;

but the problem is it is writing the file content like this as it returns UTF8 format (this is the saved file content from user's directory)-

@echo off
set "wallpaperPath=C:UsersmeOneDriveDesktopimage.jpg"
reg add "HKCU\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d "%wallpaperPath%" /f
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters

here there is no slashes used

  1. The last one and Main problem is that how will I run this file as these will be depend on each other like - first image will be saved , soon after file will be written , then just after that the file will be executed.

I watched all the documents and api guides , i went through the discussion channel to find out what others had problem while using shell but in the end I'm confused on how will this work . Link to shell api - https://tauri.app/v1/api/js/shell

I tried everything I could think of, I have been working on this this small project from 5-6 days , I know this can be achieved by using other Languages but now It's a challenge to me as i spent a lot of time on this .

Once again -

  1. how to write file in other format to save it as i liked , as it's necessary to run.
  2. how can i run this file automatically after that.

IMPORTANT - any other approach is welcomed.

1 Answers1

0

Well, u can for sure use the sidebar thing, Though i guess this can be done directly using rust code too

Full reference with sidecars: https://tauri.app/v1/guides/building/sidecar/

Extract from the docs

Tauri.Conf.json

{
  "tauri": {
    "bundle": {
      "externalBin": [
        "/absolute/path/to/sidecar",
        "relative/path/to/binary",
        "binaries/my-sidecar"
      ]
    },
    "allowlist": {
      "shell": {
        "sidecar": true,
        "scope": [
          { "name": "/absolute/path/to/sidecar", "sidecar": true },
          { "name": "relative/path/to/binary", "sidecar": true },
          { "name": "binaries/my-sidecar", "sidecar": true }
        ]
      }
    }
  }
}

A binary with the same name and a -$TARGET_TRIPLE suffix must exist on the specified path. For instance, "externalBin": ["binaries/my-sidecar"] requires a src-tauri/binaries/my-sidecar-x86_64-unknown-linux-gnu executable on Linux. You can find the current platform's target triple by looking at the host: property reported by the rustc -Vv command.

If the grep and cut commands are available, as they should on most Unix systems, you can extract the target triple directly with the following command:

rustc -Vv | grep host | cut -f2 -d' '

On Windows you can use PowerShell instead:

rustc -Vv | Select-String "host:" | ForEach-Object {$_.Line.split(" ")[1]}

JS / TS

import { Command } from '@tauri-apps/api/shell'
// alternatively, use `window.__TAURI__.shell.Command`
// `binaries/my-sidecar` is the EXACT value specified on `tauri.conf.json > tauri > bundle > externalBin`
const command = Command.sidecar('binaries/my-sidecar')
const output = await command.execute()```
AHQ Secret
  • 46
  • 7