-
Notifications
You must be signed in to change notification settings - Fork 14
/
export-illustrator.ps1
195 lines (168 loc) · 5.51 KB
/
export-illustrator.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
param([string[]]$Export = @())
# Files to export, relative to this script's directory
$files = [ordered]@{
'skin' = 'src/core/res/skin.ai';
'system' = 'src/core/res/system.ai';
'layout-grid' = 'src/core/res/layout-grid.ai';
}
# Scales to export
$scales = @(
1.00, 1.25, 1.50, 1.75,
2.00, 2.25, 2.50, 2.75,
3.00, 3.25, 3.50, 3.75,
4.00
)
# *************************************************************************
# ** !!! DO NOT EDIT BELOW THIS LINE !!! **
# *************************************************************************
$filesToExport =
if ($Export.Count -eq 0) {
$files.Values
} else {
$files[$Export]
}
if (($filesToExport.Count -eq 0) -or ($scales.Count -eq 0)) { return }
Add-Type @"
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
namespace Windower
{
public static class ComUtils
{
public static object GetActiveObject(string progId)
{
if (progId == null)
{
throw new ArgumentNullException("progId");
}
Guid clsid;
var hresult = CLSIDFromProgIDEx(progId, out clsid);
if (hresult < 0)
{
Marshal.ThrowExceptionForHR(hresult);
}
object obj;
hresult = GetActiveObject(clsid, IntPtr.Zero, out obj);
if (hresult < 0)
{
Marshal.ThrowExceptionForHR(hresult);
}
return obj;
}
[DllImport("ole32")]
private static extern int CLSIDFromProgIDEx(
[MarshalAs(UnmanagedType.LPWStr)] string lpszProgID,
out Guid lpclsid);
[DllImport("oleaut32")]
private static extern int GetActiveObject(
[MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
IntPtr pvReserved,
[MarshalAs(UnmanagedType.IUnknown)] out object ppunk);
}
}
"@
try {
$illustrator = [Windower.ComUtils]::GetActiveObject(
"Illustrator.Application")
}
catch {
Write-Host -NoNewline "Starting Adobe Illustrator... "
try {
$illustrator = New-Object -ComObject "Illustrator.Application"
Write-Host -ForegroundColor Green "OK"
} catch {
Write-Host -ForegroundColor Red "FAILED"
throw
}
$closeIllustrator = $true
}
$root = Split-Path $script:MyInvocation.MyCommand.Path
$options = New-Object -ComObject "Illustrator.ExportOptionsPNG24"
$options.antiAliasing = $true;
$options.transparency = $true;
$options.artBoardClipping = $true;
function Export-Document
{
param($document, $base)
$scales | ForEach-Object {
$scale = [int]($_ * 100)
$outFile = "$base.$scale.png"
$destination = $ExecutionContext.SessionState.Path.
GetUnresolvedProviderPathFromPSPath((Join-Path $root $outFile))
Write-Host -NoNewline "Exporting $outFile... "
try {
$options.verticalScale = $scale;
$options.horizontalScale = $scale;
$document.Export($destination, 5 <#aiPNG24#>, $options)
Write-Host -ForegroundColor Green "OK"
} catch {
Write-Host -ForegroundColor Red "FAILED"
throw
}
}
}
$filesToExport | ForEach-Object {
$file = Join-Path '.' $_
$path = Resolve-Path (Join-Path $root $file)
$base = [System.IO.Path]::Combine(
(Split-Path $file),
[System.IO.Path]::GetFileNameWithoutExtension($file))
$document = $illustrator.Documents |
Where-Object { $_.FullName -ieq $path } |
Select-Object -First 1
if ($null -eq $document) {
Write-Host -NoNewline "Opening $file... "
try {
$document = $illustrator.open($path)
$closeDocument = $true
Write-Host -ForegroundColor Green "OK"
} catch {
Write-Host -ForegroundColor Red "FAILED"
throw
}
}
$layers = $document.Layers
$stableLayers = $layers | Where-Object { $_.Name -eq '<stable>' }
$testLayers = $layers | Where-Object { $_.Name -eq '<test>' }
if ($stableLayers.Count -gt 0 -or $testLayers.Count -gt 0) {
$stableLayers | ForEach-Object { $_.Visible = $true }
$testLayers | ForEach-Object { $_.Visible = $false }
$stableBase = Join-Path `
(Join-Path (Split-Path $base) 'stable') `
(Split-Path $base -Leaf)
Export-Document $document $stableBase
$stableLayers | ForEach-Object { $_.Visible = $false }
$testLayers | ForEach-Object { $_.Visible = $true }
$testBase = Join-Path `
(Join-Path (Split-Path $base) 'test') `
(Split-Path $base -Leaf)
Export-Document $document $testBase
} else {
Export-Document $document $base
}
if ($closeDocument) {
Write-Host -NoNewline "Closing $file... "
try {
$document.Close(2 <#aiDoNotSaveChanges#>)
Write-Host -ForegroundColor Green "OK"
} catch {
Write-Host -ForegroundColor Red "FAILED"
throw
}
}
}
if ($closeIllustrator) {
Write-Host -NoNewline "Closing Adobe Illustrator... "
try {
$illustrator.Quit()
Write-Host -ForegroundColor Green "OK"
}
catch {
Write-Host -ForegroundColor Red "FAILED"
throw
}
}
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($options)
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($illustrator)
Write-Host "Export complete!"