www.Inmagic.com    Inmagic Forums    Inmagic Forums  Hop To Forum Categories  DB/Textworks    Toggle case in TextWorks
Go
New
Find
Notify
Tools
Reply
  
-star Rating Rate It!  Login/Join 
Posted
I think it would be useful while editing a record to be able to quickly change the case of text in TextWorks without having to delete and retype the text, and preferably without having to copy and paste it from another application like Microsoft Word where you can select the text, press Shift and then press the F3 key repeatedly to toggle between the three case modes of "lowercase", "UPPERCASE" and "Title Case".

Does anyone know of a utility I can use within TextWorks for this purpose? Alternatively could this go on the end of a list of possible enhancements?

Thanks, Philip J.
 
Posts: 8 | Location: Canberra, Australia | Registered: Thu January 20 2005Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
you could use jscript, the string methods toLowerCase() and toUpperCase() will cover two, the following function i found by searching, and will convert to title case.

i would think the most efficient way would be just to offer 3 buttons for the three choices,

function toTitleCase(var tString)
{
	var splitBy = tString.split(" ");
	var rsvWords = new Array(  'and', 
                              'the', 
                              'to', 
                              'for', 
                              'is', 
                              'in', 
                              'a', 
                              'at', 
                              'an', 
                              'from', 
                              'by', 
                              'if', 
                              'of'
                              );
	for (i=0;i<splitBy.length;i++) 
  {
		if (i == 0)
		{
			splitBy[i] = (splitBy[i].substring(0,1)).toUpperCase() + splitBy[i].substring(1);
		} else if(rsvWords.indexOf(splitBy[i]) < 0) {
		  	splitBy[i] = (splitBy[i].substring(0,1)).toUpperCase() + splitBy[i].substring(1);
		}
	}
	return splitBy.join(' ');
} 


Luke Stephenson
 
Posts: 112 | Location: Ottawa, Ont, Canada | Registered: Sun July 13 2003Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
I'm new to scripting. It took a long time to come up with the following (based on Sample JavaScript Script 1):
// Low case button
function LowCase_onClick()
{
tString = Form.boxes("Title").content;
newString = tString.toLowerCase( );
Form.boxes("Title").content = newString;
}
This puts all of the text in the "Title" box into lower case. Don't forget to assign the button name and field name under the Name tab (the button's properties only affect its appearance, not its function). The following substitute second line uses the length property of the string to return an initial sentence capital:

newString = tString.charAt(0).toUpperCase() + tString.substring(1,tString.length).toLowerCase();

However for Title Case you cannot simply change ".toLowerCase" to ".toTitleCase". DB/TextWorks version 10 Jscript does not seem to support this method. As an alternative, you need to do something like:

// Title case button
function TitleCase_onClick()
{
var tString = Form.boxes("Title").content;
var lcString = tString.toLowerCase();
var newString = lcString.replace(/(\s[a-z])/g,function (match) { return match.toUpperCase() });
var tcString = newString.charAt(0).toUpperCase() + newString.substring(1, newString.length);
Form.boxes("Title").content = tcString;
}

This finds the white space before a word (\s) and replaces the following letter to upper case. Integrating the scripting Luke provided for prepositions was beyond me. The following is a cruder substitute using alternation:

// Title case button
function TitleCase_onClick()
{
var tString = Form.boxes("Title").content;
var lcString = tString.toLowerCase();
var tcString = lcString.replace(/(\s[a-z])/g,function (match) { return match.toUpperCase() });
var prepString = tcString.replace(/The |To |For |Is |In |A |At |An |And |As |For |From |But |By |If |Of |On |Or |Nor |Yet |So |/gi,function (match) { return match.toLowerCase() });
var startString = prepString.charAt(0).toUpperCase() + prepString.substring(1, prepString.length);
Form.boxes("Title").content = startString;
}

These lower case exceptions (including a few additional prepositions and conjunctions) add up to a very long line, which nevertheless still seems to work. It will leave the last word capitalized, which may not look right, but is in keeping with the recommendations of some style manuals. Capitalisation of titles isn't an exact science, and some additional editing may be required. For example, between, against, through, etc, are also sometimes lower cased, but this button should handle the bulk of the most common little words that are customarily lowercased. Incidentally, Margaret Watts also added changing case to the wishlist in 2004.

One reason it took me so long to get to this stage (in addition to ".toTitleCase" not working) was that I initially tried to set up a button that changes the case of a selected field no matter where it occurs in the record. (I'd prefer not to set up a proliferation of field specific buttons for the title, subtitle, monograph title, identifier/title as subject fields, etc).

It is possible to set up a button that will delete a field entry anywhere in the edit screen solely on the basis of cursor position:

// Delete button
function Delete_onClick()
{
Command.deleteEntry();
}

On this basis I assume you ought to be able to select, copy, edit and replace a selected entry, but I haven't worked out how to do this, if indeed it is possible.
 
Posts: 8 | Location: Canberra, Australia | Registered: Thu January 20 2005Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
Hi

I have just started using this script and it works a treat! The only problem is that each entry after the first starts in lower case. I have no idea if it's possible to alter the script so that each entry begins with an upper case letter.

Thanks in Advance

Jane
 
Posts: 15 | Registered: Thu March 15 2007Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
The title field in our database is nonrepeatable so I wasn't aware of the bug Jane identified Smile. It may be best to handle repeat fields with arrays, but it was beyond me. A simpler solution is to add the entry separator character as an additional search criteria to simply finding the white space before a word (\s). The Unicode for this special unit separator is 001F, so just add |(\u001F[a-z]) as an alternate search in the same line as the \s:

var tcString = lcString.replace(/(\s[a-z])|(\u001F[a-z])/g,function (match) { return match.toUpperCase() });

I'm not completely sure why, but it seems you still need the charAt(0) line to continue to return an initial uppercase character at the start of the box.
 
Posts: 8 | Location: Canberra, Australia | Registered: Thu January 20 2005Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
Hi Philip

I tried to implement the additional functionality (bug fix) and it breaks the script. Frown

My team would love not having to replace those pesky lower case initial alphas so if you can work out the problem that would be great!

Cheers

Jane
 
Posts: 15 | Registered: Thu March 15 2007Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
Hi Jane,

The fix works for me so I'm not sure what might be different for you Confused. Here is what we have for our identifiers field:

// Identifier title case button
function IdTitleCase_onClick()
{
  var fieldString = Form.boxes("Identifiers").content; // Extract box contents
  var lcString = fieldString.toLowerCase(); // Put entries into lower case
  var tcString = lcString.replace(/(\s[a-z])|(\u001F[a-z])/g,function (match) { return match.toUpperCase() });
  var prepString = tcString.replace(/The |To |For |Is |In |A |At |An |And |As |For |From |But |By |If |Of |On |Or |Nor |Yet |So |/gi,function 
    (match) { return match.toLowerCase() });
  var startString = prepString.charAt(0).toUpperCase() + prepString.substring(1, prepString.length);
  Form.boxes("Identifiers").content = startString;
}


Maybe it's just something to do with the line length as it appears in this format? We've got each line starting with "var" indented and as long as possible - only the long string of prepositions needs to be carried over onto a new line - with an additional two space indent before "(match)". Does the script still fail if you try searching for something else instead of u001F, e.g. some other character or word?

For possible alternative control codes see http://www.unicode.org/charts/PDF/U0000.pdf. The Jscript syntax is at http://msdn2.microsoft.com/en-us/library/1400241x.aspx.

Unfortunately you don't need to do much wrong anywhere in a script for the whole thing to fall over Frown.

This message has been edited. Last edited by: Philip J,
 
Posts: 8 | Location: Canberra, Australia | Registered: Thu January 20 2005Reply With QuoteEdit or Delete MessageReport This Post
 Previous Topic | Next Topic powered by eve community  
 

www.Inmagic.com    Inmagic Forums    Inmagic Forums  Hop To Forum Categories  DB/Textworks    Toggle case in TextWorks