# Automator

有兴趣的同学可以参考苹果官方教程:《自动操作使用手册》 (opens new window)

以下是我使用的自动操作:

# 在Finder中通过快捷键创建文本文件

视频教程:https://www.bilibili.com/video/BV1Vg4y1872j/ (opens new window)

  1. 打开Automator并选择Quick Action
  2. Workflow receives current选择no input 然后把any application改为Finder.app
  3. 在左侧Actions搜索Run AppleScript并将其拖到右侧空白区域。
  4. 粘贴我们事先准备的代码(该代码由Rarylson Freitas (opens new window)提供)
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  1. 保存文件,记住文件名,后面关联快捷键要用到(文件默认存放目录为~/Library/Services)。
  2. 进入系统设置,找到Keyboard打开对话框。
  3. Shortcuts标签页的Services中找到我们刚才保存的文件名并配置快捷键。

注意

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

更新时间: 4/18/2021, 1:21:06 AM