[Golang|AE] AE中英文切换 [读取注册表,读写xml]

用法

go build , 然后生成exe 双击直接运行, 具体用法参见powershell的

深坑

  1. 自带的xml会删除注释(未解决), 用其他方法很麻烦, 不过注释有没有无所谓
  2. 多重字段的写入, 比如ae自带的 一堆<Data>, 这时候创建结构体用用 []Data, 并且写入的时候使用索引写入

姊妹篇

使用powershell更改ae语言

源码

package main

import (
    "encoding/xml"
    "fmt"
    "log"
    "os"
    "path/filepath"
    "strconv"
    "strings"

    "golang.org/x/sys/windows/registry"
)

type Configuration struct {
    XMLName xml.Name `xml:"Configuration"`
    Payload Payload  `xml:"Payload"`
    Other   []Other  `xml:"Other"`
}

type Other struct {
    XMLName   xml.Name `xml:"Other"`
    Data      []Data   `xml:"Data"`
    AdobeCode string   `xml:"adobeCode,attr"`
}

type Payload struct {
    XMLName   xml.Name `xml:"Payload"`
    Data      []Data   `xml:"Data"`
    AdobeCode string   `xml:"adobeCode,attr"`
}

type Data struct {
    Key     string `xml:"key,attr"`
    Content string `xml:",chardata"`
}

type AEKey struct {
    Version     string
    InstallPath string
}

func readLang(path string) (lang string, err error) {
    xmlPath := filepath.Join(path, "AMT", "application.xml")
    xmlData, err := os.ReadFile(xmlPath)
    if err != nil {
        fmt.Println("Error:", err)
    }

    var config Configuration

    if err := xml.Unmarshal(xmlData, &config); err != nil {
        fmt.Println("Error:", err)
        return "", err
    }

    // 获取 installedLanguages 的内容

    for _, data := range config.Payload.Data {
        if data.Key == "installedLanguages" {
            lang = data.Content
            return
        }
    }

    return
}

func writeLang(path string, newLang string) (err error) {
    success := false
    xmlPath := filepath.Join(path, "AMT", "application.xml")
    xmlData, err := os.ReadFile(xmlPath)
    if err != nil {
        fmt.Println("Error:", err)
        return err
    }

    var config Configuration
    if err := xml.Unmarshal(xmlData, &config); err != nil {
        fmt.Println("Error:", err)
        return err
    }

    // 写入内容
    for index, data := range config.Payload.Data {
        if data.Key == "installedLanguages" {
            config.Payload.Data[index].Content = newLang
            success = true
        }
    }

    if !success {
        println("未找到")
        return fmt.Errorf("写入失败")
    }

    xmlBytes, err := xml.MarshalIndent(config, "", "    ")
    if err != nil {
        return
    }

    output := string(xmlBytes)
    output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + output

    replacer := []string{"&#xA;", "&#x9;"}
    for _, rep := range replacer {
        output = strings.ReplaceAll(output, rep, "")
    }

    os.WriteFile(xmlPath, []byte(output), 0666)
    return nil
}

func main() {

    aekeys := make([]AEKey, 0)

    ROOT := `SOFTWARE\Adobe\After Effects`

    k, err := registry.OpenKey(registry.LOCAL_MACHINE, ROOT, registry.ENUMERATE_SUB_KEYS)
    if err != nil {
        log.Fatal(err)
    }
    defer k.Close()
    subs, _ := k.ReadSubKeyNames(-1)

    for _, sub := range subs {
        subkey, _ := registry.OpenKey(registry.LOCAL_MACHINE, ROOT+"\\"+sub, registry.QUERY_VALUE)
        path, _, _ := subkey.GetStringValue("InstallPath")
        aekeys = append(aekeys, AEKey{
            Version:     sub,
            InstallPath: path,
        })
    }

    amount := len(aekeys)

    var installPath string

    if amount == 0 {
        fmt.Printf("%v\n", "未查询到任何AE注册表信息")
    } else if amount == 1 {
        fmt.Printf("查询到当前AE版本%v\n", aekeys[0].Version)
        installPath = aekeys[0].InstallPath
    } else {
        for index, aekey := range aekeys {
            fmt.Printf("%d. %s\n", index+1, aekey.Version)
        }
        fmt.Print("请选择AE版本(序号): ")
        for {
            var input string
            fmt.Scanln(&input)
            l, err := strconv.Atoi(input)
            if err != nil || l < 1 || l > len(aekeys) {
                fmt.Println("请选择正确的索引")
            } else {
                installPath = aekeys[l-1].InstallPath
                break
            }
        }
    }

    curLang, err := readLang(installPath)

    if err != nil {
        fmt.Printf("错误: %v\n", err)
        return
    }

    fmt.Printf("\n")

    languages := []string{"zh_CN", "en_US"}
    var langID int

    for i, lang := range languages {
        fmt.Printf("%v. %v\n", i+1, lang)
    }
    fmt.Printf("请选择要更换的语言(当前语言为 %v): ", curLang)

    for {
        var input string
        fmt.Scanln(&input)
        l, err := strconv.Atoi(input)
        if err != nil || l < 1 || l > len(languages) {
            fmt.Println("请选择正确的语言索引")
        } else {
            langID = l
            break
        }
    }

    newLang := languages[langID-1]

    err_write := writeLang(installPath, newLang)

    if err_write != nil {
        fmt.Printf("切换失败: %v\n", err)
        return
    }

    fmt.Printf("已经成功切换到:%s\n", newLang)

    fmt.Printf("按任意键退出\n")
    fmt.Scanln()
}

给TA充电
共{{data.count}}人
人已充电
go工具箱编程

[月离工具箱] 提取文件夹里所有文件

2024-4-7 18:41:14

杂记

[网站搭建]gitea

2022-6-26 10:57:39

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
搜索