Change Active Tool with script!
Did you know that you can change active tool through scripting? YES, YOU CAN, and it’s so easy! This is new option in InDesign CS5 scripting, and it’s so cool! You can even retrieve current tool name, hint, icon file! It’s really great feature! So, let’s look how to do it! 😀
This option is located in application class, under ‘toolBoxTools’ properties. First, let’s see all available tool options:
UITools.NONE | UITools.SELECTION_TOOL |
UITools.DIRECT_SELECTION_TOOL | UITools.GAP_TOOL |
UITools.PEN_TOOL | UITools.ADD_ANCHOR_POINT |
UITools.DELETE_ANCHOR_POINT | UITools.CONVERT_DIRECTION_POINT |
UITools.LINE_TOOL | UITools.TYPE_TOOL |
UITools.TYPE_ON_PATH_TOOL | UITools.PENCIL_TOOL |
UITools.SMOOTH_TOOL | UITools.ERASE_TOOL |
UITools.POLYGON_FRAME_TOOL | UITools.RECTANGLE_FRAME_TOOL |
UITools.ELLIPSE_FRAME_TOOL | UITools.POLYGON_TOOL |
UITools.RECTANGLE_TOOL | UITools.ELLIPSE_TOOL |
UITools.ROTATE_TOOL | UITools.SCALE_TOOL |
UITools.SHEAR_TOOL | UITools.SCISSORS_TOOL |
UITools.FREE_TRANSFORM_TOOL | UITools.GRADIENT_SWATCH_TOOL |
UITools.GRADIENT_FEATHER_TOOL | UITools.NOTE_TOOL |
UITools.EYE_DROPPER_TOOL | UITools.MEASURE_TOOL |
UITools.HAND_TOOL | UITools.ZOOM_TOOL |
UITools.TABLE_TOOL | UITools.PLACE_CURSOR_TOOL |
UITools.MOTION_PATH_TOOL | UITools.PAGE_TOOL |
Good, so, now let’s see how to get active tool:
var myTool = app.toolBoxTools;
Ok, ‘myTool’ now holds active tool object. We can retrieve info about it:
var myToolObj = myTool.currentTool; var myToolName = myTool.currentToolName; var myToolHint = myTool.currentToolHint; var myToolIcon = myTool.currentToolIconFile;
Cool! So, let’s try to change active tool to for example ‘Pencil Tool’:
myTool.currentTool = UITools.PENCIL_TOOL;
Yeah, it works 😀 Also, you can add event listener to track tool changing or something else. We will add simple listener just for example:
#targetengine myToolListener app.toolBoxTools.addEventListener("afterAttributeChanged", myTest); function myTest(){ alert(app.toolBoxTools.currentToolHint); }
Now, each time we change tool, alert will pop-up and show tool hint.
That’s it! It’s really handy, and easy control over active tool!
Have fun! 😀
Hi Marijan,
an excellent observation. I wish we could go further. Especially with the Eye Dropper Tool, so we could configure it. Cannot imagine a way to do it now.
Uwe