How do I script the 'Add' command in Font Book?

Thanks to StefanK i found there was an ‘Add’ command in Font Book, but I can’t figure out a way to script it.

Can anyone finish this for me please. I’ve been sending a keypress to the Font Book ‘Install Font’ button, but it doesn’t always work correctly.


tell application "Finder"
	set theFonts to (get files of entire contents of folder "Fonts" whose kind contains "font")
	tell application "Font Book"
		activate
		repeat with anItem in theFonts
			--open anItem
			add anItem to [b](need to fix this bit)[/b]
		end repeat
	end tell
	tell application "Font Book"
		quit
	end tell
end tell

Thanks

Santa

Hi Santa,

I must apologize, Font Book has an add command, but it seems only
to add present fonts to font collections.

But there is an other way. If you move the font to /Library/Fonts and use the open command,
the font will be added to the library.
If the font will be moved to any user font folder the dialog with the button “Install Font” will appear.
In fact of postscript fonts with a font suitcase and an outline file, the suitcase must be opened to add it to Font Book.

The problem is, sometimes the dialog in Font Book opens also when the fonts are saved in /Library/Fonts.
Maybe someone has a solution for this.
Here’s the script

set fFont to alias ((path to desktop as Unicode text) & "Fonts")

tell application "Finder"
	set theFonts to (get files of entire contents of fFont whose kind contains "font")
	set nameFonts to {}
	repeat with i in theFonts
		set end of nameFonts to name of contents of i
	end repeat
	do shell script "mv -f " & quoted form of POSIX path of fFont & "* /Library/Fonts/" -- use shell script to avoid authentication
end tell

tell application "Font Book"
	activate
	repeat with anName in nameFonts
		set anItem to ((path to fonts from local domain as Unicode text) & anName)
		if kind of (info for anItem as alias) does not contain "outline" then -- filter PS outline files
			open anItem
		end if
	end repeat
end tell
-- quit  application "Font Book"

Thanks Stefan

Unfortunately I struck a few problems with your script.

The shell script moves everything to the fonts folder, whereas I need to be selective.

I tried moving the shell script inside the repeat loop and moving one at a time, but It sometimes crashed with the fonts I was using. Turns out that the fonts often open with a different name compared to the actual file name.

This seems to work, where the folder ‘Temporary Printing’ holds a mixture of fonts and other files, sometimes in sub-folders.

set fFont to alias ((path to desktop as Unicode text) & "Temporary Printing")
tell application "Finder"
	--try
	set NewFontInstalled to false
	
	set theFonts to (get files of entire contents of fFont whose kind contains "font")
	if number of items in theFonts > 0 then
		
		tell application "Font Book"
			activate
			repeat with aFont in theFonts
				open aFont
				if exists window 1 then
					tell application "System Events" to tell process "Font Book"
						click button "Install Font" of group 1 of window 1
					end tell
				end if
				if exists window 1 then
					tell application "System Events" to tell process "Font Book"
						click button 1 of window 1
					end tell
				end if
				set NewFontInstalled to true
			end repeat
		end tell
	end if
	if NewFontInstalled then
		tell application "Font Book"
			quit
		end tell
	end if
	--end try
end tell

Regards

Santa