Create web font collection
Last modification: Sunday, February 28, 2010 04:40 amCreates a font collection with web safe fonts in Font Book.
Implementation
on run
-- Set the name for this collection
set _collectionName to "Web"
-- Determine which font families will be part of this collection
set _fontFamilies to {"Andale Mono", "Arial", "Arial Black", "Comic Sans MS", "Courier New", "Georgia", "Impact", "Tahoma", "Times New Roman", "Trebuchet MS", "Verdana", "Symbol", "Webdings", "Wingdings"}
-- Create the collection
createCollectionWithFontFamilies(_collectionName, _fontFamilies)
end run
on createCollectionWithFontFamilies(_collectionName, _fontFamilies)
-- Gather all the font families
set _foundFonts to {}
repeat with _i from 1 to count of _fontFamilies
set _familyName to item _i of _fontFamilies
set _foundFont to findMostRecentFontFamilyWithName(_familyName)
if _foundFont is not false then
set end of _foundFonts to _foundFont
end if
end repeat
-- Create the collection and add the font families
if _foundFonts is not {} then
set _collection to createFontCollection(_collectionName)
repeat with _font in _foundFonts
tell application "Font Book"
repeat 3 times
try
add _font to _collection
exit repeat
end try
delay 1
end repeat
end tell
end repeat
end if
end createCollectionWithFontFamilies
on createFontCollection(_collectionName)
(* Creates a collection in Font Book using the name
specified as an argument to this function. *)
tell application "Font Book"
(* BUGFIX: Font Book v2.2.1 creates additional unnamed
collection when making new one. *)
-- Snapshot collection names for later clean up
set _expectedCollectionNames to name of font collections
set end of _expectedCollectionNames to _collectionName
(* BUGFIX END *)
if (exists font collection _collectionName) is false then
make new font collection at the end of font collections with properties {name:_collectionName}
-- Wait until the font collection actually exists
repeat
try
get font collection _collectionName
if (exists font collection _collectionName) then exit repeat
end try
delay 0.1
end repeat
end if
(* BUGFIX: Font Book v2.2.1 creates additional unnamed
collection when making new one. *)
-- Clean up garbage collections
set _collectionNames to name of font collections
repeat with _i from 1 to count of _collectionNames
if item _i of _collectionNames is not in _expectedCollectionNames then
delete font collection (item _i of _collectionNames)
end if
end repeat
(* BUGFIX END *)
repeat
try
return font collection _collectionName
end try
delay 0.1
end repeat
end tell
end createFontCollection
on findMostRecentFontFamilyWithName(_familyName)
(* Returns the most recent font family whose name matches the argument. *)
-- Query the Font Book application for the font family
tell application "Font Book"
set _foundFontFamilies to font families whose name is _familyName
end tell
-- Nothing found, return early
if _foundFontFamilies is {} then return false
-- Initialize variables
set _mostRecentFontFamily to false
set _mostRecentVersion to 0
-- Find the most recent versions
repeat with _i from 1 to count of _foundFontFamilies
set _fontFamily to item _i of _foundFontFamilies
-- Get the path to the first file for this font family
tell application "Font Book"
set _files to files of _fontFamily
set _file to (item 1 of _files) as string
end tell
-- Ask the Finder for the version (a bit buggy)
repeat 3 times
try
tell application "Finder" to set _versionString to version of file _file
end try
if _versionString is not "" and _versionString is not missing value then
exit repeat
end if
delay 1
end repeat
-- Convert the version string into a number
set _version to stringToNumber(_versionString)
-- Keep track of the most recent font family
if _version > _mostRecentVersion then
set _mostRecentFontFamily to _fontFamily
set _mostRecentVersion to _version
end if
end repeat
return _mostRecentFontFamily
end findMostRecentFontFamilyWithName
on stringToNumber(_string)
(* Strips away all non-numerical characters
from a string and keeps only the first dot. *)
set _filteredString to ""
set _dotParsed to false
repeat with _character in characters of _string
set _asciiNum to ASCII number _character
if _asciiNum ≥ 48 and _asciiNum ≤ 57 then
set _filteredString to _filteredString & (_character as string)
else if _asciiNum = 46 and _dotParsed is false then
set _filteredString to _filteredString & (_character as string)
set _dotParsed to true
end if
end repeat
return _filteredString as real
end stringToNumber