How to pass custom variables in javascript from API?

I’m using API to call ui vision but I don’t know exactly how to pass custom variables

this is what I put in windows cmd:

"C:\Program Files\Mozilla Firefox\firefox.exe" -no-remote -P links "file:///C:/Users/Administrator/Desktop/Tools/ui.vision.html?macro=download_mp4_file.js&direct=1&closeBrowser=1&savelog=log.txt&dynamicUrl=``https://safebooru.org/samples/2386/sample_52a9b62657315c27f209c70d2f0db0df6c6c6275.jpg&dynamicFilename=test.jpg``"

but gives error:

Status=Error: getVar: 'dynamicUrl' is not set — set it with uiv.setVar('dynamicUrl', …) first, or run the command that stores into it. To allow it, pass a default: uiv.getVar('dynamicUrl', null)

[status] download_mp4_file.js started
[error] download_mp4_file.js failed: getVar: 'dynamicUrl' is not set — set it with uiv.setVar('dynamicUrl', …) first, or run the command that stores into it. To allow it, pass a default: uiv.getVar('dynamicUrl', null) (line 2) (Runtime 0.21s)

this is the macro:

// Retrieve dynamic values passed from n8n into UI.Vision global variables
const url = uiv.getVar('dynamicUrl');
const wantedName = uiv.getVar('dynamicFilename');

const downloadFolder = 'C:\Users\Administrator\Desktop\Tools\paw downloads';

// The server generates the file on request, so give the download a generous
// start window (seconds) before the transfer itself is awaited.
uiv.setVar('!TIMEOUT_WAIT', 30);
uiv.log('Starting download: ' + url, 'blue');

// uiv.download returns the name the file actually got on disk.
const file = uiv.download(url, { timeout: 300 });

if (!file) {
throw new Error('Download did not complete - no file name returned.');
}

uiv.log('Downloaded as: ' + file, 'green');

// --- rename on disk -------------------------------------------------------
// A plain "move" through cmd.exe: no PowerShell quoting problems, and /Y
// overwrites an existing file with the wanted name so repeated runs work.
const src = downloadFolder + '\' + file;
const dst = downloadFolder + '\' + wantedName;

uiv.log('Renaming to: ' + wantedName, 'blue');

uiv.run('XRunAndWait', 'cmd.exe', '/c move /Y "' + src + '" "' + dst + '"');

// --- verify ---------------------------------------------------------------
const check = uiv.run('XRunAndWait', 'cmd.exe',
'/c if exist "' + dst + '" (exit 0) else (exit 1)');

// XRunAndWait throws on a non-zero exit code, so reaching this line means the
// file with the wanted name is on disk.
uiv.log('Saved as: ' + dst, 'green');

How to fix this?

Hi, your own parameter names are not passed to the macro.

The command line API has three fixed slots for values: cmd_var1, cmd_var2 and cmd_var3. Inside the macro they arrive as !CMD_VAR1, !CMD_VAR2 and !CMD_VAR3.

Command line:

"C:\Program Files\Mozilla Firefox\firefox.exe" -no-remote -P links "file:///C:/Users/Administrator/Desktop/Tools/ui.vision.html?macro=download_mp4_file.js&direct=1&closeBrowser=1&savelog=log.txt&cmd_var1=https://safebooru.org/samples/2386/sample_52a9b62657315c27f209c70d2f0db0df6c6c6275.jpg&cmd_var2=test.jpg"

Macro:

const url = uiv.getVar('!CMD_VAR1');
const wantedName = uiv.getVar('!CMD_VAR2');

Three more notes:

  • If a value contains &, #, %, + or a space, URL-encode it, in n8n for example with {{ encodeURIComponent($json.url) }}. Ui.Vision decodes it again. Inside a .bat file, write each % as %%.

  • If you need more than three values, pack several into one, for example cmd_var1=|, and split it in the macro with .split(‘|’).

  • In the macro as posted, the Windows paths have single backslashes. In JavaScript they must be doubled: ‘C:\Users\Administrator\Desktop\Tools\paw downloads’ and downloadFolder + ‘\’ + file. If that is only the forum formatting, ignore this.