Note: solution provided works for Ubuntu (14.04) + Sublime Text 2/3
I've ended up with a very tricky trick but I'm very happy about it. IntelliJ Idea updates resources in two cases (if enabled in settings):
- When IDE window loses focus
- When pressing a button in the Run section or using a shortcut for it Ctrl+F10
So I've started with the first case looking for an utility for my operation system (Ubuntu 14.04), which can focus IntelliJ Idea window and the un-focus it, to make it update the resources. I've stumbled upon xdotool, which allows to get the id of the window by it's title and to focus a window by it's id. I've created a simple .sh script:
current_id="$(xdotool getactivewindow)" # get id of current window
idea_id="$(xdotool search --name 'IntelliJ IDEA')"
xdotool windowfocus "${idea_id}" # switch to idea
xdotool windowfocus "${current_id}" # switch to current window
windowfocus
just focuses the window, which means if the window is on the background or on another workspace, you won't get switched to it, you'll stay where you were, but the window will just obtain a focus. But it did not work with IntelliJ Idea, resources were not updating, seems that it wants full attention.
So I've tried to replace windowfocus
with windowactivate
- it switches to the window whenever it is (even if it is on another workspace) so it becomes focused, active and visible. But in combination with switching back to the original process, it produces like a visible switch between two windows (like when you Alt+Tab between windows), which is pretty noticeable, but it actually does it's job - resources get updated.
There was also a second option left - using a shortcut. xdotool has an option to send a keystroke to a window by it's id as well:
xdotool key --window "{$idea_id}" --clearmodifiers CTRL+F10
but it did not work, even in combination with focusing the window, also leaving behind an after-effect of a pressed Ctrl key. There is a section Sendevent Notes in xdotool docs, telling that a key-event generated by xdotool sets a special flag, and as a result an application which receives the event may analyze it for a flag presence and ignore the event, which might be the case with IntelliJ Idea.
After some research I've found another key-sending tool - xvkdb - and it worked like a charm, because I guess it uses another way of sending/generating a key event:
xvkbd -window "*IntelliJ IDEA*" -text "\C\[F10]"
When run alone this command keeps the focus on a Intellij Idea window, I've used xdotool to bring the focus back to a current process. Here is a final version of the script:
current_id="$(xdotool getactivewindow)"
xvkbd -window "*IntelliJ IDEA*" -text "\C\[F10]"
xdotool windowfocus "${current_id}"
To integrate it with Sublime Text 3, which I use as a code editor, I've installed a plugin SublimeOnSaveBuild, which can run a custom build (terminal command or a script) when one saves a file in Sublime Text. Here is a simple build setup:
{
"shell_cmd": "/path/to/script/idea_update_resources.sh"
}
So now I have an Intellij Idea open on another workspace, and when I save a file in Sublime Text, resources get updated by sending a shortcut to Intellij Idea window. The time of focusing the windows using this implementation is not noticeable as I can say. As a result it is way more faster then Grunt or Gulp task, I would say instant, because IntelliJ Idea does an update by itself, and also it is safe to use in case if IntelliJ Idea does some extra operations when updating resources.
Thanks @BenJamin for the question, hope it will help somebody.
Update:
After using this setup for a while it is not working as expected: if IntelliJ Idea is on another workspace or it's interface is fully covered with another window, the solution with xvkdb will unfortunately fail and won't execute resources update. I switched back to using windowactivate
.