How to get the selected text in a browser

Till last month, I seldom worked with JavaScript. But these days I am working on a web-based product and can’t avoid learning JavaScript. So my first mini-adventure was to get the selected text on a web page, for which I got code from here . I loved the simplicity and power of JavaScript in this example.

<script type="text/javascript">
//<![CDATA[

document.onclick=getSelText;

function getSelText()
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
else return;
if(txt!='') alert(txt);
}

//]]
</script>

(Source of Code: http://www.codetoad.com/javascript_get_selected_text.asp )

  • Share/Bookmark

Export ASP.NET page to Word, Excel or PDF

A crisp code @

http://geekswithblogs.net/vivek/archive/2006/09/26/92316.aspx

Tried exporting to word…. worked well, for excel, it was showing warning though content was present .

Anyway try in deep on Thursday..

  • Share/Bookmark

Working with ‘Files & Folders’ in PowerShell

A fairly good amount of time we spend on our system for managing, searching files, folders. And when you are in learning phase of any new scripting language (just like me, in PS), it is obvious to have an eagerness about “How to” work with files and folder in that Scripting lang. Fortunately, PowerShell makes scripting files and directories as easy as working at the command line. Being an administrative shell, PowerShell directly supports tasks such as visiting all the files in a subdirectory or moving a file from one directory to another.

To retrieve the list of files in a directory, use the Get-ChildItem cmdlet. To get a specific item, use the Get-Item cmdlet:

· To list all items in the current directory, use the Get-ChildItem cmdlet:

Get-ChildItem

· To list all items that match a wildcard, supply a wildcard to the Get-ChildItem cmdlet:

Get-ChildItem *.doc

· To list all files that match a wildcard in the current directory (and all its children), use the –Include and –Recurse parameters of the Get-ChildItem cmdlet:

Get-ChildItem –Include *.doc -Recurse

· To list all directories in the current directory, use the Where-Object cmdlet to test the PsIsContainer property:

Get-ChildItem | Where { $_.PsIsContainer }

· To get information about a specific item, use the Get-Item cmdlet:

Get-Item test.txt

More to come in next post…

  • Share/Bookmark

PowerShell: Building a Form

(My Article for Weekly PowerShell Column in office e-mag)

Today we learn about a surprising feature of PowerShell: forms. Many people think that PS is just another bro of cmd.exe and can’t do much fun with GUI. Hmmm…they are simply wrong. Since PS is based on .NET platform, it can access any .NET base class easily and Forms are no exception. So let’s try something into it.

Before calling any .NET namespace, we need to load that DLL into PS session. And this can be done with following commands:

[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null

[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

For creating a form object, try this:

$form1 = New-Object System.Windows.Forms.Form

You can set various properties of form object directly. E.g. Name, text:

$form1.Text = "Ankit Form"

$form1.Name = "form1"

For a button object, try it:

$button1 = New-Object System.Windows.Forms.Button

$button1.Name = "button1"

 

clip_image002

 

We will discuss various aspects of forms in coming posts. For now, if you wanna play with some form code, try this PS Code:

 

function GenerateForm {

[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

$form1 = New-Object System.Windows.Forms.Form
$button1 = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

$button1_OnClick=
{
[System.Windows.Forms.MessageBox]::Show("Now call Police!")

}

$OnLoadForm_StateCorrection=
{
    $form1.WindowState = $InitialFormWindowState
}

$form1.Text = "Ankit Form"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 284
$System_Drawing_Size.Height = 262
$form1.ClientSize = $System_Drawing_Size

$button1.TabIndex = 0
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 104
$System_Drawing_Size.Height = 35
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True

$button1.Text = "Kill me!"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 89
$System_Drawing_Point.Y = 102
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($button1_OnClick)

$form1.Controls.Add($button1)

$InitialFormWindowState = $form1.WindowState

$form1.add_Load($OnLoadForm_StateCorrection)

$form1.ShowDialog()| Out-Null

}

#Call the Function
GenerateForm

  • Share/Bookmark

Challenge of the Month!!

Assignment in PS. Here is the ScreenShot

need to complete by next Wednesday… Hmmmmmm

Assignment_screenShots

  • Share/Bookmark

C# in PowerShell

In today’s training session, learned about how to mix-up C# with PowerShell. An Example here:

Add-Type -Typedefinition @"
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            String[] items = new String[] { "Joe", "John", "Jimmy", "Jerr" };
            int totallenght =0;
            foreach(String item in items)
            {
                totallenght+= item.Length;
            }
            Console.WriteLine("the total is {0}",totallenght);
        }
    }
}

"@

[ConsoleApplication1.Program]::Main($null)

 

Interestingly, the whole action of the above code can be done through this smaller (Only) PowerShell code too:

function list {$args}
$totallenght = 0
list Joe John Jimmy Jerry | foreach { $totallength+= $_.Length }
"the total is $totallength"

  • Share/Bookmark

Toy Code: Dictionary in PowerShell

Note: The Code given here uses Google Dictionary for word definition. Its just a toy…

 

$Word= Read-host "What is your Word? "

$url = "http://www.google.com/dictionary?langpair=en|en&q=" + $Word

$ie = New-Object -com internetexplorer.application;
$ie.visible = $false;
$ie.navigate($url);

while ($ie.Busy -eq $true)
        {
        Start-Sleep -Milliseconds 1000;
        }

$text = $ie.document.body.innertext

$atpos=$text.IndexOf("Synonyms")

if ($atpos -gt 0){
    $len=$text.Length -$atpos
    $result=$text.Substring($atpos,$len)
    $atlast= $result.IndexOf("Web definitions")
    $mean= $result.Substring(0, $atlast)
    Write-Host $mean
        }
else { Write-host "Dictionary:: not found"}

  • Share/Bookmark

Old days back: PowerShell program for Fibonacci Series

Was free for 15 mins (after a big chaos in Office, after all its Monday) and thought to right a small Script in PowerShell. And couldn’t think beyond toy programs that generally are part of introductory course of any programming language. Anyway, wrote Fibonacci series program.

Code Here:

Write-Host "Please enter number of terms:"
$terms = Read-Host
if($terms -lt 2) {
    Write-Host "U fool!!"
}
else
{
$first=1
$Sec=1
Write-Host $first
Write-Host $sec
for ($i=1;$i -le $terms;$i++)
         {
              $temp=$sec
        $sec=$sec+$first
        $first= $temp
        Write-Host $sec   
    }
}

  • Share/Bookmark

PowerShell Script to Open Your Gmail!!

# Ensure that you are not already signed-in Gmail!
#seeking help to improve this script

$url = "http://gmail.com"
$username="Your_username"
$password="Your_password"
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
while ($ie.Busy -eq $true)
{
    Start-Sleep -Milliseconds 1000;
}
$ie.Document.getElementById("email").value = $username
$ie.Document.getElementByID("Passwd").value=$password
$ie.Document.getElementById("signin").Click();

 

See the same on My TechNet List: http://gallery.technet.microsoft.com/ScriptCenter/en-us/69eaa0bc-c0fc-4948-bd4c-6dab8d85179e

  • Share/Bookmark

Categories

Oldies

My Tweets

Follow @ankitwww (88 followers)

My Visitors