Find selected text in Dialog Memo box

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Dominic_Fichera
Pro Scripter
Posts: 82
Joined: Mon Mar 24, 2014 12:15 pm

Find selected text in Dialog Memo box

Post by Dominic_Fichera » Fri Sep 04, 2015 6:10 am

Hey all,

I've put together a realtime HTML editor kind of thing, not for any other reason than to practice some coding. Here's the snippet if anyone wants it.

// COMPILE_OPTS|D:\Users\d692908\Documents\Macro Scheduler 14\website.exe||CONSOLE=0|INCLUDES=1||RUNTIMES=1|BMPS=1
Let>FileSaved=False
Dialog>MainMenu
object MainMenu: TForm
Left = 3533
Top = 152
HelpContext = 5000
BorderIcons = [biSystemMenu, biMaximize]
Caption = 'Web-Preview'
ClientHeight = 337
ClientWidth = 680
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
Menu = MainMenu
OldCreateOrder = True
Position = poScreenCenter
ShowHint = True
OnTaskBar = False
DesignSize = (
680
337)
PixelsPerInch = 96
TextHeight = 13
object HTML: tMSMemo
Left = 8
Top = 8
Width = 289
Height = 321
Text = '<!DOCTYPE html>'#13#10'<html>'#13#10'<body>'#13#10#13#10'</body>'#13#10'</html>'
ScrollBars = ssBoth
Anchors = [akLeft, akTop, akBottom]
TabOrder = 0
end
object Website: tMSHTMLViewer
Left = 304
Top = 8
Width = 368
Height = 317
TabOrder = 1
Anchors = [akLeft, akTop, akRight, akBottom]
BorderStyle = htFocused
DefFontName = 'Times New Roman'
DefPreFontName = 'Courier New'
HistoryMaxCount = 0
NoSelect = False
PrintMarginBottom = 2.000000000000000000
PrintMarginLeft = 2.000000000000000000
PrintMarginRight = 2.000000000000000000
PrintMarginTop = 2.000000000000000000
PrintScale = 1.000000000000000000
end
object Browse: tMSButton
Left = 323
Top = 225
Width = 75
Height = 25
Caption = 'Browse'
TabOrder = 2
Visible = False
DoBrowse = False
InitialDir = 'C:\Users\%USER_NAME%\Desktop'
Filter = 'HTML Files (*.html) | *.HTML|All Files (*.*) | *.*'
BrowseStyle = fbSave
end
object Open: tMSButton
Left = 421
Top = 237
Width = 75
Height = 25
Caption = 'Open'
TabOrder = 3
Visible = False
DoBrowse = False
Filter = 'HTML Files (*.html) | *.HTML|All Files (*.*) | *.*'
BrowseStyle = fbOpen
end
object MainMenu: tMSMainMenu
object MenuItem1: tMSMenuItem
Caption = '&File'
object OpenMenu: tMSMenuItem
Caption = '&Open'
end
object SaveAs: tMSMenuItem
Caption = '&Save As'
end
object Close: tMSMenuItem
Caption = '&Close'
end
end
end
end
EndDialog>MainMenu

AddDialogHandler>MainMenu,HTML,OnChange,doCreateWebsite
AddDialogHandler>MainMenu,,onClose,doClose
AddDialogHandler>MainMenu,Close,onClick,doClose
AddDialogHandler>MainMenu,SaveAs,onClick,doSaveAs
AddDialogHandler>MainMenu,OpenMenu,onClick,doOpen
Show>MainMenu
Label>IdleLoop
Wait>.001
Goto>IdleLoop
LabelToVar>HTML1,varDefaultHTML
/*
HTML1:
<!DOCTYPE html>
<html>
<body>

</body>
</html>
*/
SRT>doCreateWebsite
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
SetDialogProperty>MainMenu,Website,HTML,varWebsiteText
If>FileSaved=True
SetDialogProperty>MainMenu,,Caption,Web-Preview - %varFilename%*
Endif
END>doCreateWebsite
SRT>doClose
Exit>0
END>doClose
SRT>doSaveAs
SetDialogProperty>MainMenu,Browse,Filename,*.html
SetDialogProperty>MainMenu,Browse,doBrowse,True
GetDialogProperty>MainMenu,Browse,Filename,varFilename
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetDialogProperty>MainMenu,,Caption,varWindowName
Position>html,varFilename,1,varHTMlExists,TRUE
If>varHTMlExists<1
ConCat>varFilename,.html
Endif
/*
IfFileExists>%varFilename%
Ask>%varFilename% already exists. Would you like to overwrite?,varFileExists
If>varFileExists=YES
DeleteFile>%varFilename%
WriteLn>%varFilename%,nWLNRes,varWebsiteText
Let>FileSaved=True
SetDialogProperty>MainMenu,,Caption,%varWindowName% - %varFilename%
Endif
Else
WriteLn>%varFilename%,nWLNRes,varWebsiteText
Let>FileSaved=True
SetDialogProperty>MainMenu,,Caption,%varWindowName% - %varFilename%
Endif
*/
IfFileExists>%varFilename%
DeleteFile>%varFilename%
Endif
WriteLn>%varFilename%,nWLNRes,varWebsiteText
Let>FileSaved=True
SetDialogProperty>MainMenu,,Caption,Web-Preview - %varFilename%
END>doSaveAs
SRT>doOpen
SetDialogProperty>MainMenu,Open,doBrowse,True
GetDialogProperty>MainMenu,Open,Filename,varFilename
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
ReadFile>varFilename,varHTML
If>varHTML=##NOFILE##
SetDialogProperty>MainMenu,HTML,Text,varWebsiteText
Else
SetDialogProperty>MainMenu,HTML,Text,varHTML
SetDialogProperty>MainMenu,,Caption,Web-Preview - %varFilename%
Endif
END>doOpen

  View Snippet Page

Does anyone know a way to find the position of the text cursor in a memo box or even the highlighted text? I want to add a feature that would be similar to when you post on the forum - You highlight the text you want, click "insert colour" or something similar, and then the selected text would be surrounded by the required code.

Before: Hello world
After: <font color="red">Hello world</font>

Hope I've made this clear, but if not, let me know :)

Thanks in advance,

Dominic Fichera

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Find selected text in Dialog Memo box

Post by Grovkillen » Fri Sep 04, 2015 7:48 am

I would do a cut (ctrl + x) and then get the clipboard text, add surounding code, put the new text to clipboard, then do a paste (ctrl + v). This happens so fast that the user wouldn't notice it at all.
Let>ME=%Script%

Running: 15.0.27
version history

Dominic_Fichera
Pro Scripter
Posts: 82
Joined: Mon Mar 24, 2014 12:15 pm

Re: Find selected text in Dialog Memo box

Post by Dominic_Fichera » Mon Sep 07, 2015 2:42 am

Hey Grovkillen,

Thanks for the response :)

The only problem with doing this is if I click a button to launch the SRT, it would take focus away from the memo box... and without focus on the memo box, it's not going to cut (CTRL+X) the highlighted text...

Perhaps is there a way to focus a dialog and THEN focus a particular object in that dialog? If there is such a thing, the cut would work :)

Let>FileSaved=False
Dialog>MainMenu
object MainMenu: TForm
Left = 3645
Top = 452
HelpContext = 5000
BorderIcons = [biSystemMenu, biMaximize]
Caption = 'Web-Preview'
ClientHeight = 337
ClientWidth = 634
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
Menu = MainMenu
OldCreateOrder = True
Position = poScreenCenter
ShowHint = True
OnTaskBar = False
DesignSize = (
634
337)
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 8
Top = 0
Width = 618
Height = 41
Anchors = [akLeft, akTop, akRight]
BevelOuter = bvNone
TabOrder = 4
object Bold: tMSButton
Left = 8
Top = 8
Width = 25
Height = 25
Caption = 'B'
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'Calibri'
Font.Style = [fsBold]
ParentFont = False
TabOrder = 0
DoBrowse = False
BrowseStyle = fbOpen
end
object Red: tMSButton
Left = 104
Top = 8
Width = 57
Height = 25
Caption = 'Red'
TabOrder = 3
DoBrowse = False
BrowseStyle = fbOpen
end
object Purple: tMSButton
Left = 488
Top = 8
Width = 57
Height = 25
Caption = 'Purple'
TabOrder = 9
DoBrowse = False
BrowseStyle = fbOpen
end
object Blue: tMSButton
Left = 424
Top = 8
Width = 57
Height = 25
Caption = 'Blue'
TabOrder = 8
DoBrowse = False
BrowseStyle = fbOpen
end
object Cyan: tMSButton
Left = 360
Top = 8
Width = 57
Height = 25
Caption = 'Cyan'
TabOrder = 7
DoBrowse = False
BrowseStyle = fbOpen
end
object Green: tMSButton
Left = 296
Top = 8
Width = 57
Height = 25
Caption = 'Green'
TabOrder = 6
DoBrowse = False
BrowseStyle = fbOpen
end
object Yellow: tMSButton
Left = 232
Top = 8
Width = 57
Height = 25
Caption = 'Yellow'
TabOrder = 5
DoBrowse = False
BrowseStyle = fbOpen
end
object Orange: tMSButton
Left = 168
Top = 8
Width = 57
Height = 25
Caption = 'Orange'
TabOrder = 4
DoBrowse = False
BrowseStyle = fbOpen
end
object Italics: tMSButton
Left = 72
Top = 8
Width = 25
Height = 25
Caption = 'I'
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'Calibri'
Font.Style = [fsItalic]
ParentFont = False
TabOrder = 2
DoBrowse = False
BrowseStyle = fbOpen
end
object Underline: tMSButton
Left = 40
Top = 8
Width = 25
Height = 25
Caption = 'U'
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'Calibri'
Font.Style = [fsUnderline]
ParentFont = False
TabOrder = 1
DoBrowse = False
BrowseStyle = fbOpen
end
object Black: tMSButton
Left = 488
Top = 8
Width = 57
Height = 25
Caption = 'Black'
TabOrder = 10
DoBrowse = False
BrowseStyle = fbOpen
end
object White: tMSButton
Left = 552
Top = 8
Width = 57
Height = 25
Caption = 'White'
TabOrder = 11
DoBrowse = False
BrowseStyle = fbOpen
end
end
object HTML: tMSMemo
Left = 8
Top = 40
Width = 289
Height = 289
Text = '<!DOCTYPE html>'#13#10'<html>'#13#10'<body>'#13#10#13#10'</body>'#13#10'</html>'
ScrollBars = ssBoth
Anchors = [akLeft, akTop, akBottom]
TabOrder = 0
end
object Website: tMSHTMLViewer
Left = 304
Top = 40
Width = 322
Height = 285
TabOrder = 1
Anchors = [akLeft, akTop, akRight, akBottom]
BorderStyle = htFocused
DefFontName = 'Times New Roman'
DefPreFontName = 'Courier New'
HistoryMaxCount = 0
NoSelect = False
PrintMarginBottom = 2.000000000000000000
PrintMarginLeft = 2.000000000000000000
PrintMarginRight = 2.000000000000000000
PrintMarginTop = 2.000000000000000000
PrintScale = 1.000000000000000000
end
object Browse: tMSButton
Left = 312
Top = 312
Width = 75
Height = 25
Caption = 'Browse'
TabOrder = 3
Visible = False
DoBrowse = False
InitialDir = 'D:\Users\%USER_NAME%\Desktop'
Filter = 'HTML Files (*.html) | *.HTML|All Files (*.*) | *.*'
BrowseStyle = fbSave
end
object Open: tMSButton
Left = 392
Top = 312
Width = 75
Height = 25
Caption = 'Open'
TabOrder = 2
Visible = False
DoBrowse = False
Filter = 'HTML Files (*.html) | *.HTML|All Files (*.*) | *.*'
BrowseStyle = fbOpen
end
object MainMenu: tMSMainMenu
object MenuItem1: tMSMenuItem
Caption = '&File'
object OpenMenu: tMSMenuItem
Caption = '&Open'
end
object SaveAs: tMSMenuItem
Caption = '&Save As'
end
object Close: tMSMenuItem
Caption = '&Close'
end
end
end
end
EndDialog>MainMenu

AddDialogHandler>MainMenu,HTML,OnChange,doCreateWebsite
AddDialogHandler>MainMenu,,onClose,doClose
AddDialogHandler>MainMenu,Close,onClick,doClose
AddDialogHandler>MainMenu,SaveAs,onClick,doSaveAs
AddDialogHandler>MainMenu,OpenMenu,onClick,doOpen
AddDialogHandler>MainMenu,Bold,onClick,doBold
AddDialogHandler>MainMenu,Underline,onClick,doUnderline
AddDialogHandler>MainMenu,Italics,onClick,doItalics
AddDialogHandler>MainMenu,Red,onClick,doRed
AddDialogHandler>MainMenu,Orange,onClick,doOrange
AddDialogHandler>MainMenu,Yellow,onClick,doYellow
AddDialogHandler>MainMenu,Green,onClick,doGreen
AddDialogHandler>MainMenu,Cyan,onClick,doCyan
AddDialogHandler>MainMenu,Blue,onClick,doBlue
AddDialogHandler>MainMenu,Purple,onClick,doPurple
AddDialogHandler>MainMenu,Black,onClick,doBlack
AddDialogHandler>MainMenu,White,onClick,doWhite

Show>MainMenu
Label>IdleLoop
Wait>.001
Goto>IdleLoop
LabelToVar>HTML1,varDefaultHTML
/*
HTML1:
<!DOCTYPE html>
<html>
<body>

</body>
</html>
*/
SRT>doCreateWebsite
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
SetDialogProperty>MainMenu,Website,HTML,varWebsiteText
If>FileSaved=True
SetDialogProperty>MainMenu,,Caption,Web-Preview - %varFilename%*
Endif
END>doCreateWebsite
SRT>doClose
Exit>0
END>doClose
SRT>doSaveAs
SetDialogProperty>MainMenu,Browse,Filename,*.html
SetDialogProperty>MainMenu,Browse,doBrowse,True
GetDialogProperty>MainMenu,Browse,Filename,varFilename
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetDialogProperty>MainMenu,,Caption,varWindowName
Position>html,varFilename,1,varHTMlExists,TRUE
If>varHTMlExists<1
ConCat>varFilename,.html
Endif
/*
IfFileExists>%varFilename%
Ask>%varFilename% already exists. Would you like to overwrite?,varFileExists
If>varFileExists=YES
DeleteFile>%varFilename%
WriteLn>%varFilename%,nWLNRes,varWebsiteText
Let>FileSaved=True
SetDialogProperty>MainMenu,,Caption,%varWindowName% - %varFilename%
Endif
Else
WriteLn>%varFilename%,nWLNRes,varWebsiteText
Let>FileSaved=True
SetDialogProperty>MainMenu,,Caption,%varWindowName% - %varFilename%
Endif
*/
IfFileExists>%varFilename%
DeleteFile>%varFilename%
Endif
WriteLn>%varFilename%,nWLNRes,varWebsiteText
Let>FileSaved=True
SetDialogProperty>MainMenu,,Caption,Web-Preview - %varFilename%
END>doSaveAs
SRT>doOpen
SetDialogProperty>MainMenu,Open,doBrowse,True
GetDialogProperty>MainMenu,Open,Filename,varFilename
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
ReadFile>varFilename,varHTML
If>varHTML=##NOFILE##
SetDialogProperty>MainMenu,HTML,Text,varWebsiteText
Else
SetDialogProperty>MainMenu,HTML,Text,varHTML
SetDialogProperty>MainMenu,Website,HTML,varHTML
SetDialogProperty>MainMenu,,Caption,Web-Preview - %varFilename%
Endif
END>doOpen

SRT>doBold
SetDialogProperty>MainMenu,HTML,Default,True
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<b>%varOriginalText%</b>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doBold
SRT>doUnderline
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<u>%varOriginalText%</u>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doUnderline
SRT>doItalics
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<i>%varOriginalText%</i>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doItalics
SRT>doRed
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<font color="Red">%varOriginalText%</font>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doRed
SRT>doOrange
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<font color="Orange">%varOriginalText%</font>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doOrange
SRT>doYellow
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<font color="Yellow">%varOriginalText%</font>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doYellow
SRT>doGreen
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<font color="Green">%varOriginalText%</font>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doGreen
SRT>doCyan
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<font color="Cyan">%varOriginalText%</font>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doCyan
SRT>doBlue
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<font color="Blue">%varOriginalText%</font>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doBlue
SRT>doPurple
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<font color="Purple">%varOriginalText%</font>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doPurple
SRT>doBlack
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<font color="Black">%varOriginalText%</font>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doBlack
SRT>doWhite
GetDialogProperty>MainMenu,HTML,Text,varWebsiteText
GetClipboard>varOriginalText
Let>varNewText=<font color="White">%varOriginalText%</font>
StringReplace>varWebsiteText,varOriginalText,varNewText,varNewText
SetDialogProperty>MainMenu,HTML,Text,varNewText
SetDialogProperty>MainMenu,Website,HTML,varNewText
END>doWhite

  View Snippet Page

I've added some changes to the script that put in the "font changing" buttons to show you what I mean by losing focus.

Currently the script works if you manually copy the text you want to change and click the button... So, getting there slowly!

Thanks,

Dominic Fichera

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Find selected text in Dialog Memo box

Post by hagchr » Mon Sep 07, 2015 5:19 am

Hi, you can get the focus back by using:

Code: Select all

SetDialogObjectFocus>Dialog Name, Object Name

Dominic_Fichera
Pro Scripter
Posts: 82
Joined: Mon Mar 24, 2014 12:15 pm

Re: Find selected text in Dialog Memo box

Post by Dominic_Fichera » Mon Sep 07, 2015 5:26 am

That's perfect! Thanks hagchr!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts