{"id":328,"date":"2012-04-03T00:36:27","date_gmt":"2012-04-03T05:36:27","guid":{"rendered":"http:\/\/www.alandmoore.com\/blog\/?p=328"},"modified":"2012-04-03T00:40:28","modified_gmt":"2012-04-03T05:40:28","slug":"launching-urls-with-awesomes-run-command","status":"publish","type":"post","link":"https:\/\/alandmoore.com\/blog\/2012\/04\/03\/launching-urls-with-awesomes-run-command\/","title":{"rendered":"Launching URLs with awesome&#8217;s Run command"},"content":{"rendered":"<p>Following on the heels of my google search hotkey in awesome, I decided to tackle expanding the functionality of the run prompt.\u00a0 Awesome&#8217;s run prompt, by default, is basically a command-launcher; it chokes on any input that doesn&#8217;t represent an executable file.<\/p>\n<p>I wanted it to behave more like the run prompt in other desktops, so that typing in a URL would open the URL in an appropriate application.<\/p>\n<p>With help from Alexander Yakushev on the awesome mailing list, I managed to figure it out&#8230;.<\/p>\n<p><!--more--><\/p>\n<p>I originally had planned to just send any identified URL to xdg-open, but I found that it really only handles file, http, and ftp urls properly.\u00a0 More exotic urls, like ssh:\/\/, fish:\/\/, smb:\/\/, vnc:\/\/, man:\/\/, and others require more explicit direction.\u00a0 So the first thing was to define which programs would handle various protocols, using a lua table:<\/p>\n<pre lang=\"lua\">--runners for various protocols\r\nmyrunners = {\r\nhttp = \"xdg-open\",\r\nftp = \"dolphin\",\r\nfile = \"xdg-open\",\r\nvnc = \"krdc\",\r\nrdp = \"krdc\",\r\nfish = \"dolphin\",\r\nsmb = \"dolphin\",\r\nman = \"dolphin\",\r\ninfo = \"dolphin\"\r\n}<\/pre>\n<p>The next step was to override the default run callback.\u00a0 This takes place down in the globalkeys assignment table, where the default file has something like this:<\/p>\n<pre lang=\"lua\">awful.key({ modkey },            \"r\",     function () mypromptbox[mouse.screen]:run() end),<\/pre>\n<p>I replaced it with this definition:<\/p>\n<pre lang=\"lua\">    \r\nawful.key({ modkey },            \"r\",     function ()\r\n  local promptbox = mypromptbox[mouse.screen]\r\n    awful.prompt.run(\r\n      {prompt = \"<span>Run: <\/span>\"},\r\n      promptbox.widget,\r\n      function (command)\r\n        local protocol = command:match(\"^(%a+):\/+\")\r\n        if protocol then\r\n          command = (myrunners[protocol] or \"xdg-open\")..\" \" .. command\r\n        end\r\n        local result = awful.util.spawn(command)\r\n        if type(result) == \"string\" then\r\n          promptbox.widget.text = result\r\n        end\r\n      end,\r\n      awful.completion.shell,\r\n      awful.util.getdir(\"cache\") .. \"\/history\")\r\nend),<\/pre>\n<p>My callback for the run command (starting on line 6 here) basically does this:<\/p>\n<ul>\n<li>Uses a regex to search for the string &#8220;:\/&#8221; as part of the first word, and puts the letters before the colon into a variable called &#8220;protocol&#8221;<\/li>\n<li>If there is a protocol, it looks up the protocol in the &#8220;myrunners&#8221; table, and prepends the resulting command (or xdg-open, if the protocol isn&#8217;t in the table) to the URL.<\/li>\n<li>If there isn&#8217;t a protocol found (the first word contains no &#8220;:\/&#8221;), then it does nothing<\/li>\n<li>Finally, the constructed command is run!<\/li>\n<\/ul>\n<p>It works great, and allows me to quickly open remote URLs with a minimum of fuss.\u00a0 So far the only shortcoming is that opening files this way requires using &#8220;file:\/\/&#8221; followed by the full path.\u00a0 I&#8217;m contemplating the best way to streamline opening files in awesome, but haven&#8217;t quite decided how I want to work with it yet.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following on the heels of my google search hotkey in awesome, I decided to tackle expanding the functionality of the run prompt.\u00a0 Awesome&#8217;s run prompt, by default, is basically a command-launcher; it chokes on any input that doesn&#8217;t represent an executable file. I wanted it to behave more like the run prompt in other desktops, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7,5],"tags":[26,25,20,22,24,34],"class_list":["post-328","post","type-post","status-publish","format-standard","hentry","category-floss","category-programming","category-technology","tag-awesome-wm","tag-desktop-tweaking","tag-how-to","tag-instructional","tag-my-computers","tag-practical-tech"],"_links":{"self":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts\/328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/comments?post=328"}],"version-history":[{"count":3,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts\/328\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts\/328\/revisions\/330"}],"wp:attachment":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/media?parent=328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/categories?post=328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/tags?post=328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}