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

利用自带的 Automator 创建自动脚本,并通过快捷键在当前目录生成文本文件以便编辑。

  •  
  •   tpxcer · 2020-04-06 11:37:11 +08:00 · 1345 次点击
    这是一个创建于 1481 天前的主题,其中的信息可能已经有所发展或是发生改变。

    分享我的一个日常操作,我习惯性在当前目录下创建文件,然后打开编辑,但是 MacOS 没有创建文件的选项。但是我们可以通过自带的 Automator 创建工作流,然后绑定快捷键来实现这个目的。以下是我的操作步骤。同时我在 B 站录制了视频,食用更香,欢迎各位前往观看,记得三连哦!

    视屏地址: https://www.bilibili.com/video/BV1Vg4y1872j

    操作步骤:

    1. 打开Automator并选择Quick Action
    2. Workflow receives current选择no input 然后把any application改为Finder.app
    3. 在左侧Actions搜索Run AppleScript并将其拖到右侧空白区域。
    4. 粘贴我们事先准备的代码(该代码由Rarylson Freitas提供)
    set file_name to "untitled"
    set file_ext to ".txt"
    set is_desktop to false
    
    -- get folder path and if we are in desktop (no folder opened)
    try
        tell application "Finder"
            set this_folder to (folder of the front Finder window) as alias
        end tell
    on error
        -- no open folder windows
        set this_folder to path to desktop folder as alias
        set is_desktop to true
    end try
    
    -- get the new file name (do not override an already existing file)
    tell application "System Events"
        set file_list to get the name of every disk item of this_folder
    end tell
    set new_file to file_name & file_ext
    set x to 1
    repeat
        if new_file is in file_list then
            set new_file to file_name & " " & x & file_ext
            set x to x + 1
        else
            exit repeat
        end if
    end repeat
    
    -- create and select the new file
    tell application "Finder"
    
        activate
        set the_file to make new file at folder this_folder with properties {name:new_file}
        if is_desktop is false then
            reveal the_file
        else
            select window of desktop
            set selection to the_file
            delay 0.1
        end if
    end tell
    
    -- press enter (rename)
    tell application "System Events"
        tell process "Finder"
            keystroke return
        end tell
    end tell
    
    1. 保存文件,记住文件名,后面关联快捷键要用到(文件默认存放目录为~/Library/Services)。
    2. 进入系统设置,找到Keyboard打开对话框。
    3. Shortcuts标签页的Services中找到我们刚才保存的文件名并配置快捷键。

    ::: warning 注意 此时在Finder中使用快捷键可能会现警告。我们还需要在系统设置中的Seurity & Privacy里面把Privacy标签页Accessibility选项中的Finder勾选才行。 :::

    5 条回复    2020-04-06 23:12:06 +08:00
    zwb124
        1
    zwb124  
       2020-04-06 12:17:50 +08:00 via iPhone
    虽然一直是这么干的,但是支持楼主的分享,很多人其实只是需要一个新建文件功能,对于不想为了一个功能装一个软件的人其实这个方法是最好的
    tpxcer
        2
    tpxcer  
    OP
       2020-04-06 13:21:46 +08:00
    @zwb124 如果有更方便的姿势就好了 ,2333
    tpxcer
        4
    tpxcer  
    OP
       2020-04-06 21:45:48 +08:00
    @zwb124 第三方的能避免我还是尽量少装。
    zwb124
        5
    zwb124  
       2020-04-06 23:12:06 +08:00 via iPhone
    @tpxcer 我也是跟你一个思路,尤其是跟系统功能有关联的,所以我没用
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5080 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 09:42 · PVG 17:42 · LAX 02:42 · JFK 05:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.