if(app.documents.length > 0){
	var currentDocument = null;
	
	currentDocument = app.activeDocument;
	if (currentDocument)
	{
		//app.activeDocument.saveAs(File.desktopDirectory.resolvePath("test.jpg"), jpgSaveOptions, true, Extension.LOWERCASE);
		
		var docName = currentDocument.name;
		docName = docName.match(/(.*)(\.[^\.]+)/) ? docName = docName.match(/(.*)(\.[^\.]+)/):docName = [docName, docName];
		
		var file;
		try
		{
			//file = currentDocument.path
			file = new File(decodeURI(currentDocument.path)+'/'+docName[1]+'.jpg');
		}catch(e)
		{
			file = new File("~/Untitled.jpg");
		}

		result = String(file.saveDlg("Choose destination for image file", "*.jpg;*.png" ));
		if (result && result != "null")
		{
			var filePath = result;
			var extension = result.split(".");
			extension = extension[extension.length-1];			
			if (extension == "jpg")
			{
				saveJpg(currentDocument, filePath.replace(".jpg", "@2x.jpg"));
				
				//resize and save small one
				halfSizeDocument(currentDocument);
				
				saveJpg(currentDocument, filePath);
				
				resetHistory(currentDocument);
				
			}else if (extension == "png")
			{
				savePng(currentDocument, filePath.replace(".png", "@2x.png"));
				
				//resize and save small one
				halfSizeDocument(currentDocument);
				
				savePng(currentDocument, filePath);
				
				resetHistory(currentDocument);
			}else{
				alert("Please provide a valid file extension (jpg or png)");
			}
		}
	}else{
		alert("Error getting active document");
	}
}else{  
    alert("There are no open documents to save");  
}

function saveJpg(document, path)
{
	var jpgSaveOptions = new JPEGSaveOptions();
	jpgSaveOptions.quality = 9;
	document.saveAs(new File(path), jpgSaveOptions, true, Extension.LOWERCASE);
}

function savePng(document, path)
{
	var pngSaveOptions = new PNGSaveOptions(); 
	document.saveAs(new File(path), pngSaveOptions, true, Extension.LOWERCASE);
}

function halfSizeDocument(document)
{
	app.preferences.rulerUnits = Units.PERCENT;
	document.resizeImage(50, undefined, undefined, ResampleMethod.BICUBICSHARPER);
}

function resetHistory(document)
{
	document.activeHistoryState = document.historyStates[document.historyStates.length-2];
}

function hasCurrentDocumentFileReference ()
{
    var ref = new ActionReference ();
    ref.putEnumerated (stringIDToTypeID ("document"), stringIDToTypeID ("ordinal"), stringIDToTypeID ("targetEnum"));
    return executeActionGet (ref).hasKey (stringIDToTypeID ("fileReference"));
}