forked from ThirdPartyNinjas/GlitchAssets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecursiveExportFile.jsfl
196 lines (160 loc) · 4.74 KB
/
RecursiveExportFile.jsfl
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
196
var outputScaleFactor = 2;
fl.outputPanel.clear();
var inputFolderURI = fl.browseForFolderURL('Select input folder');
inputFolderURI = inputFolderURI.replace(/%20/g, " ");
var outputFolderURI = fl.browseForFolderURL('Select output folder');
outputFolderURI = outputFolderURI.replace(/%20/g, " ");
searchFla(inputFolderURI, exportFileAsPng);
// searchFla function from: http://hawaiiantime.jp/blog/44/
function searchFla(dir, func)
{
var fileList = FLfile.listFolder(dir);
var len = fileList.length;
var uri;
var attr;
for (var i = 0; i < len; i++)
{
// make a file path
uri = decodeURI(dir + "/" + fileList[i]);
// check it's not a .svn file
if (FLfile.exists(uri) && uri.indexOf(".svn") == -1)
{
// get the file's info
attr = FLfile.getAttributes(uri);
// if it's a directory...
if (attr && (attr.indexOf("D") != -1))
{
// recursion
searchFla(uri, func);
}
// if it's a flash file
else if (uri.indexOf(".fla") != -1)
{
// execute
func(uri);
}
}
}
}
function exportFileAsPng(input)
{
var outputFile = input.replace(inputFolderURI, outputFolderURI);
outputFile = outputFile.replace(".fla", ".png");
verifyPathExists(outputFile);
var document = fl.openDocument(input);
try
{
document.width *= outputScaleFactor;
document.height *= outputScaleFactor;
document.selectAll();
document.scaleSelection(outputScaleFactor, outputScaleFactor, "top left");
document.exportPNG(outputFile, true, true);
}
catch(error)
{
fl.trace("Error: " + error.message + " File: " + input);
}
fl.closeDocument(document, false);
}
function exportFileAsPieces(input)
{
var outputFile = input.replace(inputFolderURI, outputFolderURI);
var outputFolder = outputFile.slice(0, -4);
verifyPathExists(outputFile);
var document = fl.openDocument(input);
try
{
if(document.library.items && document.library.items.length > 0)
{
var newDocument = fl.createDocument();
for(i in document.library.items)
{
var item = document.library.items[i];
if(item.itemType == "movie clip" || item.itemType == "graphic" || item.itemType == "bitmap")
{
var itemName = item.name.split('.')[0];
var exportPath = removeBrackets(outputFolder + "/" + itemName + ".png");
verifyPathExists(exportPath);
newDocument.width = document.width * outputScaleFactor;
newDocument.height = document.height * outputScaleFactor;
newDocument.addItem({x:0.0, y:0.0}, item);
newDocument.library.selectItem(item.name, false);
newDocument.scaleSelection(outputScaleFactor, outputScaleFactor, "top left");
if(item.itemType == "movie clip")
newDocument.exportInstanceToPNGSequence(exportPath);
else
newDocument.exportPNG(exportPath, true, false);
newDocument.selectAll();
newDocument.deleteSelection();
}
}
fl.closeDocument(newDocument, false);
}
}
catch(error)
{
fl.trace("Error: " + error.message + " File: " + input);
}
fl.closeDocument(document, false);
}
function convertFlashToPng(input)
{
var outputFile = input.replace(inputFolderURI, outputFolderURI);
var outputFolder = outputFile.slice(0, -4);
//try
//{
fl.closeAll(false);
var oldDoc = fl.openDocument(input);
var oldDom = fl.getDocumentDOM();
if(oldDom.library.items && oldDom.library.items.length > 0)
{
var newDoc = fl.createDocument();
var newDom = fl.getDocumentDOM();
for(i in oldDom.library.items)
{
var currentItem = oldDom.library.items[i];
exportItemAsPng(currentItem, outputScaleFactor, outputFolder, newDoc, newDom);
}
fl.closeDocument(newDoc, false);
}
verifyPathExists(outputFile);
oldDoc.selectAll();
oldDoc.scaleSelection(outputScaleFactor, outputScaleFactor);
oldDom.exportPNG(outputFile, true, false);
fl.closeDocument(oldDoc, false);
//}
//catch(error)
//{
// fl.trace("Error processing file: " + input);
// fl.trace(error);
// }
}
function exportItemAsPng(item, scaleFactor, outputFolder, document, dom)
{
var itemName = item.name.split('.')[0];
var exportPath = outputFolder + "/" + itemName + ".png";
document.addItem({x:0.0, y:0.0}, item);
dom.library.selectItem(item.name, false);
document.scaleSelection(scaleFactor, scaleFactor);
var tempPath = removeBrackets(exportPath);
verifyPathExists(tempPath);
if(item.itemType == "movie clip")
document.exportInstanceToPNGSequence(tempPath);
else
document.exportPNG(tempPath, true, false);
document.selectAll();
document.deleteSelection();
}
function verifyPathExists(path)
{
FLfile.createFolder(path.substring(0, path.lastIndexOf("/")));
}
function removeBrackets(path)
{
var temp = path.replace(/</g, '');
temp = temp.replace(/>/g, '');
return temp;
}