Jump to content

ForEach vs UBound


Recommended Posts

Hi,

I just started to use Autoit today. I found Ubound but the help isn't really clear. What I want to do is install a bunch of fonts in Windows 7. Some fonts are just a different version so Windows sends a pop-up asking if you want to re-install it and I have to click "Yes" for every single font that I want replaced. Here's what's on my mind.

(From Powershell..Not Autoit) For Each ($Files in $Folder)

{

9From Autoit) WinWaitActive ( "Install Fonts", "", 5 )

Send ( "Enter" )

}

Let me know if something like that is possible and thanks in advance for your help.

Sam

Link to comment
Share on other sites

Hi Sam743,

you can use _FileListToArray to get all files in a folder into an array you can work with.

[autoit]

#Include <File.au3>

Dim $a_fonts

$a_fonts = _FileListToArray("C:\fonts", "*.ttf")

For $i = 1 To $a_fonts[0]

; Install fonts and or add more code

Next

[autoit]

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Install Fonts :

#Include <File.au3>

$_Dir = FileSelectFolder ( "Choose the folder where are fonts to install.", @DesktopDir )
If Not FileExists ( $_Dir ) Then Exit
$_Filelist = _FileListToArray ( $_Dir, "*.ttf", 1 )
If Not IsArray ( $_Filelist ) Or @Error Then Exit MsgBox ( 0, "", "No Files\Folders Found." )

For $_I = 1 To  $_Filelist[0]
    $_Installed = _InstallFont ( $_Dir & "\" & $_Filelist[$_I] )
    If Not $_Installed Then ConsoleWrite ( "Install Font of " & $_Filelist[$_I] & " : " & $_Installed & @LF )
Next

Func _InstallFont ( $sSourceFile, $sFontDescript="", $sFontsPath="" )
    Local Const $HWND_BROADCAST = 0xFFFF
    Local Const $WM_FONTCHANGE = 0x1D
    If $sFontsPath = "" Then $sFontsPath = @WindowsDir & "\fonts"
    Local $sFontName = StringRegExpReplace ( $sSourceFile, "^.*\\", "" )
    If Not FileCopy ( $sSourceFile, $sFontsPath & "\" & $sFontName, 1 ) Then Return SetError ( 1, 0, 0 )
    Local $hSearch = FileFindFirstFile ( $sSourceFile )
    Local $iFontIsWildcard = StringRegExp ( $sFontName, "\*|\?" )
    Local $aRet, $hGdi32_DllOpen = DllOpen ( "gdi32.dll" )
    If $hSearch = -1 Then Return SetError ( 2, 0, 0 )
    If $hGdi32_DllOpen = -1 Then Return SetError ( 3, 0, 0 )
    While 1
        $sFontName = FileFindNextFile ( $hSearch )
        If @error Then ExitLoop
        If $iFontIsWildcard Then $sFontDescript = StringRegExpReplace ( $sFontName, "\.[^\.]*$", "" )
        $aRet = DllCall ( $hGdi32_DllOpen, "Int", "AddFontResource", "str", $sFontsPath & "\" & $sFontName )
        If IsArray ( $aRet ) And $aRet[0] > 0 Then RegWrite ( "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", $sFontDescript, "REG_SZ", $sFontsPath & "\" & $sFontName )
    WEnd
    DllClose ( $hGdi32_DllOpen )
    DllCall ( "user32.dll", "Int", "SendMessage", "hwnd", $HWND_BROADCAST, "int", $WM_FONTCHANGE, "int", 0, "int", 0 )
    Return 1
EndFunc ; _InstallFont ( )

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

Install Fonts :

#Include <File.au3>

$_Dir = FileSelectFolder ( "Choose the folder where are fonts to install.", @DesktopDir )
If Not FileExists ( $_Dir ) Then Exit
$_Filelist = _FileListToArray ( $_Dir, "*.ttf", 1 )
If Not IsArray ( $_Filelist ) Or @Error Then Exit MsgBox ( 0, "", "No Files\Folders Found." )

For $_I = 1 To  $_Filelist[0]
    $_Installed = _InstallFont ( $_Dir & "\" & $_Filelist[$_I] )
    If Not $_Installed Then ConsoleWrite ( "Install Font of " & $_Filelist[$_I] & " : " & $_Installed & @LF )
Next

Func _InstallFont ( $sSourceFile, $sFontDescript="", $sFontsPath="" )
    Local Const $HWND_BROADCAST = 0xFFFF
    Local Const $WM_FONTCHANGE = 0x1D
    If $sFontsPath = "" Then $sFontsPath = @WindowsDir & "\fonts"
    Local $sFontName = StringRegExpReplace ( $sSourceFile, "^.*\\", "" )
    If Not FileCopy ( $sSourceFile, $sFontsPath & "\" & $sFontName, 1 ) Then Return SetError ( 1, 0, 0 )
    Local $hSearch = FileFindFirstFile ( $sSourceFile )
    Local $iFontIsWildcard = StringRegExp ( $sFontName, "\*|\?" )
    Local $aRet, $hGdi32_DllOpen = DllOpen ( "gdi32.dll" )
    If $hSearch = -1 Then Return SetError ( 2, 0, 0 )
    If $hGdi32_DllOpen = -1 Then Return SetError ( 3, 0, 0 )
    While 1
        $sFontName = FileFindNextFile ( $hSearch )
        If @error Then ExitLoop
        If $iFontIsWildcard Then $sFontDescript = StringRegExpReplace ( $sFontName, "\.[^\.]*$", "" )
        $aRet = DllCall ( $hGdi32_DllOpen, "Int", "AddFontResource", "str", $sFontsPath & "\" & $sFontName )
        If IsArray ( $aRet ) And $aRet[0] > 0 Then RegWrite ( "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", $sFontDescript, "REG_SZ", $sFontsPath & "\" & $sFontName )
    WEnd
    DllClose ( $hGdi32_DllOpen )
    DllCall ( "user32.dll", "Int", "SendMessage", "hwnd", $HWND_BROADCAST, "int", $WM_FONTCHANGE, "int", 0, "int", 0 )
    Return 1
EndFunc ; _InstallFont ( )

Thanks for your help guys. That was quick! I'll test this and report back tomorrow.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...