Author Topic: function parameters  (Read 1707 times)

0 Members and 1 Guest are viewing this topic.

Nick Riviera

  • Newbie
  • *
  • Posts: 22
function parameters
« on: January 26, 2024, 09:55:43 AM »
Hello!

With the rename tool , can I use parameters in a javascript function?


Code: [Select]
function test(x) {
    var dt = new Date();
    var dx = dt.getDate() + x;
    return dx
}

RTT

  • Administrator
  • *****
  • Posts: 908
Re: function parameters
« Reply #1 on: January 28, 2024, 03:38:29 AM »
You can pass one parameter to the script using this notation:

[ScriptName](parameter)

and access it from the script, as a string type, with CurrentField.value

Using your example, the expression needs to be:
[f]_[test](value)

And the script:
Code: [Select]
function test() {
    var dt = new Date();
    var dx = dt.getDate() + CurrentField.value;
    return dx
}

The parameter can be the result of another expression:
[ScriptName](1,5,3,[script2])

Because in this case it contains a separator, you can emulate passing multiple parameters:
var parameters=CurrentField.value.split(',');

Nick Riviera

  • Newbie
  • *
  • Posts: 22
Re: function parameters
« Reply #2 on: January 31, 2024, 11:01:04 AM »
Thanks. cool. parameters works...
I would  ask, how I can run a custom script rename files with a shortcut
I tried with this script but it doesn't work for me

CallShellTools.js shortcut: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PDF-ShellTools
Target:  C:\Temp\CallShellTools.js Script  RunScript "ScriptName=test"

Code: [Select]
var PDFShellToolsExePath = "C:\Program Files (x86)\PDF-ShellTools\PDFShellTools.exe";

var ShellApp = new ActiveXObject("Shell.Application");
var SelectedFiles = [];
var Windows = ShellApp.Windows();
for (var i = 0; i < Windows.count; i++) {
    var Window = Windows(i);
    if (Window.FullName.toLowerCase().indexOf("iexplore.exe") == -1) {
        var SelectedItems = Window.Document.SelectedItems();
        for (var n = 0; n < SelectedItems.count; n++) {
            var SelectedItem = SelectedItems.Item(n);
            SelectedFiles.push(SelectedItem.Path);
        }
    }
}
if (SelectedFiles.length > 0) {
    var SelectedFilesList = SelectedFiles.join(';')
    var ArgumentsList = '';
    for (i = 1; i < WScript.Arguments.length; i++) {
        ArgumentsList += '"' + WScript.Arguments(i) + '" ';
    }
    ShellApp.ShellExecute(PDFShellToolsExePath, WScript.Arguments(0) + ' ' + ArgumentsList + ' "' + SelectedFilesList + '"', '', '', 0);
} else {
    var sapi = new ActiveXObject("sapi.spvoice");
    sapi.Speak("No files selected");
}

RTT

  • Administrator
  • *****
  • Posts: 908
Re: function parameters
« Reply #3 on: February 01, 2024, 03:03:34 AM »
You have to escape the backslash characters in a JavaScript string.
var PDFShellToolsExePath = "C:\\Program Files (x86)\\PDF-ShellTools\\PDFShellTools.exe";

Take note that you can use the scripts API from an external script directly. No need to call an internal script from an external one. You just need to register the pdfe root object (check the attached screenshot), and then initiate it as usually with any other ActiveX object.
var pdfe = new ActiveXObject("MyScripts.PDFEObject");