Last active
October 3, 2023 09:12
-
-
Save peterhellberg/e987022aa88aea216c26f8cd5f12b3dc to your computer and use it in GitHub Desktop.
Skitch replacement in Go (Rename screenshot + SCP to remote server + Copy link to clipboard)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"os/exec" | |
"os/user" | |
"path" | |
"regexp" | |
"strings" | |
) | |
var screenshotPattern = regexp.MustCompile(`(\d{4})-(\d{2})-(\d{2}) at (\d{2})\.(\d{2})\.(\d{2})\.(\w+)`) | |
func main() { | |
var ( | |
sourcedir string | |
targetdir string | |
scpPath string | |
baseURL string | |
verbose bool | |
) | |
// Get the current user | |
u, err := user.Current() | |
if err != nil { | |
fmt.Printf("error: %v\n", err) | |
os.Exit(1) | |
} | |
// Handle command line flags | |
flag.StringVar(&sourcedir, "sourcedir", u.HomeDir+"/Desktop/", "Path to source directory") | |
flag.StringVar(&targetdir, "targetdir", u.HomeDir+"/Pictures/Screenshots/", "Path to target directory") | |
flag.StringVar(&scpPath, "scpPath", "c7.se:/var/www/assets.c7.se/screenshots/", "SCP host and path to upload screenshot to") | |
flag.StringVar(&baseURL, "baseURL", "https://assets.c7.se/screenshots/", "Base URL to serve the screenshots from") | |
flag.BoolVar(&verbose, "v", false, "Enable verbose output") | |
flag.Parse() | |
// Set the prefix to screenshot if not given one on the commandline | |
prefix := flag.Arg(0) | |
if prefix == "" { | |
prefix = "screenshot" | |
} | |
// Get a information about all the files in sourcedir | |
files, err := ioutil.ReadDir(sourcedir) | |
if err != nil { | |
fmt.Printf("error: %v\n", err) | |
os.Exit(1) | |
} | |
// Loop over files in reverse order | |
for i := len(files) - 1; i >= 0; i-- { | |
f := files[i] | |
// Check if the file is a screenshot | |
if screenshotPattern.MatchString(f.Name()) { | |
m := screenshotPattern.FindStringSubmatch(f.Name()) | |
if len(m) == 8 { | |
oldFn := f.Name() | |
newFn := fmt.Sprintf("%s-%s%s%s-%s%s%s.%s", prefix, m[1], m[2], m[3], m[4], m[5], m[6], m[7]) | |
if verbose { | |
fmt.Println(oldFn, "->", newFn) | |
} | |
sourcePath := path.Join(sourcedir, oldFn) | |
targetPath := path.Join(targetdir, newFn) | |
if err := os.Rename(sourcePath, targetPath); err != nil { | |
fmt.Printf("error: %v\n", err) | |
os.Exit(1) | |
} | |
if verbose { | |
fmt.Println("scp", targetPath, scpPath) | |
} | |
if err := exec.Command("/usr/bin/scp", "-q", targetPath, scpPath).Run(); err != nil { | |
fmt.Printf("error: %v\n", err) | |
os.Exit(1) | |
} | |
link := baseURL + newFn | |
cmd := exec.Command("pbcopy") | |
cmd.Stdin = strings.NewReader(link) | |
cmd.Run() | |
fmt.Println(link) | |
return | |
} | |
} | |
} | |
} |
Author
peterhellberg
commented
Nov 21, 2016
•
Updated script for use on tiny
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"os/user"
"path"
"regexp"
"strings"
)
var screenshotPattern = regexp.MustCompile(`Screenshot from (\d{4})-(\d{2})-(\d{2}) (\d{2})-(\d{2})-(\d{2})\.(\w+)`)
func main() {
var (
sourcedir string
targetdir string
scpPath string
baseURL string
)
// Get the current user
u, err := user.Current()
if err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
// Handle command line flags
flag.StringVar(&sourcedir, "sourcedir", u.HomeDir+"/Pictures", "Path to source directory")
flag.StringVar(&targetdir, "targetdir", u.HomeDir+"/Pictures/Screenshots/", "Path to target directory")
flag.StringVar(&scpPath, "scpPath", "c7.se:/var/www/assets.c7.se/screenshots/", "SCP host and path to upload screenshot to")
flag.StringVar(&baseURL, "baseURL", "https://assets.c7.se/screenshots/", "Base URL to serve the screenshots from")
flag.Parse()
// Set the prefix to screenshot if not given one on the commandline
prefix := strings.ReplaceAll(flag.Arg(0), " ", "-")
if prefix == "" {
prefix = "screenshot"
}
// Get a information about all the files in sourcedir
files, err := os.ReadDir(sourcedir)
if err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
// Loop over files in reverse order
for i := len(files) - 1; i >= 0; i-- {
f := files[i]
// Check if the file is a screenshot
if screenshotPattern.MatchString(f.Name()) {
m := screenshotPattern.FindStringSubmatch(f.Name())
if len(m) == 8 {
oldFn := f.Name()
newFn := fmt.Sprintf("%s-%s%s%s-%s%s%s.%s", prefix, m[1], m[2], m[3], m[4], m[5], m[6], m[7])
sourcePath := path.Join(sourcedir, oldFn)
targetPath := path.Join(targetdir, newFn)
if err := os.Rename(sourcePath, targetPath); err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
if err := exec.Command("/usr/bin/scp", "-q", targetPath, scpPath).Run(); err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
link := baseURL + newFn
cmd := exec.Command("xclip", "-selection", "clipboard")
cmd.Stdin = strings.NewReader(link)
cmd.Run()
fmt.Println(link)
return
}
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment