Skip to content

Instantly share code, notes, and snippets.

@zdavkeos
Created May 15, 2013 03:39
Show Gist options
  • Save zdavkeos/5581507 to your computer and use it in GitHub Desktop.
Save zdavkeos/5581507 to your computer and use it in GitHub Desktop.
Small Haskell utility that substitutes the clipboard text to another program's command line arguments. See file for usage/examples.
-- About: Run a command using the text on the clipboard.
--
-- The command line arguments form the command. If "{}" is
-- in the arguments, it is substituted with the text from the
-- clipboard, otherwise the clipboard text is appended to the end.
-- Examples:
-- copy the filename on the clipboard to the parent directory
-- > withcb cp {} ../
--
-- unzip the filename on the clipboard in the current directory
-- > withcb unzip
--
-- download file who's url is in the clipboard
-- > withcb wget
import Data.List
import Data.Maybe
import System.Process
import System.Environment
import System.Clipboard (getClipboardString)
processArgs :: [String] -> String -> [String]
processArgs args subst = case elemIndex "{}" args of
Just i -> let (h, _:t) = splitAt i args
in h ++ (subst:t)
Nothing -> args ++ [subst]
main :: IO ()
main = do
args <- getArgs
maybeCB <- getClipboardString
let cb = fromMaybe (error "Cliboard empty") maybeCB
let cmd = unwords (processArgs args cb)
code <- system cmd
putStrLn $ "done. " ++ show code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment