Jump to content


Photo

Scriptlet Library v4


  • Please log in to reply
76 replies to this topic

#1 Rajat

Rajat
  • Members
  • 1886 posts

Posted 24 February 2005 - 12:49 PM

Another script whose mommy is Mrs Necessity.

This is to store all those li'l functions or snips (or Scriptlets as i like to call 'em) that you use often in other scripts.
I've never really been a fan of #Include because most of the times u've to modify the scriptlets a li'l to adapt it to current use. This is the way to go!

Revision: toralf made many changes to my original script and his version is much more feature-rich. So i've updated this post to direct to his script instead:

Posted Image

Download

#2 Chris

Chris
  • Administrators
  • 10727 posts

Posted 24 February 2005 - 02:33 PM

Very nice script. Thanks for sharing it.

#3 polyethene

polyethene

    Administrator

  • Administrators
  • 5473 posts

Posted 24 February 2005 - 03:53 PM

I got to try it out, it's fantastic!
Thanks for sharing. :)

#4 Rajat

Rajat
  • Members
  • 1886 posts

Posted 23 April 2005 - 11:25 AM

a li'l change: added TAB key support to editor window.

#5 corrupt

corrupt
  • Members
  • 2558 posts

Posted 23 April 2005 - 04:30 PM

Very handy. Thanks for sharing :).

#6 Rajat

Rajat
  • Members
  • 1886 posts

Posted 23 April 2005 - 06:42 PM

this will come in even more handy with the new function feature! call it Function Library if u like!

#7 tester

tester
  • Guests

Posted 16 July 2005 - 11:44 AM

if 2 item have name:
abc abc
abcabc

this 2 item above will have the same content.

#8 tester

tester
  • Guests

Posted 16 July 2005 - 11:46 AM

WinName = Scriptlet Library



SetWorkingDir, %A_ScriptDir%



;loading settings

IniRead, PosX, Library.ini, Settings, PosX, 100

IniRead, PosY, Library.ini, Settings, PosY, 100

IniRead, SizeW, Library.ini, Settings, SizeW, 600

IniRead, SizeH, Library.ini, Settings, SizeH, 405

IniRead, LastUsed, Library.ini, Settings, LastUsed, --





;reading scriptlets

FileRead, ScriptFile, Library.ini



Loop, Parse, ScriptFile, `n

{

	IfInString, A_LoopField, <---Start_

		StartFile = 1

	

	IfNotEqual, StartFile, 1, Continue

	

	;from here the lines which contain scripts are parsed one by one

	

	;scriptlet started

	IfInString, A_LoopField, <---Start_

	{

		StringReplace, ItemName, A_LoopField, <---Start_,

		StringReplace, ItemName, ItemName, --->,

		StringReplace, ItemName, ItemName, `n,, A

		StringReplace, ItemName, ItemName, `r,, A

		ItemList = %ItemList%|%ItemName%

		ItemRunning = 1

		Continue

	}

	

	;the current scriptlet ends here

	IfInString, A_LoopField, <---End_

	{

		ItemRunning = 0

		Continue

	}



	;the current scriptlet is getting parsed

	IfEqual, ItemRunning, 1

	{

		StringReplace, NoSpaceName, ItemName, %A_Space%,_Space_, A

		StringTrimRight, CurrScriptlet, %NoSpaceName%_Script, 0

		%NoSpaceName%_Script = %CurrScriptlet%`n%A_LoopField%

	}

}



StringTrimLeft, ItemList, ItemList, 1

Sort, ItemList, D|





;removing extra carriage returns from scriptlets

Loop, parse, ItemList, |

{

	StringReplace, NoSpaceName, A_LoopField, %A_Space%,_Space_, A

	StringTrimLeft, %NoSpaceName%_Script, %NoSpaceName%_Script, 1

	StringRight, Test, %NoSpaceName%_Script, 2

	

	IfEqual, Test, `r`n	

		StringTrimRight, %NoSpaceName%_Script, %NoSpaceName%_Script, 2

}

	

	





;creating window and controls



Gui, +Resize

Gui, Color,, 0xE9F5F0

Gui, Add, ListBox, x2 y37 w130 h342 vSelItem gSelector, %ItemList%

Gui, Add, Button, x196 y7 w50 h20, &Save

Gui, Add, Button, x136 y7 w50 h20, &Copy

Gui, Add, Button, x26 y7 w20 h20, +

Gui, Add, Button, x66 y7 w20 h20, --



Gui, Font, , Courier

Gui, Add, Edit, x132 y37 w460 h342 Multi vEditData,



Gui, Show, x%PosX% y%PosY% h0 w0, %WinName%

WinMove, %WinName%,,,, %SizeW%, %SizeH%



;last selection

Control, ChooseString, %LastUsed%, ListBox1, %WinName%



;resize settings

WinGetPos,,, WinW, WinH, %WinName%

ControlGetPos,,, C1W, C1H, ListBox1, %WinName%

ControlGetPos,,, C2W, C2H, Edit1, %WinName%



Diff2W := WinW - C2W





Return







Selector:

	Gui, Submit, NoHide

	StringReplace, NoSpaceName, SelItem, %A_Space%,_Space_, A

	CurrData := %NoSpaceName%_Script

	GuiControl,, Edit1, %CurrData%

Return

	





Button+:

	InputBox, ItemName, Enter Scriptlet Name:,,, 200, 90

	IfNotEqual, ErrorLevel, 0, Return

	IfEqual, ItemName,, Return

	ControlGetText, CurrData, Edit1, %WinName%

	FileAppend, <---Start_%ItemName%--->`n%CurrData%`n<---End_%ItemName%--->`n, Library.ini

	

	StringReplace, NoSpaceName, ItemName, %A_Space%,_Space_, A

	%NoSpaceName%_Script = %CurrData%

	ItemList = %ItemList%|%ItemName%

	Sort, ItemList, D|

	GuiControl,, ListBox1, |%ItemList%

Return







Button--:

	MsgBox, 4, Delete Scriptlet?, Please confirm deletion of current scriptlet.

	IfMsgBox, Yes

	{

		Gui, Submit, NoHide

		ItemList = |%ItemList%|

		StringReplace, ItemList, ItemList, |%SelItem%|,|, A

		StringTrimLeft, ItemList, ItemList, 1

		StringTrimRight, ItemList, ItemList, 1

	}

	

	GuiControl,, ListBox1, |%ItemList%

Return





ButtonSave:

	Gui, Submit, NoHide

	ControlGetText, CurrData, Edit1, %WinName%

	StringReplace, NoSpaceName, SelItem, %A_Space%,_Space_, A

	%NoSpaceName%_Script = %CurrData%

Return





ButtonCopy:

	ControlGetText, Clipboard, Edit1, %WinName%

Return	

	



~Esc::

	IfWinNotActive, %WinName%,, Return

GuiClose:

	FileDelete, Library.ini

	WinGetPos, PosX, PosY, SizeW, SizeH, %WinName%

	IniWrite, %PosX%, Library.ini, Settings, PosX

	IniWrite, %PosY%, Library.ini, Settings, PosY

	IniWrite, %SizeW%, Library.ini, Settings, SizeW

	IniWrite, %SizeH%, Library.ini, Settings, SizeH

	IniWrite, %SelItem%, Library.ini, Settings, LastUsed

	

	FileAppend, `r`n, Library.ini

	

	ControlGet, ItemList, List,, ListBox1, %WinName%

		

	Loop, Parse, ItemList, `n

	{

		IfEqual, A_LoopField,, Continue

		StringReplace, NoSpaceName, A_LoopField, %A_Space%,_Space_, A

		

		StringTrimRight, CurrScriptlet, %NoSpaceName%_Script, 0

		FileAppend, `r`n<---Start_%A_LoopField%--->`r`n%CurrScriptlet%`r`n<---End_%A_LoopField%--->`r`n, Library.ini

	}

	

	ExitApp

Return





GuiSize:

	;function adapted from jonny's clip.ahk. thanx jonny!

	new_w := (A_GuiWidth - 5) 

	new_h := (A_GuiHeight - 39) 

	field_y := (A_GuiHeight - 40) 

	new_w := (new_w / 4) 

	guicontrol,move,SelItem,w%new_w% h%field_y% 

	new_x := (new_w + 5) 

	new_w := (new_w * 3) 

	guicontrol,move,EditData,w%new_w% h%field_y% x%new_x% 

Return



	

Tab::

	WinGetTitle, ATitle, A

	ControlGetFocus, ACtrl, A

	

	OtherWin = 1

	IfEqual, ATitle, %WinName%

		IfEqual, ACtrl, Edit1

		{

			Send, ^{Tab}

			OtherWin = 0

		}

	

	IfEqual, OtherWin, 1

		Send, {Tab}

Return


#9 Rajat

Rajat
  • Members
  • 1886 posts

Posted 16 July 2005 - 03:16 PM

that's true because the data of an item is saved in the var 'itemname_Script' and a varname doesn't allow a space in it. i know a way to fix that, but as this is a very little problem it might be a while till i get to it.

#10 Guests

  • Guests

Posted 17 July 2005 - 08:14 PM

Rajat, your imagination amazes me sometimes. You have posted some awesome scripts here that I have come to depend on and use daily.

Thank you....

#11 toralf

toralf
  • Fellows
  • 3948 posts

Posted 22 July 2005 - 09:00 AM

Dear Rajat,

I have had a closer look at the script, I like the way the scriplets are stored in the INI.
I decided to make a mod.
1) ListBox is replaced with ListView
2) Names of scriplets can be any string without any limit
3) Names like "1 2" and "12" are regarded different
4) I was able t o reduce the number of lines to 102 (coming from 188)[Ok, I also removed some comments and empty lines]

Would you allow me to incorporate this script in a larger software I plan to code?

Edit 05-07-25:
- I changed some var names
- added sort after add, new item gets selected
- right mouse click rolls the window up
- add some shortcuts
- it now has 139 lines :(

WinNameGui1 = Scriptlet Library v2
;requires AHK 1.0.37

Gui, 1:+Resize
Gui, 1:Add, Button, Section gBtnAdd, &Add
Gui, 1:Add, Button, ys vBtnDelete gBtnDelete Disabled, &Del
Gui, 1:Add, Button, x+100 ys vBtnSave gBtnSave Disabled, &Save changes
Gui, 1:Add, Button, ys gBtnCopy, Copy to &Clipboard
Gui, 1:Add, ListView, xs Section w100 h200 -Multi AltSubmit vLsbNames gLsbNames, Scriptlet Name|Internal ID
Internal_ID = 0
Loop, Read, %A_ScriptName%.ini
    {
        If A_LoopReadLine Contains <---Start_
            {
                Internal_ID++
                StringReplace, Name, A_LoopReadLine, <---Start_,
                LV_Add("",Name,Internal_ID)
                ItemInProcess := True
                Script%Internal_ID% =
            }
        Else If A_LoopReadLine Contains <---End_,
            {
                ItemInProcess := False
                StringTrimRight, Script%Internal_ID%, Script%Internal_ID%, 1
            }
        Else If ItemInProcess
                Script%Internal_ID% := Script%Internal_ID% . A_LoopReadLine . "`n"
    }
Gui, 1:Font, , Courier
Gui, 1:Add, Edit, ys w200 h200 Multi T8 vEditData,
IniRead, GuiSize1, %A_ScriptName%.ini, Settings, GuiSize1, %A_Space%
Gui, 1:Show, %GuiSize1%, %WinNameGui1%
WinGet, Gui1UniqueID, ID, %WinNameGui1%
GuiRolledUp := False
Return

$Rbutton::
  MouseGetPos, , , GuiID
  If ( Gui1UniqueID = GuiID )
    {
      If GuiRolledUp
        {
          GuiControl, 1:Move, LsbNames, h%LsbNamesH%
          Gui, 1:Show, AutoSize
        }
      Else
        {
          GuiControlGet, LsbNames, Pos, LsbNames
          WinMove, ahk_id %Gui1UniqueID%,,,,, 30
        }
      GuiRolledUp := not GuiRolledUp
    }
  Else
      Send, {Rbutton}
return

LsbNames:
    LV_GetText(ID, LV_GetNext(), 2)
    If ( ID <> "Internal ID" )
      {
        GuiControl,1:, EditData, % Script%ID%  ;%
        GuiControl,1: Enable, BtnDelete
        GuiControl,1: Enable, BtnSave
      }
    Else
      {
        GuiControl,1:, EditData
        GuiControl,1: Disable, BtnDelete
        GuiControl,1: Disable, BtnSave
      }
Return

BtnAdd:
    Gui, 1:+OwnDialogs
    Gui, 1:Submit, NoHide
    InputBox, Name, Enter Scriptlet Name:,,, 200, 90
    IfNotEqual, ErrorLevel, 0, Return
    IfEqual, Name,, Return
    Gui, 1:Submit, NoHide
    Internal_ID++
    LV_Add("Select",Name,Internal_ID)
    LV_ModifyCol(1, "Sort")
    Script%Internal_ID% = %EditData%
Return

BtnDelete:
    RowNumber := LV_GetNext()
    LV_GetText(Name, RowNumber, 1)
    MsgBox, 4, Delete Scriptlet?, Please confirm deletion`nof current scriptlet:`n%Name%
    IfMsgBox, Yes
        {
            LV_GetText(ID, RowNumber, 2)
            Script%ID% =
            LV_Delete(RowNumber)
        }
Return

BtnSave:
    Gui, 1:Submit, NoHide
    LV_GetText(ID, LV_GetNext(), 2)
    Script%ID% = %EditData%
Return

BtnCopy:
    Gui, 1:Submit, NoHide
    Clipboard = %EditData%
Return

GuiEscape:
GuiClose:
    If GuiRolledUp
        {
          GuiControl, 1:Move, LsbNames, h%LsbNamesH%
          Gui, 1:Show, AutoSize
        }
    FileDelete, %A_ScriptName%.ini
    WinGetPos, PosX, PosY, SizeW, SizeH, %WinNameGui1%
    IniWrite, x%PosX% y%PosY% w%SizeW% h%SizeH%, %A_ScriptName%.ini, Settings, GuiSize1
    FileAppend, `n`n, %A_ScriptName%.ini
    Loop, % LV_GetCount()  ;%
        {
            LV_GetText(Name, A_Index, 1)
            LV_GetText(ID, A_Index, 2)
            Script := Script%ID%
            FileAppend, <---Start_%Name%`n%Script%`n<---End_%Name%`n`n`n, %A_ScriptName%.ini
        }
    ExitApp
Return

GuiSize:
    new_w := (A_GuiWidth - 30 ) / 3
    new_h := A_GuiHeight - 41
    GuiControl, 1:Move, LsbNames, w%new_w% h%new_h%
    LV_ModifyCol(1, new_w)
    LV_ModifyCol(2, 0)
    new_x := new_w + 20
    new_w := new_w * 2
    GuiControl, 1:Move, EditData, w%new_w% h%new_h% x%new_x%
Return


#12 Rajat

Rajat
  • Members
  • 1886 posts

Posted 22 July 2005 - 02:56 PM

that's nice work toralf! may i suggest incorporating sorting to your script.. that'd be nice i guess.

Would you allow me to incorporate this script in a larger software I plan to code?

sure... please go ahead with that!

#13 toralf

toralf
  • Fellows
  • 3948 posts

Posted 22 July 2005 - 03:25 PM

What kind of sort do you want?
If you click on the column header of the listview it sorts the names ascending or descending. When you then close the gui, it will be saved in that order.

#14 Rajat

Rajat
  • Members
  • 1886 posts

Posted 22 July 2005 - 03:44 PM

i should have clarified... i meant auto-sorting after adding a new item.

#15 Laszlo

Laszlo
  • Fellows
  • 4713 posts

Posted 23 July 2005 - 01:29 AM

Beaut!