open();
run("Rotate 90 Degrees Right");
outResult = getDirectory("Choose the results folder");

	name = getTitle();
	print(name);
	lengthName = lengthOf(name);
	//print(lengthName);
	index1 = indexOf(name, "_");
	//print(index1);
	index2 = indexOf(name, "_", index1+1);
	//print(index2);
	potNumber = 	substring(name, index1+1, index2);
	print("pot number is: " + potNumber);
	index4 = lastIndexOf(name, "_");
	date = "20" + substring(name, index4+1, index4+7);
	print("date is: " + date);
	time = substring(name, index4+7, index4+13);
	print("time is: " + time); 
	
	resultsTitle = name + ".txt";
	title1 = resultsTitle;
	table= "["+title1+"]";
	if(isOpen(resultsTitle) != true)
	{
        	run("New... ", "name="+table+" type=Table");
		f=table;
		print(f, "\\Headings:image\tpotNumber\tdate\ttime\tlowestLeaf\thighestLeaf\tamountOfLeaves");
	}

	waitForUser("Check the leaf rank of the lowest visible leaf collar and the highest visible leaf collar");
	Dialog.create("V-stage");
	Dialog.addNumber("Lowest visible leaf collar is: ", 0);
	Dialog.addNumber("Highest visible leaf collar is: ", 0);
	Dialog.show();
	lowLeaf = Dialog.getNumber();
	highLeaf = Dialog.getNumber();
	print("Lowest leaf: ", lowLeaf);
	print("Highest leaf: ", highLeaf);
	print("Number of leaves to measure: ", 1+highLeaf-lowLeaf);
	run("Set Measurements...", "  redirect=None decimal=3");
	setTool("point");
	waitForUser("Click and measure at soil level and at each leaf collar, going up");

	saveAs("Results", outResult + name + "xy.txt");
	
	
	

	
	g = "["+resultsTitle+"]";
	resultString = name + "\t" + potNumber + "\t" + date + "\t" + time + "\t" + lowLeaf + "\t" + highLeaf + "\t" + highLeaf-lowLeaf;
	print(g, resultString);
	selectWindow(resultsTitle);
	saveAs("Text", outResult + resultsTitle);
	run("Clear Results");
	selectWindow(name + ".txt");
	run("Close");
	selectWindow(name);
	run("Close");