Created
April 18, 2019 14:39
-
-
Save aaronedell/9a54ee88a1731a636139a0a2be56deb2 to your computer and use it in GitHub Desktop.
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" | |
"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