Autohotkey.com Topic
AhkScript Topic
DOWNLOAD

StealFunc is a function-oriented script (tool) to extract only the required functions from an ahk library . It recursively processes the library file to only extract functions that are minimally needed.
From v0.1 , it has feature to scan a autohotkey script snippet and extract the foreign functions that are used in that script from another given script.

The script is presented as a function.

return_script := stealFunc(function_list, function_file, is_list)
;EXAMPLE
Clipboard := stealFunc("Gdip_Startup`nGdip_SetBitmaptoClipboard`nGdip_CreateBitmapFromFile`nGdip_DisposeImage", "path_to_gdip_lib", 1)

The first parameter 'function_list' stores a list of functions or a working ahk script snippet whose used function are to be extracted.
The second parameter 'function_file' is the file from which functions are going to be extracted.
The third parameter 'is_list' should be 1 if 'function_list' has a list of functions or 0 otherwise.

The script also has a gui added for user convenience and can be commented out anytime.
Below are the screenshots of the GUI in action.



function_list

As said above, function_list can be a list OR a valid snippet of an Autohotkey script.

EXAMPLE for function_list as a list -

t =
(
Gdip_Startup
Gdip_CreateBitmapFromFile
Gdip_DisposeImage
Gdip_shutdown
gdip_setbitmaptoclipboard
)
output := stealFunc(t, "S:\Portables\AutoHotkey\My Scripts\ClipStep\lib\Gdip_All.ahk", 1) 	; t is a list so is_list=1
gui, add, edit, h600 w800 +multi, % output
gui, show

EXAMPLE for function_list as a snippet -
;Gdip_SetImagetoClipboard()
;Sets some Image to Clipboard

Gdip_SetImagetoClipboard( pImage ){
  ;Sets some Image file to Clipboard
  PToken := Gdip_Startup()
  pBitmap := Gdip_CreateBitmapFromFile(pImage)
  Gdip_SetBitmaptoClipboard(pBitmap)
  Gdip_DisposeImage( pBitmap )
  Gdip_Shutdown( PToken)
}

;Gdip_CaptureClipboard()
; Captures Clipboard to file

Gdip_CaptureClipboard(file, quality){
  PToken := Gdip_Startup()
  pBitmap := Gdip_CreateBitmapFromClipboard()
  Gdip_SaveBitmaptoFile(pBitmap, file, quality)
  Gdip_DisposeImage( pBitmap )
  Gdip_Shutdown( PToken)
}

The above valid code can serve as a input in the function_list parameter when you change the 3rd parameter is_list to 0.
The stealFunc function scans through the snippet and lists the used User-defined functions. It then finds them in the Input script file (here the Gdip.ahk) and copies them out if they are found. It rescans and rescans all the gdip.ahk functions so that the functions they are dependent on are also extracted.
The Snippet can be any valid AHK Script and the stealFunc function will always scan it and find the used functions in the Input script file.

Example 2

Using the richEdit library, I am running the following code.
Gui, +LastFound
hwnd := WinExist()
hRichEdit := RichEdit_Add(hwnd, 5, 5, 200, 300)
Gui, Show, w210 h310
return

With stealFunc, you can extract only the function from the lib which are required to run RichEdit_Add. So the code goes -

t =
(
Gui, +LastFound
hwnd := WinExist()
hRichEdit := RichEdit_Add(hwnd, 5, 5, 200, 300)
Gui, Show, w210 h310
return
)
output := stealFunc(t, "C:\Users\Avi\Desktop\RichEdit.ahk", 0)  ; is_list = 0
gui, add, edit, h600 w800 +multi, % output
gui, show

The output you get is of 74 lines, much less than 2100 lines.

Example 3

t =
(
  curvolume := va_getmastervolume()
  va_setmastervolume(100.0)
  SAPI.Speak(Speech)
  va_setmastervolume(curvolume)
)

Using VA (VistaAudio), the following code steals the minimal function needed for the above code to work.

output := stealFunc(t, "S:\Portables\AutoHotkey\My Scripts\Comuntiy Packages\VA-2.2\VA.ahk", 0)  ; not list so is_list=0
gui, add, edit, h600 w800 +multi, % output
gui, show

The output is 158 lined, certainly better than 880 lines.



Comments and bugs are appreciated!