【发布时间】:2019-08-11 13:00:13
【问题描述】:
在this answer 中,它显示您可以通过查看 app.fonts 属性找到 Photoshop 可用的字体。但当然,这在 After Effects 中不起作用,因为 Adobe。
有没有办法列出可用字体,以便我可以编写一个让用户选择字体的脚本?
【问题讨论】:
标签: fonts adobe extendscript after-effects
在this answer 中,它显示您可以通过查看 app.fonts 属性找到 Photoshop 可用的字体。但当然,这在 After Effects 中不起作用,因为 Adobe。
有没有办法列出可用字体,以便我可以编写一个让用户选择字体的脚本?
【问题讨论】:
标签: fonts adobe extendscript after-effects
查看 AE 脚本参考,AE 似乎没有像 PS 和 AI 那样访问字体集合的方法......话虽如此,我想出了一个使用 After Effects 的 PC 解决方法system.callSystem() 方法将字体收集过程传递给 PowerShell,并使用 AE 中的 ScriptUI 提示用户进行选择。有关 ScriptUI 的更多信息,请参阅 Peter Kahrel here 撰写的文档。
这是一个工作示例,但仅在运行 Windows 10 的 PC 上的 AE CC2019 中进行了测试。在 AE 中,您需要确保在 Allow Scripts to Write Files and Access Network 下的选项i>Preferences > Scripting & Expressions 已启用。
创建一个名为 getFonts.ps1 的 powershell 文件并将其保存到您的桌面(或任何您喜欢的地方,只需确保使用新位置更新 jsx 文件)。然后复制粘贴以下代码:
[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')
$fontList = (New-Object System.Drawing.Text.InstalledFontCollection)
# save a file to the desktop with a list of all the fonts
$fontFile = "~/Desktop/fonts.txt"
# since we use Add-Content, we are appending to the file.
# Delete the file on run if it exists so we don't continue appending to the list
if (Test-Path $fontFile) {
Remove-Item $fontFile
}
# loop through the collection and write each font name to fonts.txt on the desktop
for ($i = 0; $i -lt $fontList.Families.Length; $i++) {
# $fontObjs.add($fontList.Families[$i].Name)
$fontNames = $fontList.Families[$i].Name
Add-Content $fontFile "$fontNames"
}
创建一个名为 textFonts.jsx 的新 jsx 文件。复制并粘贴以下内容:
// powershell file location
var pathToPs1File = "~/Desktop/getFonts.ps1"
// execute powershell file
var fonts = system.callSystem("Powershell.exe -ExecutionPolicy Bypass " + pathToPs1File)
// Give the powershell script some time (3 seconds in this case) to write all the font names
// it may need more time if you have 1000s of fonts, adjust as needed
$.sleep(3000)
// function to parse through the fonts pulled from the text file
// will return array of font names for ScriptUI
function getAllFonts(fontsFromFile) {
fontsFromFile = fontsFromFile.split("\n");
var fontListForScriptUI = []
for (i = 0; i < fontsFromFile.length; i++) {
if (!fontsFromFile[i]) {
continue;
}
else {
fontListForScriptUI.push(fontsFromFile[i])
}
}
return fontListForScriptUI;
}
// Script UI will return the *name* of the font chosen.
function main() {
//surpress error dialogs
app.beginSuppressDialogs()
var scriptVersion = 1.0;
var fontFile = File("~/Desktop/fonts.txt");
fontFile.open("e")
var fontList = fontFile.read();
fontFile.close();
var allFonts = getAllFonts(fontList);
//////////////////////////////////////////////////////////////////////////
//
// Options Dialog
//
//////////////////////////////////////////////////////////////////////////
var options = new Window('dialog', 'Test Script ' + scriptVersion);
options.alignChildren = ['fill', 'top'];
options.graphics.font = ScriptUI.newFont ("Segoe UI", "Regular", 14);
if (app.version == "13.0.1") { // if its CS6, font color is dark, otherwise font color is light
options.graphics.foregroundColor = options.graphics.newPen (options.graphics.PenType.SOLID_COLOR, [0.2, 0.2, 0.2], 1);
}
else {
options.graphics.foregroundColor = options.graphics.newPen (options.graphics.PenType.SOLID_COLOR, [1,1,1,], 1);
}
//////////////////////////////////////////////////////////////////////////
// List Font Names
//////////////////////////////////////////////////////////////////////////
var groupOptions = options.add('panel', undefined, 'Font Picker');
groupOptions.orientation = 'column';
groupOptions.alignChildren = 'left';
groupOptions.margins = 30;
groupOptions.indent = 30;
groupOptions.graphics.font = ScriptUI.newFont ("Segoe UI", "Regular", 14);
groupOptions.add('statictext', undefined, 'System Fonts:');
var fontList = groupOptions.add('dropdownlist', undefined, allFonts);
fontList.preferredSize.width = 300;
//////////////////////////////////////////////////////////////////////////
// OK & Cancel Buttons
//////////////////////////////////////////////////////////////////////////
var btns = options.add('group {alignment: "right" }');
btns.orientation = 'row';
var okButton = btns.add('button', undefined, 'OK', { name: 'ok' });
var canButton = btns.add('button', undefined, 'Cancel', { name: 'cancel' });
var myResult = options.show();
if (myResult == 2) {
// on cancel, alert the user and exit the script
alert("Operation Canceled!");
exit(0);
}
options.close();
return fontList.selection.text
}
// store the returned value for later as pickedFont
var pickedFont = main();
alert(pickedFont);
最后,在AE中运行jsx文件。
【讨论】:
[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing'); (convertTo-json(New-Object System.Drawing.Text.InstalledFontCollection))>$env:temp\fontlist.txt