Skip to content

Commit ffbb437

Browse files
committed
Added document name as part of the assets folder. Also added validation so the action only runs on saved documents.
1 parent a2cb38a commit ffbb437

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

us-android-export.jsx

+37-7
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,51 @@
1313
var docRef = app.activeDocument,
1414
activeLayer = docRef.activeLayer,
1515
activeLayer2,
16-
newWidth, newHeight;
16+
newWidth,
17+
newHeight,
18+
docName = docRef.name;
1719

1820

1921
// Run main function
2022
init();
2123

2224
// The other functions
2325
function init() {
24-
saveFunc('xxhdpi');
25-
saveFunc('xhdpi');
26-
saveFunc('hdpi');
27-
saveFunc('mdpi');
28-
saveFunc('ldpi');
26+
if(!isDocumentNew()) {
27+
saveFunc('xxhdpi');
28+
saveFunc('xhdpi');
29+
saveFunc('hdpi');
30+
saveFunc('mdpi');
31+
saveFunc('ldpi');
32+
} else {
33+
alert("Please save your document before running this script.");
34+
}
35+
2936
}
3037

38+
// Test if the document is new (unsaved)
39+
// http://2.adobe-photoshop-scripting.overzone.net/determine-if-file-has-never-been-saved-in-javascript-t264.html
40+
41+
function isDocumentNew(doc){
42+
// assumes doc is the activeDocument
43+
cTID = function(s) { return app.charIDToTypeID(s); }
44+
var ref = new ActionReference();
45+
ref.putEnumerated( cTID("Dcmn"),
46+
cTID("Ordn"),
47+
cTID("Trgt") ); //activeDoc
48+
var desc = executeActionGet(ref);
49+
var rc = true;
50+
if (desc.hasKey(cTID("FilR"))) { //FileReference
51+
var path = desc.getPath(cTID("FilR"));
52+
53+
if (path) {
54+
rc = (path.absoluteURI.length == 0);
55+
}
56+
}
57+
return rc;
58+
};
59+
60+
3161
function resizeDoc(document, scale) {
3262

3363
var calcWidth = activeLayer.bounds[2] - activeLayer.bounds[0], // Get layer's width
@@ -108,7 +138,7 @@ function saveFunc(dpi) {
108138
var Name = docRef2.name.replace(/\.[^\.]+$/, ''),
109139
Ext = decodeURI(docRef2.name).replace(/^.*\./,''),
110140
Path = docRef.path,
111-
folder = Folder(Path + '/assets/' + 'drawable-' + dpi);
141+
folder = Folder(Path + '/' + docName + '-assets/' + 'drawable-' + dpi);
112142

113143
if(!folder.exists) {
114144
folder.create();

0 commit comments

Comments
 (0)