-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSave restorse workspace
133 lines (122 loc) · 4.65 KB
/
Save restorse workspace
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
// This macros let the user save all open image and text windows in a so called workspace folder
// and restore them all at once with the 'restore workspace' macro
// a 'workspaces' directory must exist and be set up using the 'workspaces settings' macro
// author [email protected]
macro "Workspaces settings" {
// declares a workspaces directory
wsdir = getDirectory(" Select or create a Workspaces directory");
call("ij.Prefs.set", "workspaces.path", wsdir);
}
macro "Save Workspace [F1]" {
line="";
// checks whether a workspaces directory has been declared
path = call("ij.Prefs.get", "workspaces.path", "nopath");
if (path=="nopath") exit ("No 'workspaces' directory set up");
// Creates the save workspace dialog
Dialog.create("Save Workspace ");
Dialog.addString ("name",tstamp()+".ijw",20);
Dialog.addCheckbox ("Close All windows", true);
Dialog.show();
wsName = Dialog.getString();
caw= Dialog.getCheckbox();
if (!endsWith(wsName,".ijw")) wsName=wsName+".ijw";
wsPath = path+wsName;
if (File.exists(wsPath)) exit("This workspace exists !\nChoose another name or \nDelete existing folder.");
File.makeDirectory(wsPath);
// start with saving non image windows.
list = getList("window.titles");
if (list.length>0) for (i=0; i<list.length; i++) {
selectWindow(list[i]);
if (list[i]=="Results") {
run("Text...", "save=["+wsPath+File.separator+"ws_results.xls]");
line = ""+line+"text;ws_results.xls;Results"+"\n"; }
else if (list[i]=="Log") {
run("Text...", "save=["+wsPath+File.separator+"log.txt]");
line = ""+line+"text;log.txt;Log"+"\n"; }
else if (list[i]=="ROI Manager") {
roiManager("Save", wsPath+File.separator+"ws_roimanager.zip");
line = ""+line+"text;ws_roimanager.zip;ROI Manager"+"\n"; }
else { // we have another type of text window, (eg macro), we can try to save it
contents = getInfo("window.contents");
name = list[i];
while (File.exists(wsPath+File.separator+name)) { name="_"+name; }
f=File.open(wsPath+File.separator+name);
print (f,contents);
c=File.close(f);
line = ""+line+"text;"+name+";"+list[i]+"\n"; }
if (caw) run ("Close");
}
// then save all image windows as tif.
ids=getIDs();
for (i=0;i<ids.length;i++){
selectImage(ids[i]);
title=getTitle();
getDimensions(width, height, channels, slices, frames);
getCursorLoc(x, y, z, modifiers);
getLocationAndSize(xw, yw, wwidth, wheight);
line = ""+line+ids[i]+";"+title+";"+channels+";"+slices+";"+frames+";"+z+";"+xw+";"+yw+";"+wwidth+";"+wheight+"\n";
// save as tif as in saveAs("Tiff", "C:\\Documents and Settings\\mutterer\\Bureau\\embryos.tif");
saveAs("Tiff", wsPath+File.separator+ids[i]+".tif");
rename(title);
if (caw) run ("Close");
}
// finally create
f=File.open(wsPath+File.separator+"content.ijw");
print (f,line);
c=File.close(f);
showStatus ("Workspace saved");
}
macro "Restore Workspace [F2]" {
// checks whether a workspaces directory has been declared
path = call("ij.Prefs.get", "workspaces.path", "nopath");
if (path=="nopath") exit ("No 'workspaces' directory found");
// Creates the save workspace dialog
Dialog.create("Restore Workspace ");
ews = getEWS(path);
if (ews.length>0) Dialog.addChoice ("existing WS",ews);
else Dialog.addMessage("- no existing workspace");
Dialog.show();
path = path+Dialog.getChoice();
// get the contents.iws file and process it.
str = File.openAsString(path+File.separator+"content.ijw");
lines=split(str,"\n");
for (i=0;i<lines.length;i++) {
e = split (lines[i],";");
if (e[0]=="text") { //restore text window
if (e[1]=="ws_roimanager.zip") { roiManager("Open", path+e[1] ); }
else if (e[1]=="ws_results.xls") { run("Results...", "openasstring...=["+path+e[1]+"]"); }
else if (e[1]=="log.txt") {
str = File.openAsString(path+File.separator+"log.txt");
print(str);
}
else { run("Edit...", "open=["+path+e[1]+"]"); }
} else if (e[0]<0) { //restore image
open (path+File.separator+e[0]+".tif");
rename (e[1]);
setLocation (e[6],e[7],e[8],e[9]);
if (e[5]>1) setSlice (e[5]);
}
}
}
// functions below
function getIDs() {
a = newArray(nImages); for (i=1;i<=nImages;i++) {selectImage(i);a[i-1]=getImageID();}
return a;
}
function getEWS(path) {
count=0; list=getFileList(path); wslist=list;
for (i=0;i<list.length;i++){ if (endsWith(list[i],".ijw/")) { wslist[count]=list[i]; count++; } }
shortlist = newArray(count);
for (i=0;i<count;i++){ shortlist[i]=wslist[i];}
return shortlist ;
}
function tstamp(){
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
month++; if (month<10) month="0"+month;
if (dayOfMonth<10) dayOfMonth="0"+dayOfMonth;
if (hour<10) hour="0"+hour;
if (minute<10) minute="0"+minute;
if (month<10) second="0"+second;
s=""+year+""+month+""+dayOfMonth+""+hour+""+minute+""+second;
return s;
}