{"id":1508,"date":"2013-03-08T20:21:33","date_gmt":"2013-03-09T01:21:33","guid":{"rendered":"http:\/\/lukemiller.org\/?p=1508"},"modified":"2013-03-08T20:21:33","modified_gmt":"2013-03-09T01:21:33","slug":"imagej-startupmacros","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2013\/03\/imagej-startupmacros\/","title":{"rendered":"ImageJ StartupMacros"},"content":{"rendered":"<p>Before I lose this file again: <a title=\"ImageJ StartupMacros.txt\" href=\"http:\/\/www.lukemiller.org\/misc_files\/StartupMacros.txt\" target=\"_blank\">StartupMacros.txt<\/a> This is a text file, based on the original StartupMacros.txt file that came with ImageJ, that can be dropped in the ImageJ\/macros folder that is created when you install ImageJ. Start up ImageJ, and if this macro doesn&#8217;t automatically run, go to Plugins&gt;Macros&gt;Startup Macros to load it. It installs a few more drawing tools on the ImageJ toolbar and creates some keyboard shortcuts for the following tools:<\/p>\n<ul>\n<li>Line tool &#8211; press &#8216;l&#8217;<\/li>\n<li>Brush tool &#8211; press &#8216;b&#8217;<\/li>\n<li>Eraser tool &#8211; press &#8216;e&#8217;<\/li>\n<li>Wand tool &#8211; press &#8216;w&#8217;<\/li>\n<\/ul>\n<p>I either found or created some code found at the bottom of the file to create those shortcut keys. There&#8217;s also a shortcut bound to &#8216;k&#8217; (for count, I don&#8217;t know) that converts an image to 8-bit grayscale, thresholds the image, and counts the resulting dots. That&#8217;s probably not useful for anyone else, but illustrates the results of using the macro recording facilities of ImageJ.<\/p>\n<p>I use these keyboard shortcuts along side my drawing pad when measuring limpets. It&#8217;s quicker to hit a key on the keyboard than to find my mouse and click buttons on the ImageJ toolbar or try to do the same with the pen. There&#8217;s also the existing &#8216;m&#8217; keyboard shortcut that makes a measurement of whatever is currently selected with the wand tool.<\/p>\n<p>Most of the code below is not mine.<\/p>\n<pre>\/\/ \"StartupMacros\"\r\n\/\/ The macros and macro tools in this file (\"StartupMacros.txt\") are \r\n\/\/ automatically installed in the Plugins&gt;Macros submenu and\r\n\/\/  in the tool bar when ImageJ starts up.\r\n\r\n\/\/  About the drawing tools.\r\n\/\/\r\n\/\/  This is a set of drawing tools similar to the pencil, paintbrush, \r\n\/\/  eraser and flood fill (paint bucket) tools in NIH Image. The \r\n\/\/  pencil and paintbrush draw in the current foreground color \r\n\/\/  and the eraser draws in the current background color. The\r\n\/\/  flood fill tool fills the selected area using the foreground color.\r\n\/\/  Hold down the alt key to have the pencil and paintbrush draw \r\n\/\/  using the background color or to have the flood fill tool fill \r\n\/\/  using the background color. Set the foreground and background \r\n\/\/  colors by double-clicking on the flood fill tool or on the eye  \r\n\/\/  dropper tool.  Double-click on the pencil, paintbrush or eraser \r\n\/\/  tool  to set the drawing width for that tool.\r\n\/\/\r\n\/\/ Icons contributed by Tony Collins.\r\n\r\n   \/\/ Global variables\r\n   var pencilWidth=1,  eraserWidth=10, leftClick=16, alt=8;\r\n   var brushWidth = 10; \/\/call(\"ij.Prefs.get\", \"startup.brush\", \"10\");\r\n   var floodType =  \"8-connected\"; \/\/call(\"ij.Prefs.get\", \"startup.flood\", \"8-connected\");\r\n\r\n   \/\/ The macro named \"AutoRun\" runs when ImageJ starts.\r\n   \/\/ macro \"AutoRun\" {print(\"AutoRun\");}\r\n\r\n   \/\/ The macro named \"AutoRunAndHide\" runs when ImageJ starts\r\n   \/\/ and the file containing it is not displayed when ImageJ opens it.\r\n   \/\/ macro \"AutoRunAndHide\" {}\r\n\r\n   \/\/macro \"AutoRun\" {\r\n      \/\/run(\"My Plugin\");\r\n  \/\/}\r\n\r\n  \/\/macro \"Unused Tool -\" {}\r\n\r\n  var pmCmds = newMenu(\"Popup Menu\",\r\n       newArray(\"Help...\", \"Rename...\", \"Duplicate...\", \"Original Scale\", \r\n       \"Paste Control...\", \"-\", \"Record...\", \"Capture Screen \", \"Monitor Memory...\", \r\n       \"List Commands...\", \"Control Panel...\", \"Startup Macros...\", \"Search...\"));\r\n\r\n  macro \"Popup Menu\" {\r\n      cmd = getArgument();\r\n      if (cmd==\"Help...\")\r\n           showMessage(\"About Popup Menu\",\r\n               \"To customize this menu, edit the line that starts with\\n\\\"var pmCmds\\\" in ImageJ\/macros\/StartupMacros.txt.\");\r\n      else\r\n          run(cmd);\r\n  }\r\n\r\n  macro \"Abort Macro or Plugin (or press Esc key) Action Tool - CbooP51b1f5fbbf5f1b15510T5c10X\" {\r\n      setKeyDown(\"Esc\");\r\n  }\r\n\r\n  var xx = requires138b(); \/\/ check version at install\r\n  function requires138b() {requires(\"1.38b\"); return 0; }\r\n\r\n     var dCmds = newMenu(\"Developer Menu Tool\",\r\n      newArray(\"ImageJ Website\",\"News\", \"Documentation\", \"ImageJ Wiki\", \"Resources\", \"Macro Language\", \"Macros\", \r\n      \"Macro Functions\", \"Startup Macros...\", \"Plugins\", \"Source Code\", \"Mailing List Archives\", \"-\", \"Record...\", \r\n      \"Capture Screen \", \"Monitor Memory...\", \"List Commands...\", \"Control Panel...\", \"Search...\", \"Debug Mode\"));\r\n\r\n  macro \"Developer Menu Tool - C037T0b11DT7b09eTcb09v\" {\r\n       cmd = getArgument();\r\n       if (cmd==\"ImageJ Website\")\r\n           run(\"URL...\", \"url=http:\/\/rsbweb.nih.gov\/ij\/\");\r\n       else if (cmd==\"News\")\r\n           run(\"URL...\", \"url=http:\/\/rsbweb.nih.gov\/ij\/notes.html\");\r\n       else if (cmd==\"Documentation\")\r\n           run(\"URL...\", \"url=http:\/\/rsbweb.nih.gov\/ij\/docs\/\");\r\n       else if (cmd==\"ImageJ Wiki\")\r\n           run(\"URL...\", \"url=http:\/\/imagejdocu.tudor.lu\/imagej-documentation-wiki\/\");\r\n       else if (cmd==\"Resources\")\r\n           run(\"URL...\", \"url=http:\/\/rsbweb.nih.gov\/ij\/developer\/\");\r\n       else if (cmd==\"Macro Language\")\r\n           run(\"URL...\", \"url=http:\/\/rsbweb.nih.gov\/ij\/developer\/macro\/macros.html\");\r\n       else if (cmd==\"Macros\")\r\n           run(\"URL...\", \"url=http:\/\/rsbweb.nih.gov\/ij\/macros\/\");\r\n        else if (cmd==\"Macro Functions\")\r\n           run(\"URL...\", \"url=http:\/\/rsbweb.nih.gov\/ij\/developer\/macro\/functions.html\");\r\n        else if (cmd==\"Plugins\")\r\n           run(\"URL...\", \"url=http:\/\/rsbweb.nih.gov\/ij\/plugins\/\");\r\n        else if (cmd==\"Source Code\")\r\n           run(\"URL...\", \"url=http:\/\/rsbweb.nih.gov\/ij\/developer\/source\/\");\r\n        else if (cmd==\"Mailing List Archives\")\r\n           run(\"URL...\", \"url=https:\/\/list.nih.gov\/archives\/imagej.html\");\r\n        else if (cmd==\"Debug Mode\")\r\n           setOption(\"DebugMode\", true);\r\n\t else if (cmd!=\"-\")\r\n            run(cmd);\r\n  }\r\n\r\n  var sCmds = newMenu(\"Stacks Menu Tool\", \r\n       newArray(\"Add Slice\", \"Delete Slice\", \"Next Slice [&gt;]\", \"Previous Slice [&lt;]\", \"Set Slice...\", \"-\",\r\n       \"Convert Images to Stack\", \"Convert Stack to Images\", \"Make Montage...\", \"Reslice [\/]...\", \"Z Project...\",\r\n       \"3D Project...\", \"Plot Z-axis Profile\", \"-\", \"Start Animation\", \"Stop Animation\", \"Animation Options...\",\r\n       \"-\", \"MRI Stack (528K)\"));\r\n  macro \"Stacks Menu Tool - C037T0b11ST8b09tTcb09k\" {\r\n      cmd = getArgument();\r\n      if (cmd!=\"-\") run(cmd);\r\n  }\r\n\r\n   macro \"Pencil Tool - C037L494fL4990L90b0Lc1c3L82a4Lb58bL7c4fDb4L5a5dL6b6cD7b\" {\r\n        getCursorLoc(x, y, z, flags);\r\n        if (flags&amp;alt!=0)\r\n              setColorToBackgound();\r\n        draw(pencilWidth);\r\n   }\r\n\r\n  macro 'Pencil Tool Options...' {\r\n       pencilWidth = getNumber(\"Pencil Width (pixels):\", pencilWidth);\r\n  }\r\n\r\n   macro \"Paintbrush Tool - C037La077Ld098L6859L4a2fL2f4fL3f99L5e9bL9b98L6888L5e8dL888c\" {\r\n        getCursorLoc(x, y, z, flags);\r\n        if (flags&amp;alt!=0)\r\n              setColorToBackgound();\r\n        draw(brushWidth);\r\n   }\r\n\r\n   macro 'Paintbrush Tool Options...' {\r\n      brushWidth = getNumber(\"Brush Width (pixels):\", brushWidth);\r\n      call(\"ij.Prefs.set\", \"startup.brush\", brushWidth);\r\n  }\r\n\r\n    macro \"Flood Fill Tool -C037B21P085373b75d0L4d1aL3135L4050L6166D57D77D68La5adLb6bcD09D94\" {\r\n        requires(\"1.34j\");\r\n        setupUndo();\r\n        getCursorLoc(x, y, z, flags);\r\n        if (flags&amp;alt!=0) setColorToBackgound();\r\n        floodFill(x, y, floodType);\r\n     }\r\n\r\n   function draw(width) {\r\n        requires(\"1.32g\");\r\n        setupUndo();\r\n        getCursorLoc(x, y, z, flags);\r\n        setLineWidth(width);\r\n        moveTo(x,y);\r\n        x2=-1; y2=-1;\r\n        while (true) {\r\n            getCursorLoc(x, y, z, flags);\r\n            if (flags&amp;leftClick==0) exit();\r\n            if (x!=x2 || y!=y2)\r\n                lineTo(x,y);\r\n            x2=x; y2 =y;\r\n            wait(10);\r\n        }\r\n   }\r\n\r\n   function setColorToBackgound() {\r\n       savep = getPixel(0, 0);\r\n       makeRectangle(0, 0, 1, 1);\r\n       run(\"Clear\");\r\n       background = getPixel(0, 0);\r\n       run(\"Select None\");\r\n       setPixel(0, 0, savep);\r\n       setColor(background);\r\n   }\r\n\r\n  macro 'Flood Fill Tool Options...' {\r\n      Dialog.create(\"Flood Fill Tool\");\r\n      Dialog.addChoice(\"Flood Type:\", newArray(\"4-connected\", \"8-connected\"), floodType);\r\n      Dialog.show();\r\n      floodType = Dialog.getChoice();\r\n      call(\"ij.Prefs.set\", \"startup.flood\", floodType);\r\n  }\r\n\r\n  \/\/ This tool draws arrows. Double click on the tool icon\r\n  \/\/ to set the width and length. Hold the shift key down to\r\n  \/\/ constrain to mod 45 degree angles. Double click\r\n  \/\/ on the eye dropper tool to define the color.\r\n  \/\/ Press \"z\" to undo.\r\n  var arrowWidth = 3;\r\n  var arrowLength = -1;\r\n  var arrowFirst = 1;\r\n  var arrowLast = 1;\r\n  macro \"Arrow Tool (double click to configure) -C037L1ee1L65e1La9e1L65a9\" {\r\n      leftButton=16; rightButton=4; alt=8; shift=1;\r\n      getCursorLoc(x, y, z, flags); \r\n      xstart = x; ystart = y; \r\n      x2=x; y2=y;\r\n      setOption(\"disablePopupMenu\", true); \r\n      while (flags&amp;leftButton!=0) { \r\n          getCursorLoc(x, y, z, flags); \r\n          if (x!=x2 || y!=y2) {\r\n              dx = x - xstart;\r\n              dy = y - ystart;\r\n              a = atan2(dy, dx)*180\/PI;\r\n              aa = abs(a);\r\n              ra = sqrt(dx*dx + dy*dy);\r\n              dx \/= ra;\r\n              dy \/= ra;\r\n              if (flags&amp;shift!=0) {\r\n                 s2 = sqrt(2)\/2;\r\n                 if (aa&lt;=22.5||aa&gt;=157.5)\r\n                    dy=0;\r\n                 else if (aa&gt;=67.5&amp;&amp;aa&lt;=112.5)                     dx=0;                 else if (a&gt;22.5&amp;&amp;a&lt;67.5)\r\n                    {dx=s2;dy=s2;}\r\n                else if (a&lt;-22.5&amp;&amp;a&gt;-67.5)\r\n                    {dx=s2;dy=-s2;}\r\n                else if (a&lt;-112.5&amp;&amp;a&gt;-157.5)\r\n                    {dx=-s2;dy=-s2;}\r\n               else if (a&gt;112.5&amp;&amp;a&lt;157.5)                     {dx=-s2;dy=s2;}               }               if (arrowLength&gt;=0)\r\n                 ra = arrowLength;\r\n              x = xstart + dx*ra;\r\n              y = ystart + dy*ra;\r\n              makeLine(xstart, ystart, x, y);\r\n          }\r\n          x2=x; y2=y; \r\n          wait(10); \r\n      }\r\n      setOption(\"disablePopupMenu\", false);\r\n       if (x!=xstart || y!=ystart)\r\n          drawArrow(x2, y2, xstart, ystart, arrowWidth);\r\n      run(\"Select None\");   \r\n  }\r\n\r\n  function drawArrow(x1, y1, x2, y2, arrowWidth) {\r\n      setupUndo();\r\n      setLineWidth(arrowWidth);\r\n      size = 8+10*arrowWidth*0.5;\r\n      dx = x2-x1;\r\n      dy = y2-y1;\r\n      ra = sqrt(dx*dx + dy*dy);\r\n      dx \/= ra;\r\n      dy \/= ra;\r\n      x3 = x2-dx*size;\r\n      y3 = y2-dy*size;\r\n      r = 0.35*size;\r\n      x4 = round(x3+dy*r);\r\n      y4 = round(y3-dx*r);\r\n      x5 = round(x3-dy*r);\r\n      y5 = round(y3+dx*r);\r\n      if (arrowLength==-1 || arrowLength&gt;size)\r\n         drawLine(x1, y1, x2-dx*size, y2-dy*size);\r\n      makePolygon(x4,y4,x2,y2,x5,y5);\r\n      fill;\r\n   }\r\n\r\n  macro \"Arrow Tool (double click to configure) Options\" {\r\n      Dialog.create(\"Arrow Tool\");\r\n      Dialog.addString(\"Width:\", arrowWidth);\r\n      len = \"\"+arrowLength;\r\n      if (arrowLength&lt;0) len = \"variable\";\r\n      Dialog.addString(\"Length:\", len);\r\n      Dialog.show();\r\n      arrowWidth = parseInt(Dialog.getString());\r\n      if (isNaN(arrowWidth)) arrowWidth = 3;\r\n      arrowLength = parseInt(Dialog.getString());\r\n      size = 8+10* arrowWidth*0.5;\r\n      if (arrowLength&lt;size) arrowLength=size;\r\n      if (isNaN(arrowLength)) arrowLength=-1;\r\n  }\r\n\r\n  macro \"Set Drawing Color...\"{ \r\n      run(\"Color Picker...\"); \r\n  }\r\n\r\n  macro \"-\" {} \/\/menu divider\r\n\r\n  macro \"About Startup Macros...\" {\r\n      path = getDirectory(\"macros\")+\"\/About Startup Macros\";\r\n      if (!File.exists(path))\r\n          exit(\"\\\"About Startup Macros\\\" not found in ImageJ\/macros\/.\");\r\n      open(path);\r\n  }\r\n\r\n  macro \"Save As JPEG... [j]\" {\r\n     quality = call(\"ij.plugin.JpegWriter.getQuality\");\r\n     quality = getNumber(\"JPEG quality (0-100):\", quality);\r\n     run(\"Input\/Output...\", \"jpeg=\"+quality);\r\n     saveAs(\"Jpeg\");\r\n  }\r\n\r\n\/\/***************************************************\r\n\/\/Activate line tool by pressing lower case L button\r\nmacro \"Line Tool [l]\" {setTool(4);}  \r\n\r\n\/\/***************************************************\r\n\/\/Activate brush tool\r\n  macro \"Paintbrush Toggle [b]\" {\r\n\tsetTool(18);\r\n\twhile (true) {\r\n\t\t getCursorLoc(x, y, z, flags);\r\n       \t\t if (flags&amp;alt!=0)\r\n             \t\tsetColorToBackgound();\r\n       \t\t draw(brushWidth);\r\n\t}\r\n}\r\n\r\n\/\/***************************************************\r\nvar eraserWidth=10;\r\n\r\n\/\/Install Eraser Tool\r\n   macro \"Eraser Tool - C037R0aa4 P0a61f1aa0Pbef5f1\" { \r\n        setColorToBackgound();\r\n        draw(eraserWidth);\r\n   }\r\n\r\n\/\/Activate Eraser tool with 'e' key\r\nmacro \"Eraser Toggle [e]\" {\r\n\tsetTool(21);\r\n\tsetColorToBackgound();\r\n\tdraw(eraserWidth)\r\n}\r\n\r\n\/\/***************************************************\r\n\/\/Activate Wand tool with 'w' key\r\n\/\/ Added by LPM 20130113\r\nmacro \"Wand Toggle [w]\" {\r\n\tsetTool(\"wand\");\r\n}\r\n\r\n\/\/**********************************************************************************\r\n\/\/This macro converts an image to 8-bit (grayscale), thresholds the image, and \r\n\/\/counts the black spots in the resulting image using the Analyze Particles routine.\r\n\/\/Press the 'k' key to activate the macro. \r\nmacro \"Count dots [k]\" {\r\n\trun(\"8-bit\");\r\n\t\/\/run(\"Threshold...\");\r\n\tsetAutoThreshold();\r\n\tsetThreshold(85, 255);\r\n\trun(\"Convert to Mask\");\r\n\trun(\"Analyze Particles...\", \"size=30-Infinity circularity=0.00-1.00 show=Nothing summarize\");\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Before I lose this file again: StartupMacros.txt This is a text file, based on the original StartupMacros.txt file that came with ImageJ, that can be dropped in the ImageJ\/macros folder that is created when you install ImageJ. Start up ImageJ, and if this macro doesn&#8217;t automatically run, go to Plugins&gt;Macros&gt;Startup Macros to load it. It [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[29],"class_list":["post-1508","post","type-post","status-publish","format-standard","hentry","category-journal","tag-imagej"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/comments?post=1508"}],"version-history":[{"count":4,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1508\/revisions"}],"predecessor-version":[{"id":1512,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1508\/revisions\/1512"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=1508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=1508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=1508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}