|
13 | 13 | var docRef = app.activeDocument,
|
14 | 14 | activeLayer = docRef.activeLayer,
|
15 | 15 | activeLayer2,
|
16 |
| - newWidth, newHeight; |
| 16 | + newWidth, |
| 17 | + newHeight, |
| 18 | + docName = docRef.name; |
17 | 19 |
|
18 | 20 |
|
19 | 21 | // Run main function
|
20 | 22 | init();
|
21 | 23 |
|
22 | 24 | // The other functions
|
23 | 25 | 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 | + |
29 | 36 | }
|
30 | 37 |
|
| 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 | + |
31 | 61 | function resizeDoc(document, scale) {
|
32 | 62 |
|
33 | 63 | var calcWidth = activeLayer.bounds[2] - activeLayer.bounds[0], // Get layer's width
|
@@ -108,7 +138,7 @@ function saveFunc(dpi) {
|
108 | 138 | var Name = docRef2.name.replace(/\.[^\.]+$/, ''),
|
109 | 139 | Ext = decodeURI(docRef2.name).replace(/^.*\./,''),
|
110 | 140 | Path = docRef.path,
|
111 |
| - folder = Folder(Path + '/assets/' + 'drawable-' + dpi); |
| 141 | + folder = Folder(Path + '/' + docName + '-assets/' + 'drawable-' + dpi); |
112 | 142 |
|
113 | 143 | if(!folder.exists) {
|
114 | 144 | folder.create();
|
|
0 commit comments