Suitcase Metadata Extractor/Logger

This script uses Suitcase Fusion as a conduit to collect font information/metadata into a spreadsheet.

We use it to give font information to design agencies so they can legally buy fonts for working on our projects. I have it run nightly using cron and just make sure all our fonts are listed in Suitcase. Most of the Excel code was derived using the Record feature of AppleScript, since Excel X (10.1.5) is watchable.

--------------------------------------------------------------------------------
--
-- Font Metadata Extract via Suitcase
-- by Kevin Quosig
--
--
-- Uses Suitcase to pull basic font metadata about all fonts
-- that it has access to and save to an Excel spreadsheet
-- that can be provided to vendor partners so they can but
-- any fonts they need to do work for us
--
--------------------------------------------------------------------------------
--
-- DECLARE PROPERTIES
--
-- debugging/logging on?
property g_debug : true

--debugging names and paths
property g_log_file_name : "LogFile.txt"
property g_log_file_path : "LogsPath:"

--system paths
property g_desktop_folder_path : path to desktop folder

--file names and paths
property g_filename : ("My_Fonts" & "_") as text
property g_export_path : "ExportPath:"

--
-- UTILITY HANDLERS
--

--Log Entry Generation
-- With help from StephanK of MacScripter
-- http://bbs.applescript.net/viewtopic.php?pid=76607#p76607
--
on logMe(log_string, indent_level)
	if g_debug is true then --allows turning the debugger on and off so my logMe's can be left in final version
		set log_target to (g_log_file_path & g_log_file_name) as text
		try
			set log_file_ref to open for access file log_target with write permission
			repeat indent_level times
				write tab to log_file_ref starting at eof
			end repeat
			write ((log_string as text) & return) to log_file_ref starting at eof
			close access log_file_ref
			return true
		on error
			try
				close access file log_target
			end try
			return false
		end try
	end if
end logMe

-- YYMMDD Datestamp Generator
-- with help from StefanK of MacScripter
-- http://bbs.applescript.net/viewtopic.php?id=20420
--
on dateStamp()
	-- Load date components from system
	tell (current date)
		set dayStamp to day
		set monthStamp to (its month as integer)
		set yearStamp to year
	end tell
	
	--coerce components to two-digit form
	set dayStamp to (text -2 thru -1 of ("0" & dayStamp as text))
	set monthStamp to (text -2 thru -1 of ("0" & monthStamp as text))
	set yearStamp to (text 3 thru 4 of (yearStamp as text))
	
	--Assemble datestamp
	return yearStamp & monthStamp & dayStamp as text
end dateStamp

-- File Writer
-- Built with help from MacScripter. For details see:
-- http://bbs.applescript.net/viewtopic.php?pid=76721
--
on write_to_disk(write_me, write_where)
	set file_reference to open for access file write_where with write permission
	try
		set eof of file_reference to 0 -- erase the file
		write write_me to file_reference
		close access file_reference
	on error error_info
		close access file_reference
		display dialog error_info
	end try
end write_to_disk


--
-- MAIN SCRIPT
--
--get datestamp
set date_stamp to my dateStamp()

--write to central automation log
--
my logMe("---", 1)
my logMe("Font Metadata Extract via Suitecase Start--" & (current date), 1)

--get font metadata from Suitcase Fusion
--
tell application "Suitcase Fusion"
	
	-- Built with help from MacScripter. For details see:
	-- http://bbs.applescript.net/viewtopic.php?pid=76721
	--
	
	--get list of all fonts and their metadata
	
	script get_fonts
		property all_fonts : every Font
	end script
	
	set data_extract to ""
	
	--loop through list and load variable
	repeat with current_font in get_fonts's all_fonts
		tell contents of current_font
			set data_extract to data_extract & (name & tab & family & tab & foundry & tab & font type & tab & "'" & version & return)
		end tell
	end repeat
	
end tell

--write to temp file
set g_filename to (g_filename & date_stamp) as text
set temp_file_path to (g_desktop_folder_path & g_filename & ".txt") as text
my write_to_disk(data_extract, temp_file_path)

--write data to Excel
--
tell application "Microsoft Excel"
	
	--open text dump
	Open temp_file_path
	
	--sort data
	Sort Selection Key1 Range "R1C1" Order1 xlAscending Header xlGuess OrderCustom 1 Orientation xlTopToBottom without MatchCase
	
	--format data
	Select Range "C1"
	set NumberFormat of Selection to "@"
	Select Range "C2"
	set NumberFormat of Selection to "@"
	
	--fix data (Suitcase sometimes does not give plain-English metadata)
	Replace every Cell What "«constant ****FTOP»" ReplaceWith "OpenType" LookAt xlWhole SearchBy xlByRows without MatchCase
	Replace every Cell What "«constant ****FTMP»" ReplaceWith "Multiple Master" LookAt xlWhole SearchBy xlByRows without MatchCase
	
	--add policy
	Select Range "R1"
	Insert Selection Shift xlDown
	Insert Selection Shift xlDown
	Insert Selection Shift xlDown
	Insert Selection Shift xlDown
	Insert Selection Shift xlDown
	Select Range "R1C1"
	set FormulaR1C1 of ActiveCell to "I had some disclaimer copy for our department here."
	Select Range "R2C1"
	set FormulaR1C1 of ActiveCell to "I left these dummy lines in so you can see how to do"
	Select Range "R3C1"
	set FormulaR1C1 of ActiveCell to "text manipulation with Excel. ;)"
	
	--add title
	Select Range "R1"
	Insert Selection Shift xlDown
	Insert Selection Shift xlDown
	Select Range "R1C1"
	set Bold of Font of Selection to true
	set Size of Font of Selection to 18.0
	set FormulaR1C1 of ActiveCell to "My Fonts Metadata List"
	
	--add column headers
	Select Range "R7C1:R7C5"
	set ColorIndex of Font of Selection to 2.0
	set ColorIndex of Interior of Selection to 1
	set Pattern of Interior of Selection to xlSolid
	set Bold of Font of Selection to true
	Select Range "R7C1"
	set FormulaR1C1 of ActiveCell to "NAME"
	Select Range "R7C2"
	set FormulaR1C1 of ActiveCell to "FAMILY"
	Select Range "R7C3"
	set FormulaR1C1 of ActiveCell to "FOUNDRY"
	Select Range "R7C4"
	set FormulaR1C1 of ActiveCell to "TYPE"
	Select Range "R7C5"
	set FormulaR1C1 of ActiveCell to "VERSION"
	Select Range "R7C6"
	
	--size columns
	set ColumnWidth of Range "C1" to 33
	set ColumnWidth of Range "C2" to 33
	set ColumnWidth of Range "C3" to 33
	set ColumnWidth of Range "C4" to 14
	set ColumnWidth of Range "C5" to 12
	
	--set page setup
	set LeftMargin of PageSetup of ActiveSheet to 36.0
	set RightMargin of PageSetup of ActiveSheet to 36.0
	set TopMargin of PageSetup of ActiveSheet to 36.0
	set BottomMargin of PageSetup of ActiveSheet to 36.0
	set Zoom of PageSetup of ActiveSheet to 50
	
	--save and close file
	set xls_file_path to (g_export_path & g_filename & ".xls") as text
	Save ActiveWorkbook In (xls_file_path) As xlNormal
	Close ActiveWorkbook
end tell

--clean-up temp file
--
tell application "Finder"
	delete file temp_file_path
end tell

--finish automation log
--
my logMe("Font Metadata Extract via Suitecase End--" & (current date), 1)
my logMe("---", 1)