Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/machinebox/sdk-go/tagbox"
)
func main() {
var (
dir = flag.String("dir", "./testdata", "source directory")
tagboxAddr = flag.String("tagbox", "http://localhost:8080", "tagbox address")
images = flag.String("images", ".jpg", "image files extension")
)
flag.Parse()
tagboxClient := tagbox.New(*tagboxAddr)
filepath.Walk(*dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
}
if !strings.HasSuffix(info.Name(), *images) {
return nil
}
parts := strings.Split(path, string(filepath.Separator))
if len(parts) < 2 {
return nil
}
tags, err := tag(tagboxClient, path)
if err != nil {
log.Println("[ERROR]: Tagging", err)
return nil
}
s := []string{}
for _, tag := range tags {
s = append(s, tag)
}
comment := strings.Join(s, ",")
updateFile(comment, path)
return nil
})
}
func updateFile(comment, path string) {
onrun := "on run {f, c}"
command := fmt.Sprintf(`tell app "Finder" to set comment of (POSIX file f as alias) to c`)
//log.Println("/usr/bin/osascript", "-e", onrun, "-e", command)
cmd := exec.Command("/usr/bin/osascript", "-e", onrun, "-e", command, "-e", "end", path, comment)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
log.Println(err)
}
}
func tag(tagboxClient *tagbox.Client, path string) ([]string, error) {
tags := []string{}
r, err := os.Open(path)
if err != nil {
return tags, err
}
defer r.Close()
resp, err := tagboxClient.Check(r)
if err != nil {
return tags, err
}
for _, tag := range resp.Tags {
tags = append(tags, tag.Tag)
}
log.Println(tags)
return tags, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.