NeoBook Function: Convert Long Day and Month to Short

There are times when you may need to convert long days (Sunday) into short days (Sun), and long months into short months. An easy way to do this is to put the conversion code into a NeoBook function, and then call it whenever you need it.

For example, an RSS xml feed requires the published date in this format: Fri, 14 Nov 2008 09:11:18. To code a button that will do this when clicked, create 2 new functions (Options|Function Library), name them Convert Long Day to Short and Convert Long Month to Short (or anything you like), and paste the following code into them:

DAY FUNCTION

If "[day]" "=" "Monday"
SetVar "[shortday]" "Mon"
endif

If "[day]" "=" "Tuesday"
SetVar "[shortday]" "Tue"
endif

If "[day]" "=" "Wednesday"
SetVar "[shortday]" "Wed"
endif

If "[day]" "=" "Thursday"
SetVar "[shortday]" "Thu"
endif

If "[day]" "=" "Friday"
SetVar "[shortday]" "Fri"
endif

If "[day]" "=" "Saturday"
SetVar "[shortday]" "Sat"
endif

If "[day]" "=" "Sunday"
SetVar "[shortday]" "Sun"
endif

MONTH FUNCTION

If "[month]" "=" "January"
SetVar "[shortmonth]" "Jan"
endif

If "[month]" "=" "February"
SetVar "[shortmonth]" "Feb"
endif

If "[month]" "=" "March"
SetVar "[shortmonth]" "Mar"
endif

If "[month]" "=" "April"
SetVar "[shortmonth]" "Apr"
endif

If "[month]" "=" "May"
SetVar "[shortmonth]" "May"
endif

If "[month]" "=" "June"
SetVar "[shortmonth]" "Jun"
endif

If "[month]" "=" "July"
SetVar "[shortmonth]" "Jul"
endif

If "[month]" "=" "August"
SetVar "[shortmonth]" "Aug"
endif

If "[month]" "=" "September"
SetVar "[shortmonth]" "Sep"
endif

If "[month]" "=" "October"
SetVar "[shortmonth]" "Oct"
endif

If "[month]" "=" "November"
SetVar "[shortmonth]" "Nov"
endif

If "[month]" "=" "December"
SetVar "[shortmonth]" "Dec"
endif

In your button, enter this code:

... Convert day and month
Call "Convert to short day"
Call "Convert to short month"
... Copy to clipboard
SetVar "[clipboard]" "[shortday], [daynum] [shortmonth] [year] [hour]:[minute]:[second]"
... Paste from clipboard
SendKeys "" "{CtrlDn}v{CtrlUp}"

These two could be combined into one function, but since you may not need both for your application, we're using 2 functions. If you don't need the time, year etc., just delete those variables from the setvar action.

To use them, locate your cursor in a textentry object where you want the date and time to appear, click the button, and voila!, there it is.

Neobook Tip: USA States And Territories

Contributed by Sam Cox:

Here are the USA states and territories (abbreviations and full names) in NeoBook variables delineated (separated) by commas. Using previously posted subroutines, you can populate ListBox or ComboBox objects from these variables. For example:

SetVar "[Args]" "ComboBoxStates"
Gosub "ListBoxClear"

SetVar "[Args]" "ComboBoxStates,[USA_States_Abbrev]"
Gosub "ListBoxAddItemsFromCSV"

Please note that each variable is one long string. The appearance of multiple lines is caused by the word-wrap.

CODE

SetVar "[USA_States_Abbrev]" "AL,AK,AZ,AR,CA,CO,CT,DE,FL,GA,HI,ID,IL,IN,IA,KS,KY,LA,ME,MD,MA,MI,MN,MS,MO, MT,NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,OR,PA,RI,SC,SD,TN,TX,UT,VT,VA,WA,WV,WI,WY"

CODE

SetVar "[USA_States]" "Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,Florida,Georgia,Hawaii,Idaho,Illinois, Indiana,Iowa,Kansas,Kentucky,Louisana,Maine,Maryland,Massachusetts,Michigan,Minnesota,Mississippi, Missouri,Montana,Nebraska,Nevada,New Hampshire,New Jersey,New Mexico,New York,North Carolina,North Dakota,Ohio, Oklahoma,Oregon,Pennsylvania,Rhode Island,South Carolina,South Dakota,Tennessee,Texas,Utah,Vermont,Virginia,Washington,West Virginia,Wisconsin,Wyoming"

CODE

SetVar "[USA_States_Territories_Abbrev]" "AL,AK,AZ,AR,CA,CO,CT,DE,DC,FL,GA,GU,HI,ID,IL,IN,IA,KS,KY,LA,ME,MD,MA,MI,MN,MS,MO, MT,NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,OR,PA,PR,RI,SC,SD,TN,TX,UT,VT,VA,VI,WA,WV,WI,WY"

CODE

SetVar "[USA_States_Territories]" "Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,District of Columbia,Florida,Georgia, Guam,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky,Louisana,Maine,Maryland, Massachusetts,Michigan,Minnesota,Mississippi,Missouri,Montana,Nebraska,Nevada,New Hampshire,New Jersey,New Mexico, New York,North Carolina,North Dakota,Ohio,Oklahoma,Oregon,Pennsylvania,Puerto Rico,Rhode Island,South Carolina,South Dakota, Tennessee,Texas,Utah,Vermont,Virginia,Virgin Islands,Washington,West Virginia,Wisconsin,Wyoming"

Neobook Tip: Toggle Object between hidden and visible

Contributed by Sam Cox:

Occasionally you want to show an object if it's hidden or hide the object if it's visible. The typical code for this scenario is:

GetObjectInfo "ObjectName" "Visible" "[Status]"
If "[Status]" "=" "True"
HideObject "ObjectName" "hideEffect" "hideSpeed"
Else
ShowObject "ObjectName" "showEffect" "showSpeed"
Endif

Thinking that this is a bit cumbersome, I wrote a subroutine that does all the hard work. All you need do is specify the ObjectName, showEffect, showSpeed, hideEffect, and hideSpeed as one parameter and then call the subroutine.

For example, suppose you want to toggle Picture1 between visible and hidden. Here's how using this new subroutine:

SetVar "[Args]" "Picture1,Wipe Right,10,Fade,5"
Gosub "ToggleObject"

If Picture1 is visible when you call ToggleObject, it will become hidden with HideObject "Picture1" "Fade" "5"; if Picture1 is hidden when you call ToggleObject, it will become visible with ShowObject "Picture1" "Wipe Right" "10".

In general, the values passed to subroutine ToggleObject in the [Args] string are:

SetVar "[Args]" "objectName,showEffect,showSpeed,hideEffect,hideSpeed"

The last two arguments (hideEffect and hideSpeed) are optional. If not provided, their values default to those of showEffect and showSpeed. For example:

SetVar "[Args]" "Picture1,Fade,7"
Gosub "ToggleObject"

is the same as writing:

SetVar "[Args]" "Picture1,Fade,7,Fade,7"
Gosub "ToggleObject"

Here is the subroutine:

SUBROUTINE CODE

:ToggleObject
StrParse "[Args]" "," "[Arg]" "[Arg0]"

.. subroutine arguments parsed from [Args]
.. [Arg1] = name of object
.. [Arg2] = show effect
.. [Arg3] = show speed (0..10)
.. [Arg4] = hide effect (defaults to same as show effect)
.. [Arg5] = hide speed (defaults to same as show speed)

.. local variables
.. [Arg0] = number of subroutine arguments (not used)
.. [Arg6] = "True" if object [Arg1] is visible, otherwise "False"

If "[Arg4]" "=" ""
SetVar "[Arg4]" "[Arg2]"
Endif
If "[Arg5]" "=" ""
SetVar "[Arg5]" "[Arg3]"
Endif
GetObjectInfo "[Arg1]" "Visible" "[Arg6]"
If "[Arg6]" "=" "True"
HideObject "[Arg1]" "[Arg4]" "[Arg5]"
Else
ShowObject "[Arg1]" "[Arg2]" "[Arg3]"
Endif
Return

Neobook Tip: Numbering Lines: Add Line Numbers to Text

Contributed by Sam Cox:

Here is a subroutine that adds leading numbers to lines of text.

Note: In this subroutine, variables named [:VarName] are local to the subroutine and variables named [::VarName] are global constants.

The variable [NumberLines] is the input and output of the subroutine. Typical usage is:
CODE

SetVar "[NumberLines]" "lines to be numbered"
Gosub "NumberLines"
SetVar "[Result]" "[NumberLines]"

The global constant value [::EOL], if defined, defines the character(s) separating lines. If [::EOL] is undefined, the subroutine separates lines with [#13][#10].
CODE

:NumberLines
If "[NumberLines]" "<>" ""
If "[::EOL]" "<>" ""
SetVar "[:EOL]" "[::EOL]"
Else
SetVar "[:EOL]" "[#13][#10]"
Endif
SetVar "[NumberLines]" "[NumberLines][#255]"
StrParse "[NumberLines]" "[:EOL]" "[:Line]" "[:LineCount]"
If "[:Line[:LineCount]]" "=" "[#255]"
SetVar "[:TrailingEOL]" "[:EOL]"
SetVar "[:LineCount]" "[:LineCount]-1"
Else
SetVar "[:TrailingEOL]" ""
StrReplace "[:Line[:LineCount]]" "[#255]" "" "[:Line[:LineCount]]"
Endif
SetVar "[NumberLines]" ""
Loop "1" "[:LineCount]" "[:N]"
SetVar "[NumberLines]" "[NumberLines][:EOL][:N]. [:Line[:N]]"
EndLoop
DeleteArray "[:Line]" "[:LineCount]"
StrLen "[:EOL]" "[:EOL_Length]"
StrDel "[NumberLines]" "1" "[:EOL_Length]" "[NumberLines]"
SetVar "[NumberLines]" "[NumberLines][:TrailingEOL]"
Endif
Return

For example, create a multi-line Text Entry object named TextEntry1 and a PushButton object captioned "Number All Lines" with this code:
CODE

SetVar "[NumberLines]" "[TextEntry1]"
Gosub "NumberLines"
SetVar "[TextEntry1]" "[NumberLines]"
FocusObject "TextEntry1"

Run the program, enter a few lines of text, and click the button.

To number just the selected lines in TextEntry1, create a second PushButton object captioned "Number Selected Lines" with this code:
CODE

SetVar "[ClipBoardSave]" "[Clipboard]"
SetVar "[Clipboard]" "[#255]"
FocusObject "TextEntry1"
SendKeys "" "{CtrlDn}C{CtrlUp}"
If "[ClipBoard]" "<>" "[#255]"
SetVar "[NumberLines]" "[ClipBoard]"
Gosub "NumberLines"
SetVar "[Clipboard]" "[NumberLines]"
FocusObject "TextEntry1"
SendKeys "" "{CtrlDn}V{CtrlUp}"
Endif
SetVar "[ClipBoard]" "[ClipboardSave]"

Run the program, enter several lines of text, highlight a few of them, and click the "Number Selected Lines" button.

Neobook Tip: Combinations And Permutations

Contributed by Sam Cox:

Here is code for computing combinations of NN things taken KK at a time.

CODE

SetVar "[NN]" "[NumberOfThings]"
SetVar "[KK]" "[TakeThisMany]"
Gosub "Combinations" ..returns value in [Combinations]

The returned value [Combinations] is the number of ways of consuming (i.e., taking without replacement) KK things from a population of NN. For example,
CODE

SetVar "[NN]" "42"
SetVar "[KK]" "6"
Gosub "Combinations"

returns [Combinations] = 5245786 which is the odds each Colorado State Lottery ticket has of winning.

COMBINATIONS SUBROUTINE

Note: This version of the subroutine does not validate that its arguments [NN] and [KK] are both positive integers and that [NN] is greater than [KK].
CODE

:Combinations
SetVar "[N]" "[NN]"
Gosub "Factorial"
SetVar "[FactNN]" "[Factorial]"
SetVar "[N]" "[KK]"
Gosub "Factorial"
SetVar "[FactKK]" "[Factorial]"
SetVar "[N]" "[NN]-[KK]"
Gosub "Factorial"
Math "[FactNN]/([FactKK]*[Factorial])" "0" "[Combinations]"
Return

Similarly, here is code for computing the permutations of NN things taken KK at a time.
CODE

SetVar "[NN]" "[NumberOfThings]"
SetVar "[KK]" "[TakeThisMany]"
Gosub "Permutations" ..returns value in [Permutations]

For example, let's compute how many unique four letter sequences we can make from 10 letters without using any letter more than once.
CODE

SetVar "[NN]" "10"
SetVar "[KK]" "4"
Gosub "Permutations"

returns [Permutations] = 5040.

PERMUTATIONS SUBROUTINE

Note: This version of the subroutine does not validate that its arguments [NN] and [KK] are both positive integers and that [NN] is greater than [KK].
CODE

:Permutations
SetVar "[N]" "[NN]"
Gosub "Factorial"
SetVar "[FactNN]" "[Factorial]"
SetVar "[N]" "[KK]"
Gosub "Factorial"
Math "[FactNN]/[Factorial]" "0" "[Permutations]"
Return

FACTORIAL SUBROUTINE
CODE

:Factorial
Math "[N]" "0" "[N]"
SetVar "[Factorial]" "1"
If "[N]" ">" "1"
Loop "2" "[N]" "[FactTemp]"
Math "[FactTemp]*[Factorial]" "0" "[Factorial]"
EndLoop
EndIf
Return