Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Font loader from batch files or macros?


  • Please log in to reply
25 replies to this topic
bluegecko
  • Members
  • 14 posts
  • Last active: May 02 2011 01:48 AM
  • Joined: 18 Oct 2006
Thanks PhiLho

I've been busy getting confused with DllCall etc. For testing I'm using CTRL-Shift-Y to call the function:

^+y::
SendMessage, 0x1D,,,, ahk_id 0xFFFF
return

[where 0x1D is WM_FONTCHANGE; 0xFFF is HWND_BROADCAST (required)]

Font is added to the registry via a batch file. Doesn't work so far, but the screen flickers when the hotkey is pressed, so there's hope. Let's see what PostMessage does...

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
There might be a bug with broadcast message in AHK.
Every atempt I tried failed, I monitored messages via Spy and applications didn't receive Broadcast message.

You can find about that problem in bugs forum
Posted Image

bluegecko
  • Members
  • 14 posts
  • Last active: May 02 2011 01:48 AM
  • Joined: 18 Oct 2006
Bug or no bug, I doubt that explains why I'm getting nowhere - I've a lot to learn about AHK syntax.

Either of the following WM_FONTCHANGE broadcasts do seem to work, at least in so far as they flicker the screen:
PostMessage, 0x1D,,,, ahk_id 0xFFFF
SendMessage, 0x1D,,,, ahk_id 0xFFFF

Okay, I'll call it a day for now - need to get on with some work. Will log in again tomorrow morning. Many thanks everyone for the help so far.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Note that AFAIK, you don't need to broadcast this message if you install a font just for an application that you run after the install, it will see the changes on startup. The other apps won't see the fonts, but that's not a problem if I understood correctly.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

bluegecko
  • Members
  • 14 posts
  • Last active: May 02 2011 01:48 AM
  • Joined: 18 Oct 2006
I think I've got it!

The following routines seem to work, and instantly too (they update a word processor's font menu even when its running). For testing, CTRL-SHIFT-Y (install) and CTRL-SHIFT-U (uninstall) are used:

^+y::
DllCall("AddFontResource", Str, "c:\fontname.ttf")
PostMessage, 0x1D,,,, ahk_id 0xFFFF
return

^+u::
DllCall("RemoveFontResource", Str, "c:\fontname.ttf")
PostMessage, 0x1D,,,, ahk_id 0xFFFF
return

Hopefully I'm not missing something, and it really is as simple as that...

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
that is good news...
I never managed to isue Broadcast message before, but it might be due to the fact i used another mechanism that you can see here


I just checked and it seems to work your way.

Thank you for this discovery.
Posted Image

bluegecko
  • Members
  • 14 posts
  • Last active: May 02 2011 01:48 AM
  • Joined: 18 Oct 2006
It's time to wrap up this thread, then. I've got the trick down a tee for loading and unloading fonts on the fly (instantly, too - no delay) without rebooting, or mucking about with the windows font folder or system registry, so here's the rundown in case anyone else needs it. My ignorance has the advantage of keeping the coding simple:

THE PROBLEM
Some programs, such as multilingual dictionaries, require special fonts to work. These are normally installed along with the program. However, those additional fonts also appear in the font menu of word processors, which can become very cluttered. The solution, as described below, is only to load special "one program only" fonts when those programs start, and to unload the fonts when the program closes. The following method works on Windows XP SP2 Home edition, and should work on most other flavours of Windows too. The idea could easily be applied to other OS.

THE SOLUTION
The program is loaded via a batch file rather than via the program's icon/shortcut. Before triggering the program (the second line, below), the batch file sends a script to autohotkey to load the required fonts using the AddFontResource function, and broadcast the change system-wide using WM_FONTCHANGE. The program then loads and runs. Upon the program closing, the batch file continues by sending an unload script to autohotkey.

I didn't try the alternative approach of letting autohotkey detect the program launch - I'm much happier with a one-off, user-triggered solution than have autohotkey poll constantly for programs that I might not use for weeks.

Files required:

[BATCH FILE]
c:\programs\autohotkey\autohotkey.exe c:\fonts\fonts_load.ahk"
c:\programs\fonthungryprogram\whatever.exe
c:\programs\autohotkey\autohotkey.exe c:\fonts\fonts_unload.ahk"

[FONTS_LOAD.AHK]
DllCall("AddFontResource", Str, "c:\fonts\fontone.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fonttwo.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fontthree.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fontfour.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fontfive.ttf")
PostMessage, 0x1D,,,, ahk_id 0xFFFF
return

[FONTS_UNLOAD.AHK]
DllCall("RemoveFontResource", Str, "c:\fonts\fontone.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fonttwo.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fontthree.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fontfour.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fontfive.ttf")
PostMessage, 0x1D,,,, ahk_id 0xFFFF
return

Barring any major oversight, I think that's all there is to it. I'm not sure whether sending the scripts to autohotkey when it's already running is recommended, but it works, and scripts contained in autohotkey.ini don't appear to be affected (they continue to work during and after the font loading and unloading).

Thanks once again to everyone who helped out on this thread, and of course to the developers of autohotkey (thankyouthankyouthankyou!) - a really sweet piece of programming that I'm well chuffed to have come across.

BoBo
  • Guests
  • Last active:
  • Joined: --
Script undoubtfully nominated to be moved to the Scripts&Functions section! 8)

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Good, I indeed thought it was so simple, but I had no time to test this. And you learnt some bits while doing this.
Another thing you can learn: no need for three files, even less for a batch one, AutoHotkey is able to run your program:

DllCall("AddFontResource", Str, "c:\fonts\fontone.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fonttwo.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fontthree.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fontfour.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fontfive.ttf")
PostMessage, 0x1D,,,, ahk_id 0xFFFF

RunWait c:\programs\fonthungryprogram\whatever.exe

DllCall("RemoveFontResource", Str, "c:\fonts\fontone.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fonttwo.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fontthree.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fontfour.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fontfive.ttf")
PostMessage, 0x1D,,,, ahk_id 0xFFFF
As I wrote earlier, perhaps you can even get rid of the PostMessages, because the newly ran program will see them (theoritically) and the already running programs won't, which is more or less what you wanted. To test, of course.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

ZenoArrow
  • Members
  • 13 posts
  • Last active: Jan 17 2015 08:31 PM
  • Joined: 21 Aug 2014

Hi all.

 

Know this is a really old post, but I found it really useful in putting together a temporary font script, which I wanted to share...

;Script to dynamically load and unload fonts in Windows.
;Originally created to allow custom fonts on a portable Cygwin install.
;Supports TTF and OTF fonts.

;N.B. Change FontFolder depending on where you are storing the temporary fonts.
;Default FontFolder is DRIVE:\TempFonts, where DRIVE is the same as the AHK script.

#Persistent
#SingleInstance Force

FontFolder := SubStr(a_scriptDir, 1, InStr(a_scriptDir,":\") + 1) . "TempFonts"
FontExtensions := "ttf,otf"

TempFonts := Object()
Loop, %FontFolder%\*
{
	If A_LoopFileExt In %FontExtensions%
	{
	TempFonts.Insert(A_LoopFileName)
	}
}

If TempFonts.Maxindex() != ""
{
	For Index, Font In TempFonts
	{
		FileString := FontFolder . "\" . Font
		DllCall("AddFontResource", Str, FileString)
	}

	;PostMessage, 0x1D,,,, ahk_id 0xFFFF
}

; run on exit
OnExit, ExitSub
return

ExitSub:
If A_ExitReason Not In Error
{

	If TempFonts.Maxindex() != ""
	{
		For Index, Font In TempFonts
		{
			FileString := FontFolder . "\" . Font
			DllCall("RemoveFontResource", Str, FileString)
		}

		PostMessage, 0x1D,,,, ahk_id 0xFFFF
	}

}
ExitApp

This is my first AHK script, though I did have a lot of help on IRC (thanks GeekDude!). Hope it's useful.



SnowFlake_FlowSnake
  • Members
  • 845 posts
  • Last active: Jan 24 2016 05:24 PM
  • Joined: 08 Oct 2012

cool first AHK script :D


  • Download link of my scripts on Autohotkey.com 2/10/2015 [DOWNLAND]
  • Contact Info:  https://github.com/floowsnaake //  FloowSnaake(A)gmail.com
  • IF you need Help send me a PM,Email or Post on Github

  • Quote by tank  Posted 29 September 2015 - 06:14 PM

  • "Eventually i will find a way to convert the DB back to PHPBB3. but i dont have the bandwidth right now. No one that has tried has had success. It is the Only way i can keep this open is if i could successfully convert it."