×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Codewarrior Scripting

From Nokia Developer Wiki
Jump to: navigation, search
Macoshark (Talk | contribs)
(New page: == Introduction == CodeWarrior has a (poor) scripting API that can be used to add nifty little helper features that can make the everyday programming task a bit more easier. == Installat...)
 
Macoshark (Talk | contribs)
Line 3: Line 3:
  
 
== Installation ==
 
== Installation ==
All that needs to be done is to store the perl scripts to a folder somewhere and then adding a command binding in CodeWarrior to call the script. In each script I will give the binding that I use, but they can be bound to any key combination.
+
<p>All that needs to be done is to store the perl scripts to a folder somewhere and then adding a command binding in CodeWarrior to call the script. In each script I will give the binding that I use, but they can be bound to any key combination.</p>
 +
<p>The steps to install a script are:
 +
<ul>1. From CodeWarrior menu select Edit->Commands and Key Bindings.</ul>
 +
<ul>2. Click "New Group" to create a group for the scripts if it does not already exist.</ul>
 +
<ul>3. Click "New Command" and give it a good name.</ul>
 +
<ul>4. In the "Execute" box browse the path for wperl.exe (C:\perl\bin\wperl.exe in my case)</ul>
 +
<ul>5. In the "Argument" box browse the path for the script and add possible command line parameters.</ul>
 +
<ul>6. Click "New Binding" and hit they key combination you want. You may need to remove the old binding for it.</ul>
 +
<ul>7. Click "Save" and you are done.</ul></p>
 +
 
 +
== Limitations ==
 +
As I already mentioned, the support that CodeWarrior provides is rather poor. This means that the scripting API is very limited and the scripts do not work in all windows. For example they do not work debugger windows or the error/warning window. The source file must be opened into a new window for CodeWarrior to successfully run the script. Usually it can be done by double-clicking for example the error description.
  
 
== To Upper/Lower Case ==
 
== To Upper/Lower Case ==
Line 39: Line 50:
 
}
 
}
 
</code>
 
</code>
 +
 +
=== Command Binding ===
 +
<p><b>To Upper Case:</b>
 +
<pre>
 +
Arguments:  C:\scripts\upper_case.pl
 +
Key Binding: Ctrl-U
 +
</pre></p>
 +
 +
<p><b>To Lower Case:</b><pre>
 +
Arguments:  C:\scripts\upper_case.pl L
 +
Key Binding: Ctrl-Shift-U
 +
</pre></p>
 +
 +
== Open Folder ==
 +
This script opens the file explorer to the folder of the active document. I find it useful when I step into someone else's code while debugging and want to see the header file which is usually located nearby.
 +
 +
=== open_folder.pl ===
 +
<code perl>
 +
use strict;
 +
 +
use Win32::OLE;
 +
use POSIX;
 +
 +
# Create instance of CodeWarrior
 +
my $CWApp = Win32::OLE->new("CodeWarrior.CodeWarriorApp");
 +
 +
# Get active document
 +
my $doc = $CWApp->ActiveDocument() or die "can't get active doc!\n";
 +
 +
# Get the full path of the active document
 +
my $file_spec = $doc->FileSpec();
 +
my $path = $file_spec->FullPath();
 +
 +
# Strip the file name off
 +
$path =~ s/(.+\\).+$/$1/e;
 +
 +
# Open explorer to containing folder
 +
my $cmd = "explorer ".$path;
 +
system( $cmd );
 +
</code>
 +
 +
=== Command Binding ===
 +
<p>
 +
<pre>
 +
Arguments:  C:\scripts\open_folder.pl
 +
Key Binding: Ctrl-å
 +
</pre></p>

Revision as of 18:04, 27 June 2007

Contents

Introduction

CodeWarrior has a (poor) scripting API that can be used to add nifty little helper features that can make the everyday programming task a bit more easier.

Installation

All that needs to be done is to store the perl scripts to a folder somewhere and then adding a command binding in CodeWarrior to call the script. In each script I will give the binding that I use, but they can be bound to any key combination.

The steps to install a script are:

    1. From CodeWarrior menu select Edit->Commands and Key Bindings.
    2. Click "New Group" to create a group for the scripts if it does not already exist.
    3. Click "New Command" and give it a good name.
    4. In the "Execute" box browse the path for wperl.exe (C:\perl\bin\wperl.exe in my case)
    5. In the "Argument" box browse the path for the script and add possible command line parameters.
    6. Click "New Binding" and hit they key combination you want. You may need to remove the old binding for it.
    7. Click "Save" and you are done.

Limitations

As I already mentioned, the support that CodeWarrior provides is rather poor. This means that the scripting API is very limited and the scripts do not work in all windows. For example they do not work debugger windows or the error/warning window. The source file must be opened into a new window for CodeWarrior to successfully run the script. Usually it can be done by double-clicking for example the error description.

To Upper/Lower Case

This is a script that implements the conversion of selected text to upper or lowercase. This comes standard in Visual Studio, but is lacking from CodeWarrior.

File: upper_case.pl

use strict;
use warnings;
 
use Win32::OLE;
use POSIX;
 
# Create instance of CodeWarrior
my $CWApp = Win32::OLE->new("CodeWarrior.CodeWarriorApp");
 
# Get active document
my $doc = $CWApp->ActiveDocument() or die "can't get active doc!\n";
my $text_engine = $doc->TextEngine or die "can't get TextEngine!\n";
 
# Check that there is a selection
if( $text_engine->HasSelection() ) {
# Get selected text
my $selected_text = $text_engine->SelectionText();
 
my $param = uc $ARGV[0];
if ( $param eq "L" ) {
$selected_text = lc $selected_text;
} else {
$selected_text = uc $selected_text;
}
 
# Reset the selected line contents
$text_engine->InsertText( $selected_text );
}

Command Binding

To Upper Case:

Arguments:   C:\scripts\upper_case.pl
Key Binding: Ctrl-U

To Lower Case:

Arguments:   C:\scripts\upper_case.pl L
Key Binding: Ctrl-Shift-U

Open Folder

This script opens the file explorer to the folder of the active document. I find it useful when I step into someone else's code while debugging and want to see the header file which is usually located nearby.

open_folder.pl

use strict;
 
use Win32::OLE;
use POSIX;
 
# Create instance of CodeWarrior
my $CWApp = Win32::OLE->new("CodeWarrior.CodeWarriorApp");
 
# Get active document
my $doc = $CWApp->ActiveDocument() or die "can't get active doc!\n";
 
# Get the full path of the active document
my $file_spec = $doc->FileSpec();
my $path = $file_spec->FullPath();
 
# Strip the file name off
$path =~ s/(.+\\).+$/$1/e;
 
# Open explorer to containing folder
my $cmd = "explorer ".$path;
system( $cmd );

Command Binding

Arguments:   C:\scripts\open_folder.pl
Key Binding: Ctrl-å

145 page views in the last 30 days.
×