Wich detected Script CC

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Native2904
Posts: 132
Joined: 23 Apr 2025 18:48
Location: Aachen

Wich detected Script CC

Post by Native2904 »

Hello,
today I tried to build a Custom Column (CC) script for XYplorer for the first time.
My goal was to display the script type directly in a column, similar to how it is shown in the editor. I started from a basic template generated by ChatGPT and then tried to work through it on my own.
I have a background in the trades, with both practical and theoretical experience, but this is new territory for me, and from that I know very well that first independent attempts in a new field often contain ridiculous mistakes.
With that in mind, I would be grateful if someone could take a look at my script and point out errors or better approaches.

e.g.: I am still very skeptical about the design compared to other scripts from the forum, and furthermore, I do not get a correct specification for any script:

Code: Select all

$r = 1201;
$head = readfile(<cc_item>, 1, 300, $r);
if (!$head) return "";

// lowercase & trim für Vergleich
$h = lower(trim($head));
$ext = lower(gettoken(<cc_item>, -1, "."));

// 1) Shebang
if (startswith($h, "#!")) {
    if (contains($h, "python")) return "Python Script";
    if (contains($h, "bash") || contains($h, "sh")) return "Shell Script";
}

// 2) Extension
if ($ext == "ahk") return "AutoHotkey Script";
if ($ext == "py") return "Python Script";
if ($ext == "ps1") return "PowerShell Script";
if ($ext == "bat") return "Batch Script";
if ($ext == "cmd") return "Batch Script";
if ($ext == "sh") return "Shell Script";
if ($ext == "xys") return "XY Script";
if ($ext == "js") return "JavaScript";

// 3) Inhalt
if (contains($h, "autohotkey")) return "AutoHotkey Script";
if (contains($h, "import ") && contains($h, "def ")) return "Python Script";
if (contains($h, "param(") || contains($h, "get-command")) return "PowerShell Script";

// Fallback für allgemeine Textdateien
if (isin($ext, "txt|cfg|csv")) return "Allgemeiner Text";

// Alles andere
return "Nicht erkannter Inhalt";
Lts. 64-bit XY-Port. ƒ JetBrainsMono @ Windows 11 Pro 24H2 ▣ 1920x1080 ⇵ 115%
Glückauf

Norn
Posts: 504
Joined: 24 Oct 2021 16:10

Re: Wich detected Script CC

Post by Norn »

GPT.png
?

Code: Select all

// Read first 300 bytes
$r = 1201;
$head = readfile(<cc_item>, 1, 300, $r);
if ($head == "") {
    return "";
}

// Normalize header for comparison
$h = recase(trim($head), "lower");

// File extension (lowercase)
$ext = recase(gettoken(<cc_item>, -1, "."), "lower");

//
// ---- Script types by shebang ----
//
if (substr($h, 1, 2) == "#!") {

    if (strpos($h, "python") > 0) {
        return "Python Script";
    }

    if (strpos($h, "bash") > 0 || strpos($h, "sh") > 0) {
        return "Shell Script";
    }

    if (strpos($h, "perl") > 0) {
        return "Perl Script";
    }

    if (strpos($h, "ruby") > 0) {
        return "Ruby Script";
    }

    return "Script (Shebang)";
}

//
// ---- Script types by extension ----
//
if ($ext == "ahk") { return "AutoHotkey Script"; }
if ($ext == "js")  { return "JavaScript"; }
if ($ext == "ps1") { return "PowerShell Script"; }
if ($ext == "py")  { return "Python Script"; }
if ($ext == "lua") { return "Lua Script"; }
if ($ext == "bat" || $ext == "cmd") { return "Batch Script"; }

//
// ---- Text formats by content ----
//
if (substr($h, 1, 5) == "<?xml") {
    return "XML Text";
}

if (substr($h, 1, 9) == "<!doctype" || substr($h, 1, 5) == "<html") {
    return "HTML Text";
}

if (substr($h, 1, 1) == "{" || substr($h, 1, 1) == "[") {
    return "JSON Text";
}

//
// ---- Plain text by extension ----
//
if ($ext == "txt") { return "Plain Text"; }
if ($ext == "md")  { return "Markdown Text"; }
if ($ext == "ini") { return "INI Text"; }
if ($ext == "log") { return "Log Text"; }

return "";
$ext = recase(gettoken(<cc_item>, -1, "."), "lower"); Can be replaced with $ext = <cc_ext>;
You do not have the required permissions to view the files attached to this post.
Last edited by Norn on 17 Jan 2026 22:02, edited 1 time in total.
Windows 11 24H2 @100% 2560x1440

highend
Posts: 14923
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Wich detected Script CC

Post by highend »

Stop using AI, if you aren't capable to correct even the basic nonsense that it creates...

Even the new version still uses things like:
$head = readfile(<cc_item>, 1, 300, $r);
which returns only crap. Read the docs for readfile() why^^

And in general: Do the extension checking first, it makes no sense to read parts of a file (and slow down everything) if the extension already identifies a file a 100%...
One of my scripts helped you out? Please donate via Paypal

Norn
Posts: 504
Joined: 24 Oct 2021 16:10

Re: Wich detected Script CC

Post by Norn »

For beginners, AI can provide templates and ideas, but you still need to check the syntax against the documentation. If you can't do that, then you need to learn scripting from scratch.
Windows 11 24H2 @100% 2560x1440

Native2904
Posts: 132
Joined: 23 Apr 2025 18:48
Location: Aachen

Re: Wich detected Script CC

Post by Native2904 »

Your explanations make sense, of course.
Nevertheless, tomorrow morning I will compare Norn's script with mine using WinMerge and learn from it.
I was aware that all the necessary conclusions could be drawn from the extension; at least after I looked around for scripting tutorials and ended up on Autohotkey, I realised that I had no use for it, so I came back to XY and looked for a case here that I could implement.
Nevertheless, thank you for now.

=======

I have learned that ChatGPT contradicts itself faster than you can blink without feeling embarrassed.
I also learned that Help.pdf is much better for research than *.chm.
Even though it was more like a stupid accident, I learned a few things about syntax that will be helpful to me as a starting point in the future.
But turning to scripting without any knowledge is like flying blind.
:oops:
Lts. 64-bit XY-Port. ƒ JetBrainsMono @ Windows 11 Pro 24H2 ▣ 1920x1080 ⇵ 115%
Glückauf

highend
Posts: 14923
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Wich detected Script CC

Post by highend »

Rule number one:
Do not develop for a CC in the first place, always write it for a normal single file / folder selection in the current pane!

Normally the only things that have to be exchanged afterwards are the variable names and return / e | echo | text statements.

Here is a small script type converter that uses what's inside the clipboard and converts it into the other format (and vice versa).
The output script is inside the clipboard again.

So for example this is the normal version script:

Code: Select all

      $gps = get("exif", "gps", <curitem>);
      text $gps ? $gps : "<no coordinates>";
Copying that into the clipboard and executing my script will lead to:

Code: Select all

      $gps = get("exif", "gps", <cc_item>);
      return $gps ? $gps : "<no coordinates>";
Would that be converted again you'd get the same output as the original script...

I'll always use text instead of e or echo because the output is easier to read and has no problems if it's a longer output...

Code: Select all

    $clp   = <clp>;
    $table = <<<'>>>'
        <cc_item>|<curitem>
        <cc_path>|<curitempath>
        <cc_name>|<curname>
        <cc_base>|<curbase>
        <cc_ext>|<curext>
        <cc_size>|<cursize>
    >>>;
    $table = regexreplace($table, "[ \t]+");

    // At least one variable must be present
    $pattern = "(" . replace($table, <crlf>, "|") . ")";
    end (!regexmatches($clp, $pattern)), "Nothing to convert, aborted!";

    // Custom column or normal variables?
    $ccTable = "(" . trim(replace(regexreplace($table, "\|.+?$"), <crlf>, "|"), "|") . ")";
    $scTable = "(" . trim(replace(regexreplace($table, "^.+?\|"), <crlf>, "|"), "|") . ")";
    $isCC    = (regexmatches($clp, $ccTable)) ? true : false;
    $isSC    = (regexmatches($clp, $scTable)) ? true : false;
    end ($isCC && $isSC), "Both variable type(s) present, cannot convert!";

    if ($isCC) { $clp = convert($clp, $table, "cc"); }
    if ($isSC) { $clp = convert($clp, $table, "sc"); }
    copytext $clp;


function convert($clp, $table, $type) {
    $orgPos  = ($type LikeI "cc") ? 1 : 2;
    $newPos  = ($orgPos == 1) ? 2 : 1;
    $pattern = ($orgPos == 1) ? "return[ ]+.+?;" : "text[ ]+.+?;";
    $replace = ($orgPos == 1) ? "return|text" : "text|return";

    foreach($line, $table, <crlf>, "e") {
        $varOrg = gettoken($line, $orgPos, "|", "t");
        $varNew = gettoken($line, $newPos, "|", "t");
        $clp = replace($clp, $varOrg, $varNew);
    }
    $matches = regexmatches($clp, $pattern, "<::>");
    $repOrg  = gettoken($replace, 1, "|");
    $repNew  = gettoken($replace, 2, "|");
    foreach($match, $matches, "<::>", "e") {
        $clp = replace($clp, $match, replace($match, $repOrg, $repNew), , , 1);
    }
    return $clp;
}
One of my scripts helped you out? Please donate via Paypal

Native2904
Posts: 132
Joined: 23 Apr 2025 18:48
Location: Aachen

Re: Wich detected Script CC

Post by Native2904 »

2highend
Thanks for the explanation :info: . I will read through it carefully later.

###########

So, thank you again. Your answer helped me approach the issue once more.
I instructed ChatGPT to go through the XY script syntax with me every morning, step by step, strictly following the rules of XY, and to make sure it reviews the material beforehand. (Brain jogging)
It might sound a bit silly…
Nevertheless,"I can now" derive proper syntax directly from a natural, literal description.
Task A – AND

Return "image file"
if the extension is jpg OR png
AND the file size is greater than 500 KB.
Otherwise, return nothing.

Code: Select all

return ((<cc_ext> == "jpg" || <cc_ext> == "png") && <cc_size> > 512000) ? "image file" : "";
All of this has now resulted in the first Autohotkey keystrokes using user variables on my machine.
Last Sunday, all I could see were characters that, after staring at them for a while, made me feel more frustrated :blackstorm: than enlightened.

There is nothing more beautiful than when, across a thousand puzzle pieces, the first contours begin to emerge and the first possibilities can already be inferred.
Lts. 64-bit XY-Port. ƒ JetBrainsMono @ Windows 11 Pro 24H2 ▣ 1920x1080 ⇵ 115%
Glückauf

Post Reply