5 public partial class Scripting : Form
7 public static string log;
8 public CodeView view =
null;
9 public static string ImageJPath = Properties.Settings.Default.ImageJPath;
11 public static void LogLine(
string s)
13 log += s + Environment.NewLine;
15 public static Dictionary<string, Script> Scripts =
new Dictionary<string, Script>();
20 public string scriptString;
21 public dynamic script;
23 public string output =
"";
24 public bool done =
false;
25 public static List<string> usings =
new List<string>();
26 public Exception ex =
null;
28 public ScriptType type = ScriptType.script;
30 public Script(
string file,
string scriptStr)
32 name = Path.GetFileName(file);
33 scriptString = scriptStr;
34 if (file.EndsWith(
".txt") || file.EndsWith(
".ijm"))
35 type = ScriptType.imagej;
41 name = Path.GetFileName(file);
42 scriptString = File.ReadAllText(file);
44 if (file.EndsWith(
".txt") || file.EndsWith(
".ijm"))
45 type = ScriptType.imagej;
54 Thread t =
new Thread(
new ThreadStart(RunScript));
57 private static string scriptName =
"";
58 private static string str =
"";
60 private static void RunScript()
62 Script rn = Scripts[scriptName];
64 if (rn.type == ScriptType.imagej)
82 rn.script = CSScript.Evaluator.LoadCode(rn.scriptString);
83 rn.obj = rn.script.Load();
84 rn.output = rn.obj.ToString();
104 @"//css_reference " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name +
@".dll
106 using System.Windows.Forms;
107 using System.Drawing;
108 using System.Threading;
110 foreach (
string s
in usings)
112 loader += usings + Environment.NewLine;
123 dynamic script = CSScript.Evaluator.LoadCode(loader);
124 return script.Load();
128 MessageBox.Show(e.Message, e.Source);
135 if (!Scripts.ContainsKey(name))
136 Scripts.Add(name,
this);
137 scriptName = this.name;
138 thread =
new Thread(
new ThreadStart(RunScript));
155 return name.ToString() +
", " + thread.ThreadState.ToString();
158 return name.ToString();
189 st.type = Event.Down;
204 st.type = Event.Move;
217 st.type = Event.None;
219 st.buts = MouseButtons.None;
224 public MouseButtons buts;
225 public bool processed =
false;
226 public override string ToString()
228 return type.ToString() +
" ,(" + p.X.ToString() +
", " + p.Y.ToString() +
"), " + buts.ToString();
239 public enum ScriptType
245 private static State state;
267 if (s.p.X == state.p.X && s.p.Y == state.p.Y && s.type == state.type)
269 state.processed =
true;
278 string st = Application.StartupPath;
279 foreach (
string file
in Directory.GetFiles(st +
"/Scripts"))
281 if (!Scripts.ContainsKey(Path.GetFileName(file)))
285 ListViewItem lv =
new ListViewItem();
288 scriptView.Items.Add(lv);
289 Scripts.Add(lv.Text, sc);
292 foreach (
string file
in Directory.GetFiles(st +
"/Tools"))
294 if (file.EndsWith(
".cs"))
296 if (!Scripts.ContainsKey(Path.GetFileName(file)))
300 sc.type = ScriptType.tool;
301 ListViewItem lv =
new ListViewItem();
304 scriptView.Items.Add(lv);
305 Scripts.Add(lv.Text, sc);
314 foreach (ListViewItem item
in scriptView.SelectedItems)
319 outputBox.Text = s.output;
322 string[] sps = s.ex.Message.Split(
'>');
324 for (
int i = 1; i < sps.Length; i++)
326 ListViewItem er =
new ListViewItem(sps[i]);
328 errorView.Items.Add(er);
333 foreach (ListViewItem item
in scriptView.SelectedItems)
341 if (logBox.SelectionStart != logBox.Text.Length)
343 logBox.SelectionStart = logBox.Text.Length;
344 logBox.ScrollToCaret();
348 private RichTextBox textBox;
353 if (!Directory.Exists(Application.StartupPath +
"//" +
"Scripts"))
354 Directory.CreateDirectory(Application.StartupPath +
"//" +
"Scripts");
355 if (!Directory.Exists(Application.StartupPath +
"//" +
"Tools"))
356 Directory.CreateDirectory(Application.StartupPath +
"//" +
"Tools");
357 InitializeComponent();
358 scriptView.MultiSelect =
false;
362 codeview.Dock = DockStyle.Fill;
363 scriptLabel.Text =
"NewScript.cs";
365 textBox = codeview.TextBox;
366 splitContainer.Panel1.Controls.Add(codeview);
374 Scripts.Add(sc.name, sc);
384 Scripts.Add(sc.name, sc);
389 if(!Scripts.ContainsKey(sc.name))
390 Scripts.Add(sc.name, sc);
408 if (scriptView.SelectedItems.Count == 0)
410 foreach (ListViewItem item
in scriptView.SelectedItems)
414 sc.scriptString = textBox.Text;
417 outputBox.Text = sc.output;
426 if (scriptView.SelectedItems.Count == 0)
428 foreach (ListViewItem item
in scriptView.SelectedItems)
447 private void scriptView_MouseDoubleClick(
object sender, MouseEventArgs e)
456 private void runToolStripMenuItem_Click(
object sender, EventArgs e)
465 private void openScriptFolderToolStripMenuItem_Click(
object sender, EventArgs e)
467 string path = Application.StartupPath +
"\\Scripts";
468 System.Diagnostics.Process.Start(
"explorer.exe", path);
475 private void refreshToolStripMenuItem_Click(
object sender, EventArgs e)
485 private void timer_Tick(
object sender, EventArgs e)
487 if (this.WindowState != FormWindowState.Minimized)
497 private void ScriptRunner_FormClosing(
object sender, FormClosingEventArgs e)
500 this.WindowState = FormWindowState.Minimized;
510 private void scriptView_SelectedIndexChanged(
object sender, EventArgs e)
512 if (scriptView.SelectedItems.Count == 0)
514 ListViewItem item = scriptView.SelectedItems[0];
515 Script s = (Script)item.Tag;
516 textBox.Text = s.scriptString;
517 scriptLabel.Text = s.name;
524 private void runButton_Click(
object sender, EventArgs e)
526 if (scriptLabel.Text.EndsWith(
".ijm"))
528 ImageJ.RunString(textBox.Text, ImageView.SelectedImage.ID, headlessBox.Checked);
540 private void scriptLoadBut_Click(
object sender, EventArgs e)
542 openFileDialog.InitialDirectory = Application.StartupPath +
"\\Scripts";
543 if (openFileDialog.ShowDialog() != DialogResult.OK)
545 Script script =
new Script(openFileDialog.FileName, File.ReadAllText(openFileDialog.FileName));
546 script.name = Path.GetFileName(openFileDialog.FileName);
547 textBox.Text = script.scriptString;
548 scriptLabel.Text = Path.GetFileName(openFileDialog.FileName);
549 ListViewItem item =
new ListViewItem();
551 item.Text = script.name;
552 scriptView.Items.Add(item);
564 private void saveButton_Click(
object sender, EventArgs e)
566 saveFileDialog.InitialDirectory = Application.StartupPath +
"\\Scripts";
567 saveFileDialog.FileName = scriptLabel.Text;
568 if (saveFileDialog.ShowDialog() != DialogResult.OK)
570 scriptLabel.Text = Path.GetFileName(saveFileDialog.FileName);
571 File.WriteAllText(saveFileDialog.FileName, textBox.Text);
578 private void stopBut_Click(
object sender, EventArgs e)
587 private void topMostBox_CheckedChanged(
object sender, EventArgs e)
589 TopMost = topMostBox.Checked;
597 private void Scripting_KeyDown(
object sender, KeyEventArgs e)
599 if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control)
601 saveButton.PerformClick();
610 private void errorBox_SelectionChanged(
object sender, EventArgs e)
612 Script sc = (Script)scriptView.SelectedItems[0].Tag;
613 Exception ex = sc.ex;
621 private void errorView_SelectedIndexChanged(
object sender, EventArgs e)
623 Exception ex = (Exception)errorView.SelectedItems[0].Tag;
624 string exs = ex.Message.Substring(ex.Message.IndexOf(
'('), ex.Message.IndexOf(
')'));
625 string ls = exs.Substring(1, exs.IndexOf(
',') - 1);
626 int line =
int.Parse(ls);
627 string c = exs.Substring(exs.IndexOf(
',') + 1, exs.IndexOf(
")") - exs.IndexOf(
',') - 1);
628 int cr =
int.Parse(c);
630 textBox.SelectionStart = line;
static void RunString(string con, string param, bool headless)
static object RunString(string st)
void Stop()
The function stops the thread.
void Run()
It creates a new thread and starts it.
static void Run(Script rn)
override string ToString()
static State GetDown(PointD pf, MouseButtons mb)
static State GetMove(PointD pf, MouseButtons mb)
static State GetUp(PointD pf, MouseButtons mb)
static void RunScript(string file)
void RefreshItems()
It reads all the files in the Scripts and Tools folders and adds them to a listview.
void RunScriptFile(string file)
static void RunString(string st)
void RefreshStatus()
It updates the status of the script and the log.
static void RunByName(string name)
static void UpdateState(State s)