Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: SOLVED: JScript code problem to find any whitespaces in collection (Read 1381 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

SOLVED: JScript code problem to find any whitespaces in collection

I had this code to find any whitespaces in the collection which someone else wrote for me. I cannot find the original thread.

It worked for an earlier version of the JScript component but not v2.7.5.

Here's the error when I click on the button :
Code: [Select]
JScript Panel v2.7.5 (id:1051102)
JavaScript runtime error:
'_' is undefined
File: <main>
Line: 100, Col: 5

Here's the code:
Code: [Select]
// ==PREPROCESSOR==
// @import "%fb2k_component_path%samples\complete\js\lodash.min.js"
// @import "%fb2k_component_path%docs\flags.txt"
// @import "%fb2k_component_path%docs\helpers.txt"
// ==/PREPROCESSOR==

function get_tag_list() {
  var items = fb.GetLibraryItems();
  var tags = [];
  for (var i = 0; i < items.Count; i++) {
    var f = items.Item(i).GetFileInfo();
    for (var j = 0; j < f.MetaCount; j++) {
      var tag = f.MetaName(j).toLowerCase();
      if (!_.includes(tags, tag))
        tags.push(tag);
    }
    f.Dispose();
  }
  items.Dispose();
  return tags;
}

ButtonStates = {
  normal: 0,
  hover: 1,
  down: 2,
  hide: 3
}

var g_theme = window.CreateThemeManager("Button");
var g_font = gdi.Font("Segoe UI", 12);

function SimpleButton(x, y, w, h, text, fonClick, state) {
  this.state = state ? state : ButtonStates.normal;
  this.x = x;
  this.y = y;
  this.w = w;
  this.h = h;
  this.text = text;
  this.fonClick = fonClick;

  this.containXY = function (x, y) {
    return (this.x <= x) && (x <= this.x + this.w) && (this.y <= y) && (y <= this.y + this.h);
  }

  this.changeState = function (state) {
    var old = this.state;
    this.state = state;
    return old;
  }

  this.draw = function (gr) {
    if (this.state == ButtonStates.hide) return;

    switch (this.state)
    {
      case ButtonStates.normal:
      g_theme.SetPartAndStateId(1, 1);
      break;

      case ButtonStates.hover:
      g_theme.SetPartAndStateId(1, 2);
      break;

      case ButtonStates.down:
      g_theme.SetPartAndStateId(1, 3);
      break;

      case ButtonStates.hide:
      return;
    }

    g_theme.DrawThemeBackground(gr, this.x, this.y, this.w, this.h);
    // RGB function is defined in docs\helpers.txt
    // DT_* are defined in docs\flags.txt
    gr.GdiDrawText(this.text, g_font, RGB(0,0,0), this.x, this.y, this.w, this.h, DT_CENTER| DT_VCENTER | DT_CALCRECT | DT_NOPREFIX);
  }

  this.onClick = function () {
    this.fonClick && this.fonClick();
  }
}

function drawAllButtons(gr) {
  for (var i in $buttons) {
    $buttons[i].draw(gr);
  }
}

function chooseButton(x, y) {
  for (var i in $buttons) {
    if ($buttons[i].containXY(x, y) && $buttons[i].state != ButtonStates.hide) return $buttons[i];
  }

  return null;
}

$buttons = {
  first_btn: new SimpleButton(5, 5, 150, 26, "Find Tag Whitespaces", function () {
    var leftquery = _.map(get_tag_list(), function (item) {
      return '"$left(%' + item + '%,1)" IS " "';
    }).join(" OR ");
    fb.ShowLibrarySearchUI(leftquery);
    var rightquery = _.map(get_tag_list(), function (item) {
      return '"$right(%' + item + '%,1)" IS " "';
    }).join(" OR ");
    fb.ShowLibrarySearchUI(rightquery);

    var whitespaces = _.map(get_tag_list(), function (item) {
      return '"%' + item + '%" HAS "  "';
    }).join(" OR ");
    fb.ShowLibrarySearchUI(whitespaces);

  })
}

var cur_btn = null;
var g_down = false;

function on_paint(gr) {
  gr.FillSolidRect(0, 0, window.Width, window.Height, utils.GetSysColour(15));
  drawAllButtons(gr);
}

function on_mouse_move(x, y) {
  var old = cur_btn;
  cur_btn = chooseButton(x, y);

  if (old == cur_btn) {
    if (g_down) return;
  } else if (g_down && cur_btn && cur_btn.state != ButtonStates.down) {
    cur_btn.changeState(ButtonStates.down);
    window.Repaint();
    return;
  }

  old && old.changeState(ButtonStates.normal);
  cur_btn && cur_btn.changeState(ButtonStates.hover);
  window.Repaint();
}

function on_mouse_leave() {
  g_down = false;

  if (cur_btn) {
    cur_btn.changeState(ButtonStates.normal);
    window.Repaint();
  }
}

function on_mouse_lbtn_down(x, y) {
  g_down = true;

  if (cur_btn) {
    cur_btn.changeState(ButtonStates.down);
    window.Repaint();
  }
}

function on_mouse_lbtn_up(x, y) {
  g_down = false;

  if (cur_btn) {
    cur_btn.onClick();
    cur_btn.changeState(ButtonStates.hover);
    window.Repaint();
  }
}

Re: JScript code problem to find any whitespaces in collection

Reply #1
Replace this...

Code: [Select]
// @import "%fb2k_component_path%samples\complete\js\lodash.min.js"

with

Code: [Select]
// @import "%fb2k_component_path%samples\js\lodash.min.js"

https://github.com/marc2k3/foo_jscript_panel/wiki/Breaking-Changes#sample-changes


Re: JScript code problem to find any whitespaces in collection

Reply #2
Legend. Thanks for the quick help. 

Re: SOLVED: JScript code problem to find any whitespaces in collection

Reply #3
and there's always Properties>Clean up

Re: SOLVED: JScript code problem to find any whitespaces in collection

Reply #4
and there's always Properties>Clean up
I went with the script to find them and then take them into mp3tag to regex out the whitespaces.

Running the clean tags every time on the entire collection would be just too slow.

I'll try the clean tags the next time after filtering, thanks for that suggestion.