Skip to content

Instantly share code, notes, and snippets.

@Flowkap
Last active August 21, 2017 10:31
Show Gist options
  • Save Flowkap/8858434 to your computer and use it in GitHub Desktop.
Save Flowkap/8858434 to your computer and use it in GitHub Desktop.
This is a small gist for autostarting applications with Awesome Window Manager (http://awesome.naquadah.org/) without the need to use a third party plugin like shifty. It's maybe not nice but works!
-- {{{
--
-- Autostarting for Awesome <3.4!
-- Add this section to the end of your rc.lua
-- configuration file within ~/.config/awesome/rc.lua
--
-- If you're using Awesome 3.5 change:
-- add_signal -> connect_signal
-- remove_signal --> disconnect_signal
--
-- Thanks to eri_trabiccolo as well as psychon
--
function spawn_once(command, class, tag)
-- create move callback
local callback
callback = function(c)
if c.class == class then
awful.client.movetotag(tag, c)
client.remove_signal("manage", callback)
end
end
client.add_signal("manage", callback)
-- now check if not already running!
local findme = command
local firstspace = findme:find(" ")
if firstspace then
findme = findme:sub(0, firstspace-1)
end
-- finally run it
awful.util.spawn_with_shell("pgrep -u $USER -x " .. findme .. " > /dev/null || (" .. command .. ")")
end
-- use the spawn_once
spawn_once("subl", "Sublime_text", tags[1][2])
spawn_once("chromium", "Chromium", tags[1][3])
spawn_once("thunar", "Thunar", tags[1][4])
spawn_once("xchat", "Xchat", tags[1][5])
-- }}}
@dvd001
Copy link

dvd001 commented Feb 8, 2014

spawn successfully launches the program, but i cannot get it to move programs to the specified tag automatically.

tried below
spawn_once("pcmanfm", "PCManFM", tags[3][2])
spawn_once("firefox-mail.sh", "FirefoxMail", tags[1][1])

have 3 monitors, in this example pcmanfm should go to tag 2 of 3rd monitor... but they all end up in 2nd monitor tag 1 regardless of the specified tag

any idea?

$ awesome -v
awesome v3.4.15 (Never Gonna Give You Up)
• Build: Apr 28 2013 18:48:19 for x86_64 by gcc version 4.8.0 (buildd@batsu)
• D-Bus support: ✔

@gBopHuk
Copy link

gBopHuk commented May 27, 2015

It doesn't work. Always run app on tag[#][1]
"#" - is number of screen

@silverweed
Copy link

silverweed commented Aug 24, 2016

Since awesome 3.5 you must replace add_signal with connect_signal and remove_signal with disconnect_signal, then it'll work just fine.

Also, I suggest replacing the argument of spawn_with_shell:

"pgrep -u $USER -x " .. findme .. " > /dev/null || (" .. command .. ")"

with

"pgrep -u $USER -x " .. findme .. " > /dev/null || exec " .. command

This avoids spawning a subshell for the process and still works the same.

@gBopHuk
Copy link

gBopHuk commented Nov 18, 2016

Thanks a lot! It look like it works fine now

@kreedz
Copy link

kreedz commented Mar 24, 2017

Doesn't work properly. Same as dvd001.
So the programs run at tag 1 regardless of the specified tag.
$ awesome -v:

awesome v3.4.15 (Never Gonna Give You Up)
 • Build: Feb 17 2013 21:10:46 for x86_64 by gcc version 4.7.2 (buildd@barber)
 • D-Bus support: ✔

@silverweed
Copy link

silverweed commented Jul 17, 2017

@kreedz Your Awesome version is pretty old...do you have a particular reason for not upgrading to v4?
If you do upgrade to >= v3.5, I have a working version in my awesome rc https://gist.github.com/silverweed/078ab193a90d2e341774#file-rc-lua-L821-L841

@coldfix
Copy link

coldfix commented Aug 21, 2017

By the way, in recent versions of awesome you can pass rules directly to the spawn function, like so:

awful.spawn(cmd, {
    tag = mouse.screen.selected_tag,
})

If you want to emulate with_shell just pass cmd = { awful.util.shell, '-c', cmd }.

This will provide more correct behaviour, i.e. set the tag for the correct process if several are spawned simultaneously) and maybe also set the tag for all windows if the process creates more than one (I didn't test).

Also, it simplifies the code by a good deal since you don't have to manage signal handlers manually and you don't need to know the window class name in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment