fmotion1
fmotion1

Reputation: 531

I need to pass a powershell variable to a to a FontForge "Script String" (-c) but can't. Need help invoking FontForge with a dynamic variable

Here is my code:

$CurrentFile = $_
$DestPath = "FontForge Export"

if(!(Test-Path -LiteralPath $DestPath -PathType Container)){
    New-Item -Path $DestPath -ItemType Directory -Force | Out-Null
}

fontforge -lang=ff -c 'Open($1); SelectAll(); UnlinkReference(); Export("FontForge Export/%n-%e.svg");' $CurrentFile

No matter what I do, I cannot get the fontforge call to accept a powershell variable in the Export("FontForge Export/%n-%e.svg"); block. The above code works because I've defined a hard-coded directory name ("FontForge Export"). But as soon as I try something like this:

$CurrentFile = $_
$FullNoExt = [IO.Path]::GetFileNameWithoutExtension($CurrentFile)
$DestFolder =  "$FullNoExt FontForge Export"

if(!(Test-Path -LiteralPath $DestFolder -PathType Container)){
    New-Item -Path $DestFolder -ItemType Directory -Force | Out-Null
}

fontforge -lang=ff -c "Open(`$1); SelectAll(); UnlinkReference(); Export("$DestFolder/%n-%e.svg");" $CurrentFile

I get en error:

Couldn't find a font file named C:/Users/[username]/Desktop/Test/Adobe CC Acumin Pro/Acumin Pro Black FontForge Export/%n-%e.svg);
The requested file, %n-%e.svg);, does not exist
Open: Failed to open: Acumin Pro Black FontForge Export/%n-%e.svg);
Called from...
 <command-string>: line 1

I need a dynamic output folder from FontForge so I can run the script in parallel over several typefaces.

No matter what I do I just can't finesse the correct syntax.

Can someone help?

I really need some fresh eyes.

Adding some more information:

Tried using the format operator with no success either.

$cmdd = "$DestFolder/%n-%e.svg"
fontforge -lang=ff -c 'Open($1); SelectAll(); UnlinkReference(); Export({0});' $CurrentFile -f $cmdd

Upvotes: 1

Views: 74

Answers (1)

mklement0
mklement0

Reputation: 440526

Fundamentally:

  • PowerShell, up to v7.2.x (including Windows PowerShell) is broken with respect to passing arguments with embedded " characters to external programs, necessitating manual \-escaping

  • This has been fixed in v7.3+, but with selective exceptions on Windows.


"Open(`$1); SelectAll(); UnlinkReference(); Export("$DestFolder/%n-%e.svg");"

What your attempt lacks is escaping of the embedded " characters in the argument that is overall enclosed in "..."; inside "...", PowerShell requires embedded " chars. to be escaped as `" (or ""):

# Sufficient in v7.3+
"Open(`$1); SelectAll(); UnlinkReference(); Export(`"$DestFolder/%n-%e.svg`");"

# In v7.2-, additional \-escaping is needed:
"Open(`$1); SelectAll(); UnlinkReference(); Export(\`"$DestFolder/%n-%e.svg\`");"

While an -f-based approach would work too, it requires the entire -f operation to be enclosed in (...):

# Sufficient in v7.3+
('Open($1); SelectAll(); UnlinkReference(); Export("{0}/%n-%e.svg");' -f $DestFolder)

# In v7.2-, additional \-escaping is needed:
('Open($1); SelectAll(); UnlinkReference(); Export(\"{0}/%n-%e.svg\");' -f $DestFolder)

Upvotes: 0

Related Questions