## Start a program and rearrange the geometry of its window ## (assume program creates EXACTLY ONE window) ## This is based on python script from ## http://askubuntu.com/questions/613973/how-can-i-start-up-an-application-with-a-pre-defined-window-size-and-position run <- function(program, x, y, w, h, args="") { ## List current windows getWindows <- function() { wl <- system("wmctrl -lp", intern=TRUE) if (!is.null(attr(wl, "status"))) { # Try again while (!is.null(attr(wl, "status"))) { wl <- system("wmctrl -lp", intern=TRUE) } } nf <- max(count.fields(textConnection(wl))) windows <- read.table(textConnection(wl), fill=TRUE, col.names=paste0("V", 1:nf)) windows$name <- gsub(" *$", "", apply(windows[-(1:4)], 1, paste, collapse=" ")) windows } w1 <- getWindows() ## Run the program system(paste(program, args)) ## Get process id of program ## (NOTE that all firefox windows have same pid) pid <- system(paste("pidof", program), intern=TRUE) ## Get multiple pids if run immediately after execution, but the ## last one is the common pid for gnome-terminal and firefox pid <- gsub(".+ ", "", pid) ## Loop (waiting for program window to open) time <- proc.time() found <- FALSE while (!found && (proc.time() - time)[3] < 10) { ## Check the window list again w2 <- getWindows() if (nrow(w2) > nrow(w1)) { wall <- rbind(w1, w2) wnew <- w2[! w2$V1 %in% w1$V1,] wprog <- wnew[grepl(pid, wnew$V3), ] if (nrow(wprog) > 0) { if (nrow(wprog) > 1) stop("More than one window matched") windowID <- wprog$V1 found <- TRUE } } } if (!found) stop("Failed to detect program window") ## Set geometry for the program window system(paste0("wmctrl -ir ", windowID, " -b remove,maximized_horz")) system(paste0("wmctrl -ir ", windowID, " -b remove,maximized_vert")) system(paste0("wmctrl -ir ", windowID, " -e 0,", x, ",", y, ",", w, ",", h)) return(windowID) }