User Functions Exchange

Discuss and share scripts and script files...
Post Reply
bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

User Functions Exchange

Post by bdeshi »

Starting from v15.00.0311, XYplorer supports user-defined functions! and a mechanism to include files!

Let's share those brilliant functions you all come up with in one place here, instead of having them sprinkled about in obscure corners of the forum!


User-defined functions in the help file:

Code: Select all

::help "idh_scripting.htm#idh_scripting_udf";

( :ninja: If any UDFs were posted elsewhere in the fora but missing here, do suggest in this topic or rather, send me a PM.)
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: User Functions Exchange

Post by bdeshi »

user function index
====
allstrpos() : Returns all positions of a substring in a string.
arabictoroman() : Convert Arabic (decimal) numbers to Roman numeric system.
comp() : Compares two numbers and returns either the larger or the smaller one.
createplaylist() : Create a playlist out of given files or all mp3 files in current folder.
dectohex() : Returns the hexadecimal form of a decimal number (Note: undocumented builtin dectohex() exists)
debug() : example of a simple debugging method.
dent() : Removes or adds indentation to text, and returns result.
dict() : An example of a dictionary/hashtable function.
dlbtn() : Downloads and adds a CTB from an online snippet URL. Returns ctbindex.
fact() : Factorial function.
flip() : boolean toggle a variable.
frac() : Returns the fractional part of a decimal number.
gethtmltoken() : returns option value or optionlist in an html() GET form submit url.
getlayout() : Returns value of one or more layout element.
getitemindex() : Returns list indexes of given items in current list.
gettabidx() : Returns a tab ID for the given location and pane, optionally creating the tab.
gettabindex() : Returns index of the tab(s) matching given criteria.
gettokenrange() : Returns a range of tokens (eg, tokens 2-5) from a tokenlist.
getvolumeserialnumber() : Returns storage volume serial of storage drive(s).
hextobin() : Returns the binary form of an hexadecimal number.
htmlescape() : Escapes special html characters in an html string.
in() : Checks if a token (or tokens) exists in a list. (Intended for conditional expressions)
instokenpos() : Inserts a token at a specified position of a tokenlist.
int() : Returns the integer part of a decimal number.
isprocessrunning() : Checks if a particular process is running. Returns 1 if it is.
lipsumgen() : Returns dummy filler text with defined word-length.
listfilter() : Filter a list by testing items against an expression.
luminance() : Change luminance of given hex color.
max() : Returns the largest in a collection of numbers.
max() : Returns the largest of two numbers.
min() : Returns the smallest of two numbers.
mstime() : Converts milliseconds to hh:mm:ss.fff format.
ncr() : Combination function. Keep input at or below the lower 20's (< 28) for any reliability
npr() : Permutation function. Keep input at or below the lower 20's (< 28) for any reliability
nthroot() : Returns the nth root of a positive number in base 10.
pad() : Pads two strings or numbers to the same length.
popupnestedpath() : Returns menu-path to the selected item of a popupnested() menu.
range() : Returns a list of numbers by custom steps from a range,
regexescape() : Escapes special regexp characters in a string.
regread() : Reads data from the registry.
regwrite() : Writes data to the registry.
remtoken() : Removes specified tokens from a tokenlist.
remtokenpos() : Removes the token at a specified position of a tokenlist.
reptokenpos() : Replaces the token at a specified position of a tokenlist.
resolvevolumeserialnumber() : Returns driveletter of a storage volume serial.
romantoarabic() : Converts Roman numbers to Arabic, ie, to modern decimal.
selsortlist() : sort a list using Selection Sort.
sendkeyescape() : Escapes a string for use in sendkeys.
sign() : Returns the sign of a decimal number.
sqrt() : Returns the square root of a positive number in base 10.
swap() : Swaps the values of two variables.
swapkeyvalue() : Swap keys and values in a "key:value;key:value" string.
varsize() : Returns the raw byte count of the value of a variable.
xycolorconv : A set of functions to convert between regular hex colors and the decimal color format used in XYplorer.ini.
xypcre : A collection of functions that add PCRE support, including regexp match, capture, replace, split.
====
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: User Functions Exchange

Post by bdeshi »

I'll start off with this one that I've always wanted: getpathcomponent() == gpc() !

function

Code: Select all

function gpc($path, $comp = 'path', $indx = 1) {
//exact alias of getpathcomponent() . See ::help 'idh_scripting_comref.htm#idh_sc_getpathcomponent';
  $path = ($path == '') ?
          ((<curitem> == '') ? <curpath> : <curitem>) : 
          $path ;
  return getpathcomponent($path, $comp, $indx);
}
call
assuming the function is saved in <xyscripts>\inc.inc:

Code: Select all

include "inc.inc"
echo gpc(<curpath>,'base');

ed. here's my original complicated version before TQ's improved one:

Code: Select all

function gpc($path, $comp, $indx) {
  $str = 'global $gpc = getpathcomponent(' .
         ($path == '') ? ('')  : (''  . quote($path)) .
         ($comp == '') ? (',') : (',' . quote($comp)) .
         ($indx == '') ? (')') : (',' . $indx . ')' )  ;
  global $gpc;
  $tmp = $gpc;  //backup $global gpc pre-function value
  load $str,,s; //call getpathcomponent()
  $ret = $gpc;  //transfer $gpc to return value
  $gpc = $tmp;  //restore $global gpc pre-function value
  unset $tmp;
  return $ret;
}
[/size]
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 60288
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: User Functions Exchange

Post by admin »

Wow, your OCV (original complicated version) was really mind-warping. Psychedelic programming, dude! :cup:

Stef123

Re: User Functions Exchange

Post by Stef123 »

Great idea, this function repository. If this is what I think it is, it provides newbies with building blocks for their own fledgling attempts at scripting? Stuff like "check if name exists", check if selection contains single or multiple folders or files - all of it much better thought-through than a podunk programmer like me could ever arrive at, by reading the help file alone.

The challenge here, imo - finding a format that matches a mid-level knowledge. Ideally, a brief description of what the function does, and an example how to use it in my own script. Well, that's speaking for me, of course :wink: - not sure whether I am totally off course with these hopes?

admin
Site Admin
Posts: 60288
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: User Functions Exchange

Post by admin »

I see even downloadable toolbar buttons (would have been possible even before UDFs).

Paste something like this into the Address Bar and press ENTER (that's lower than Mid-Level):

Code: Select all

::dlbtn([Url]);
The rest would be done by XY: Download the script that lives at the Url, create a user button from it, and add it to the toolbar. Woosh. Slick user experience...

Filehero
Posts: 2644
Joined: 27 Feb 2012 18:50
Location: Windows 10 Pro x64

Re: User Functions Exchange

Post by Filehero »

admin wrote:Psychedelic programming, dude! :cup:
Native code obfuscation. :mrgreen: The nested ternary constructs are real hard core. :ninja:

back to topic: great idea. :tup:

Cheers,
FH

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: User Functions Exchange

Post by bdeshi »

@Stef: exactly! (although this can help everybody, not just newbies :whistle: )
admin wrote:

Code: Select all

::dlbtn([Url]);
:tup:

About my OCD OCV, it's just trying hard to send null/empty values* (not just "") to xy's gpc()
Because getpathcomponent(<curpath>,'',); is not the same as getpathcomponent(<curpath>,,);
I wanted to skip defining default values and let xy::gpc() do it a it usually handles empty values. But alas an empty string is not an empty value.

Glad to cause brain damage to you all! :mrgreen:



* in fact, there you have a new wish!
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

binocular222
Posts: 1416
Joined: 04 Nov 2008 05:35
Location: Hanoi, Vietnam

Re: User Functions Exchange

Post by binocular222 »

There's a small logic loophole in getpathcomponent() itself: If target is a folder with dot "." in folder name, then get "base" will return the portion before the dot only.
So, I change Sammy's gpc() function a bit to workaround this:

Code: Select all

function gpc($path, $comp = 'path', $indx = 1) {
  $path = ($path == '') ?
          ((<curitem> == '') ? <curpath> : <curitem>) :
          $path ;
  if($comp == 'base' AND property("Type","$path") == "File folder") {return gettoken($path, -1, '\')};
  Else {return getpathcomponent($path, $comp, $indx)};
}
P.S: when I run the gpc(), then rewrite the coding of the gpc(), run again, XY does not recognize the revised gpc() version?

Edit: Can we have a more "XY sound-like" file extension? ".inc" seems weird to me. FYI: Having an universally-agreed extension help me to write syntax-highlighting.
Last edited by binocular222 on 21 Apr 2015 11:25, edited 1 time in total.
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

admin
Site Admin
Posts: 60288
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: User Functions Exchange

Post by admin »

The loophole is by design. GPC does not check for the nature of items. It works on items that do or don't exist. It's a mere string operation. No relation to reality.

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: User Functions Exchange

Post by bdeshi »

I'd expect gpc() to behave exactly like getpathcomponent() . It's meant only as a short alternative. But mangle it however you wish. It's a USER function after all.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

highend
Posts: 13260
Joined: 06 Feb 2011 00:33

Re: User Functions Exchange

Post by highend »

Code: Select all

function gpc($path, $comp = 'path', $indx = 1) {
  $path = ($path == '') ?
          ((<curitem> == '') ? <curpath> : <curitem>) :
          $path ;
  if($comp == 'base' AND property("Type","$path") == "File folder") {return gettoken($path, -1, '\')};
  Else {return getpathcomponent($path, $comp, $indx)};
}
The problem with this modification is: It's language dependent. E.g. on a german OS property("Type","$path") returns "Dateiordner" instead of "File folder".
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: User Functions Exchange

Post by bdeshi »

suggesting xyi as a standard xyplorer script include file extension
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

PeterH
Posts: 2776
Joined: 21 Nov 2005 20:39
Location: Germany

Re: User Functions Exchange

Post by PeterH »

SammaySarkar wrote:suggesting xyi as a standard xyplorer script include file extension
As long as .inc isn't "something wellknown" for other systems, (to me it seems it is not,) I'm fully with this.

Edit: though I'd not call it "XY script include" but only "XY include".
W7(x64) SP1 German
( +WXP SP3 )

binocular222
Posts: 1416
Joined: 04 Nov 2008 05:35
Location: Hanoi, Vietnam

Re: User Functions Exchange

Post by binocular222 »

Remove specific tokens from list of tokens

Code: Select all

function remtoken($string, $token, $sep = ",") {
	$string = $sep . $string . $sep;
	foreach($thistoken, $token, $sep) {
		$thistoken = $sep . $thistoken . $sep;
		$string = replace($string, $thistoken, $sep);
	}
	return trim($string,$sep)
}
Usage: save above code as "inc.inc" then execute this xys script:

Code: Select all

include "inc.inc"
$String1 = "mp3,m4a,flac,mid,spc,m4a";
	$ToDel = "m4a,mid";
	echo remtoken($String1, $ToDel)  //return "mp3,flac,spc"
Edit: Typo, thanks PeterH
Last edited by binocular222 on 24 Apr 2015 11:27, edited 1 time in total.
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

Post Reply