V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Stain5
V2EX  ›  macOS

一套 macOS 下的自动生成生词本解决方案

  •  
  •   Stain5 · 2020-06-30 17:52:40 +08:00 · 3083 次点击
    这是一个创建于 1402 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在给自带的 Dictionary 导入了 1 个 G 的词典后,遇到陌生的单词直接 Force Click 就能快速找到释义,整个人都舒适了,但也 macOS 本身没有好的解决方案,就结合 BTT 自己设计一个,当然不一定要依赖于 BTT,可以自己构建一个。

    通过 BTT 创建一个触发器,当执行时,模拟鼠标双击选中文本 -> 将所选文本储存至变量 selected_text ->运行 Apple 脚本 -> (可选)最后可以触发 Look Up 或者 /词典完成对陌生单词的学习

    因为一些原因暂时没有学习 Apple Script,所以这里就只用 apple script 获取 BTT 的变量 selected_text 并传递给 python,如果没有 BTT 的话,可以通过 apple script 读取选中的文本或者是剪切板。
    注意如果要使用这些代码要对 python 程序的位置的变量进行修改
    这里给出 Apple Script 代码

    tell application "BetterTouchTool"
    	
    	set seletedText to get_string_variable "selected_text"
    	
    end tell
    
    set location to "python3 #############Python Script Location "
    
    do shell script location & seletedText
    

    同时这里也给一段 python 生成单词本的示范代码,可以直接使用,只实现了基本的写入和防重复功能,并且可以调用 macOS 的通知功能完成程序状态的反馈

    import sys, os
    
    glossaryLocation = '/'
    
    def notify(title, text):
        os.system("""
                  osascript -e 'display notification "{}" with title "{}"'
                  """.format(text, title))
    
    # Check if word already appended
    try :
        file = open(glossaryLocation + 'AutomatedGlossary.txt')
        for word in file:
            if sys.argv[1].lower() == word.rstrip():
                notify('Word already existed','😅')
                bypass = True
                sys.exit()
    except OSError:
        notify('First time?😘','Creating a new Automated Glossary in ' + glossaryLocation)
        os.system('sleep 4')
    
    # Append Word
    file = open(glossaryLocation + 'AutomatedGlossary.txt','a')
    file.write(sys.argv[1].lower()+'\n')
    notify(sys.argv[1].capitalize() + '🥳','Successfully add to Automated Glossary')
    

    已上传至Gayhub!

    5 条回复    2021-09-29 17:06:45 +08:00
    derek80
        1
    derek80  
       2020-06-30 19:09:46 +08:00 via iPhone
    请教哪里有 1g 的字典?我这转出来的都 1,2 百兆。
    Stain5
        2
    Stain5  
    OP
       2020-06-30 19:13:06 +08:00
    @derek80 #1 牛津英汉 含语音
    duanliyu
        3
    duanliyu  
       2020-07-01 23:08:11 +08:00
    有办法执行在三指点击或查询单词自动记录吗
    Stain5
        4
    Stain5  
    OP
       2020-07-02 07:49:08 +08:00
    @duanliyu #3
    可以实现
    三指点击 触发
    模拟鼠标双击选中文本 -> 将所选文本储存至变量 selected_text ->运行 Apple 脚本 -> (可选)最后可以触发 Look Up 或者 /通过模拟触发快捷键 打开词典查询单词
    maxwell29
        5
    maxwell29  
       2021-09-29 17:06:45 +08:00
    兄嘚,ibook 双击选择会自动弹出查词界面,怎么关闭?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   761 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:29 · PVG 04:29 · LAX 13:29 · JFK 16:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.