在FileMaker中运行获取活跃字体的AppleScript时出错求解决
FileMaker内AppleScript获取系统字体列表的修复方案
问题描述
原代码在AppleScript脚本编辑器中可正常返回活跃字体列表,但在FileMaker Pro 19.6.3.302(MacOS Ventura 13.3.1)中运行时抛出错误:Can't make {availableFontFamilies of sharedFontManager of NSFontManager} into type string。
报错原因
FileMaker的AppleScript执行环境对Cocoa框架返回的NSString对象数组处理逻辑和原生AppleScript编辑器存在差异:直接将availableFontFamilies返回的数组转为AppleScript列表后,尝试整体转字符串时会触发类型转换失败,因为数组内的元素仍是Cocoa对象而非原生AppleScript字符串。
修复方案
方案一:逐个转换Cocoa字符串为AppleScript原生字符串后拼接
use framework "AppKit" -- 先在FileMaker上下文外处理Cocoa对象,避免环境差异影响 set fontFamilyNames to (current application's NSFontManager's sharedFontManager's availableFontFamilies) as list -- 逐个转换为AppleScript原生字符串 set nativeFontNames to {} repeat with fontName in fontFamilyNames set end of nativeFontNames to fontName as string end repeat tell application "FileMaker Pro" set AppleScript's text item delimiters to return set fontList to nativeFontNames as string set data cell "scriptResult" of table "globals" to fontList end tell
方案二:使用Cocoa原生方法直接拼接字符串(更高效)
利用NSString的componentsJoinedByString:方法直接将Cocoa字符串数组拼接为单个字符串,跳过AppleScript的类型转换步骤:
use framework "AppKit" -- 直接用Cocoa方法拼接字体列表为换行分隔的字符串 set fontList to (current application's NSFontManager's sharedFontManager's availableFontFamilies()'s componentsJoinedByString:return) as string tell application "FileMaker Pro" set data cell "scriptResult" of table "globals" to fontList end tell
说明
两种方案都规避了FileMaker环境中Cocoa对象到AppleScript字符串的批量转换问题:
- 方案一通过遍历逐个转换,确保每个元素都是AppleScript原生字符串,再进行拼接;
- 方案二直接借助Cocoa框架的字符串拼接能力,减少跨环境类型转换的风险,执行效率更高。
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

