Archive for the ‘NoteTab’ Category

h1

NoteTab Clip – Day of Week

19/02/2009

I seem to be doing rather a lot with NoteTab at the moment and again I thought I’d share something useful I scripted.

NoteTab has limited date related functionality but I needed to get the day of the week from a date.  I chose the Doomsday algorithm to base my clip on and the clips below simply generate an integer that represents the day of the week and then translate that into text.  The test clip simply takes a date from the user (defaults to today’s date) and then returns a prompt box informing the user what the day of the week is.

H=”Day of Week Test”
^!Prompt ^$DayOfWeekLong(^$DayOfWeekInt(^?{(M=”00/00/0000″)Enter date=^$GetDate(dd/mm/yyyy)$})$)$

H=”_DayOfWeekInt”
;Sunday=0/Monday=1/Tuesday=2/Wednesday=3/Thursday=4/Friday=5/Saturday=6
^!SetListDelimiter /
^!SetArray %TheDate%=^&
^!Set %Day%=^%TheDate1%
^!Set %Month%=^$Calc(((^%TheDate2%+9) MOD 12)+1)$
^!Set %YearFull%=^%TheDate3%
;If we are in January or February (month = 11 or 12), reduce the year by 1
^!If ^%Month%<10 SKIP ^!Dec %YearFull% ;Extract the year components ^!Set %YearShort%=^$StrCopyRight("^%YearFull%";2)$ ^!Set %Century%=^$StrCopyLeft("^%YearFull%";2)$ :Calculate ^!Result ^$Calc((^%Day% + FLOOR(2.6*^%Month% - 0.2) - 2*^%Century% + ^%YearShort% + FLOOR(^%YearShort%/4) + FLOOR(^%Century%/4)) MOD 7)$ H="_DayOfWeekLong" ^!SetListDelimiter / ^!SetArray %Days%=Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday ^!Set %Key%=^$Calc(^$DayOfWeekInt(^%TheDate%)$+1)$ ^!Result ^%Days^%Key%% [/sourcecode]

h1

NoteTab – Calculating Text

17/02/2009

In my latest foray into NoteTab arithmetic I came across a curious fact around performing arithmetic calculations on a variable containing text.  Whilst in purely mathematical terms an arithmetic operation on something that has a numeric value is a bit nonsensical, but NoteTab is a text editor…

H=”Inc Text”
^!Set %Val%=FooBar
^!Prompt >^%Val%^%Val%< [/sourcecode] This clip displays two prompts when run.  The first prompt as one might expect displays ">FooBar<".  The second prompt however displays ">1<"! Apparently any text is evaluated to zero when you try and perform a purely arithmetic operation on it.  Which broadly speaking makes sense if you just think about a piece of text as having no arithmetic value (or "zero"). So I came across this interesting point when I was taking a masked numeric input from a clip wizard.  Take a look at the following clip. Running this clip and accepting the default input of two you mjight expect should give you a final prompt of  "3", except you've spotted where I'm going with this and you know that it's probably going to be something to do with adding numbers to text and so turn out to be "1" instead.  Well ... you're absolutely right. The result is 1 as the input mask (even though it is using optional numeric place holders after the first compulsory numeric place holder) simply pads the input value with spaces. i.e. The calculation should be of the form: 2+1=3 But we actually get: "2  "+1=0+1=1 The answer is therefore quite simple - remove the spaces... So the astute amongst you will have noticed that we are now trimming the input to remove spaces which should now nicely produce the desired result when we use the default of 2 giving us a final result of 3. Errr.... except we still get a result of 1. It seems that there's either a bit of a bug or an evaluation order at work here that means we don't actually end up trimming off the spaces.  It seems we need an extra assignment step.  The following clip does produce the desired result of 3. So the moral of the story is ensure that your variables are purely numeric before you start performing arithmetic operations on them and make sure that if you need to trim user input for a numeric you assign it to a variable first before trying to trim.

h1

NoteTab Numeric Accuracy

15/02/2009

I’ve been working on something to help me with some logging activity in NoteTab and I’ll be posting a variety of useful bits and pieces from this in both this micro blog (FlagIT) and my main blog (RebootIT).  As part of this I was looking at producing some date related clips which necessitated looking at some mathematical clips and notably one to determine if a number is an Integer.  NoteTab is a text editor and numeric manipulations are definitely not its forte, so I needed to produce a clip function to do this.

Integers are whole numbers.  They have no decimal part.  Fortunately the ^$Calc()$ function does expose a few mathematical functions to a clipper such as myself and one of these is INT.

As an aside at this point the range of mathematical functions available is quite deeply hidden.  The clip help file does provide one link back into the main NoteTab help file where there is a section called “Calculate in NoteTab” which I’d recommend taking a look at.  To use the functions in there you’ll need to understand the function and the number of operands.  INT has one operand (or parameter) that is passed to it,  and this is illustrated below.

So if we take a number and evaluate it to X decimal places, then subtract its integer part (i.e. anything to the left of the decimal point), we are left with the decimal part.  This remainder will be zero if the original number was an integer and non-zero if the original number was a non-integer.

e.g. (all to 3 decimal places)

  • 3.000 – 3.000 = 0.000 => Integer
  • 3.123 – 3.000 = 0.123 => Non-Integer
  • (-3.123)-(-3.000) = -0.123 => Non-Integer

To ensure that no matter what number was being passed, I had to be sure to evaluate to an appropriate number of decimal places otherwise I could get into trouble…

e.g. (all to 3 decimal places)

  • 3.0001 would give 3.000 – 3.000 = 0.000 => Integer … which is wrong.

Therefore if the level of accuracy is equal to the length of the number of characters being passed in I’m always assured of the correct level of accuracy.

e.g.

3 would use an accuracy of 1 decimal place

  • 3.0 – 3.0 = 0.0 => Integer

3.123 would use an accuracy of 5 decimal places

  • 3.12300 – 3.00000 = 0.12300 => Non-Integer

So the clip function for IsInteger becomes the following:

H="IsInteger"
^!Set %DPofAccuracy%=^$StrSize("^&")$
^!Result=True
^!If ^$Calc(INT(^&);^%DPofAccuracy%)$=^$Calc(^&;^%DPofAccuracy%)$ END
^!Result=False

This however got me to thinking.  Every programming language using numeric variables specifies a level of accuracy in decimal places, significant figures, etc. based upon the memory allocation internally managed for the variable.  I couldn’t find anything stating what this was in NoteTab (which isn’t to say it doesn’t exist) and so I thought I’d write a quick clip to work it out.

H="Numeric Variable Accuracy"
^!ClearVariables
^!Set %Accuracy%=0
:LOOP
^!If ^%Accuracy%=0 SKIP
^!Set %CurrentValue%=1.^$StrFill("0";^$Calc(^%Accuracy%-1)$)$1
^!Set %Output%=^%Output^%NL%(^%Accuracy%) ^%CurrentValue% : ^$Calc(^%CurrentValue%;^%Accuracy%)$
^!Inc %Accuracy%
^!If ^%Accuracy%=20 FINISH
^!Goto LOOP
:FINISH
^!Info ^%Output%

The clip simply builds an ever smaller decimal part and then displays it to a specified number of decimal places.  I set it to display the results after 20 iterations from a starting point of no decimal places and these are the results (structured here in a nice table format for easier reading off the page).

Decimal Places Value Evaluation
0 1 1
1 1.1 1.1
2 1.01 1.01
3 1.001 1.001
4 1.0001 1.0001
5 1.00001 1.00001
6 1.000001 1.000001
7 1.0000001 1.0000001
8 1.00000001 1.00000001
9 1.000000001 1.000000001
10 1.0000000001 1.0000000001
11 1.00000000001 1.00000000001
12 1.000000000001 1.000000000001
13 1.0000000000001 1.0000000000001
14 1.00000000000001 1.00000000000001
15 1.000000000000001 1.000000000000001
16 1.0000000000000001 1.0000000000000000
17 1.00000000000000001 1.00000000000000000
18 1.000000000000000001 1.000000000000000000
19 1.0000000000000000001 1.0000000000000000000

As you can see at 16 decimal places there is a difference between the original value and the evaluated value. Therefore the IsInteger function (as well as any other mathematical calculations using NoteTab directly) will be subject to a 15 decimal places of accuracy limit.

Now this means that we need to ensure that the maximum number of decimal places used is also limited for the IsInteger function as if we then try to use a number with a total character length that exceeds 15 we start to get unpredictable results.

For example during the following evaluation^$IsInteger(10203040506070.1)$ the function actually ends up evaluating the following equality in the ^!If statement (line 4 of the clip).

10203040506070.0000000000000000=10203040506070.0996100000000000

Whilst it happens to give the right result there is scope here for failure.

Therefore we need to amend the clip to only allow up to 15 decimal places of accuracy.

H="IsInteger"
^!Set %DPofAccuracy%=^$Calc(MIN(^$StrSize("^&")$;15))$
^!Result=True
^!If ^$Calc(INT(^&);^%DPofAccuracy%)$=^$Calc(^&;^%DPofAccuracy%)$ END
^!Result=False
h1

NoteTab Clip – Duplicate File

02/02/2009

Quite often when working on a file I want to clone it as a backup or to work on another copy to try something out. I was doing this so often and being such a lazy coder I whipped up a quick NoteTab clip to make it even easier. Run it with the file you want to duplicate open in NoteTab and it will create a duplicate in a new tab and give it the focus.

H="Duplicate File"
^!Set %TEXT%=^$GetText$
^!TOOLBAR New Document
^!InsertText ^%TEXT%

This is a simple copy and paste job really, but a few additional lines of clip code and it could be tailored for example to retain the cursor position and text selection in the duplicate.

h1

NoteTab Clipping – Binary Clip Wizard Options

01/02/2009

NoteTab clipping uses the clip wizard functionality to produce sophisticated input dialog windows to obtain information from the user.  There is however a strange view produced when looking at a single check box. The clip code shown below shows the various pieces of clip code used and below that are the discussions of the approach and options along with the screen shots of what the clip wizard produces.

H="Single check box 1"
^!Info ^?[(T=A)Check a setting to enable it=Setting-1]

H="Single check box 2"
^!Info ^?[(T=A)Check a setting to enable it=Setting-2|]

H="Single check box 3"
^!Info ^?[(T=A)Check a setting to enable it=|Setting-3]

H="Dual option combo-box 4"
^!Info ^?[(T=C)Setting-4==_Yes|No]
No box to tick

No box to tick

Display too narrow

Display too narrow

Ghost check box

Ghost check box

Combo box with two options

Combo box with two options

The first clip uses some clip code that should display an array of check boxes … but just a single check box as shown in the first screen shot on the right. The odd thing is that the check box is missing.  This happens on both Windows Vista and Windows XP so I think it used to happen when I ran Windows 98.  So what can be done to rectify this?

Given that the array of check boxes does work for more than one check box, I thought I’d try conning NoteTab into thinking that there might be more than one check box.  This was done in the second clip by adding a pipe character after the first array item, but not then adding any additional items.  The result of this can be seen in the second screen shot where the bottom of the display of the text field seems to be too high making it difficult to read … but at least the check box had appeared.

The third option attempted was to see what happened if the pipe was shifted to come before the first item of the array rather than after it.  Unsurprisingly this generated an empty of “ghost” item in the list of check boxes as can be seen in the third screen shot.

Because none of these worked and I couldn’t find an easy way to pass and evaluate a newline token to the clip wizard (which could have added an extra line for the second option to expand the field’s height), I took a sideways step and created a fourth option with a drop down list (combo box) with two options  This seems to be the most reliable and elegant solution for a single binary option.

h1

NoteTab Clip – Use Empty File

29/01/2009

A little clip I like to use in NoteTab is one for ensuring that I have an empty file.  I usually call it using the ^!FarClip command from a standard library before I dump out the results of something where I always want it to be separate.  It’s a very short and sweet little clip – just paste it into your favourite little clip library.

H="_Use Empty File"
;If the current document isn't blank then
;open a new blank document
!If ^$GetTextSize$ = 0 Skip
^!TOOLBAR New Document

Also note that the underscore on the header. This ensures that the clip can’t be run separately but only from another clip. You can remove this if you like, but if you want to run it separately I’d suggest a quick glance and pressing the new document button or pressing CTRL+N might be faster.

h1

NoteTab Clipping – Detect Parameter Passing

27/01/2009

When working with NoteTab clips I like to be efficient and reuse clips just like programmers do with functions, subroutines, code libraries, etc. So one little thing I like to use when dealing with some clips is a conditional statement that allows the clip to work out if it was called from another clip with a parameter.

To accomplish this we’re actually going to use something that the NoteTab Clip help file recommends against – converting 4 (decimal) to a character using the ^$DecToChar()$ function. ASCII(004) is an end of transmission character, but it turns our that we can test against this on the parameter place holder. Just trying to measure the size as zero when a parameter is passed for example curiously gives a length of two characters.

^!If ^& > ^$DecToChar(4)$^$DecToChar(4)$ PARAM_PASSED Else NO_PARAM

The two go to labels used here show the logic. PARAM_PASSED is where a parameter has been passed and NO_PARAM is where no parameter was passed.

This snippet of code is useful because it can be used for example at the start of a clip that could be run manually by a user taking in a parameter through an input box. It can also be used at the end in determining how to deal with a result – display it, insert it, pass it back in the %RESULT% variable, etc.

Please note that the code does not distinguish between a clip called with no parameters from another clip and a clip run manually. Of course if the clip is called from another clip using a dummy parameter it could be used as a crude way of doing this.