- 打开/Applications/Automator.app应用,新建一个应用程序。(新建服务也可以,只不过服务需要选中文件或文件夹右键来新建,应用程序可以拖到finder的工具栏,点击新建)
- 左侧资源库选择实用工具,把下面AppleScript代码贴到右侧的代码区域。
- 下面代码配置是新建文件后用sublime text打开。如果没有sublime text或不想打开要删除下面这行代码。
do shell script "subl " & newTxt
AppleScript:
tell application "Finder"
try
set currentFolder to (folder of the front window)
set currentPath to (POSIX path of (target of the front window as alias))
set libkIsDeskTop to false
on error
set currentFolder to desktop
set currentPath to (POSIX path of (desktop as alias))
set libkIsDeskTop to true
end try
set txtName to text returned of (display dialog "请输入文件名" default answer "新建文本文档.txt")
if length of txtName = 0 then
set ext to "txt"
set baseName to "新建文本文档"
set txtName to "新建文本文档.txt"
else
set prevTID to text item delimiters of AppleScript
set text item delimiters of AppleScript to "."
set libkNameParts to text items of txtName
set text item delimiters of AppleScript to prevTID
set len to length of libkNameParts
if len = 1 then
set ext to "txt"
set baseName to txtName
set txtName to baseName & "." & ext
else if len = 2 then
set ext to last text item of libkNameParts
set baseName to item 1 of libkNameParts as text
else
set ext to last text item of libkNameParts
set baseName to text 1 thru -((length of ext) + 1) of txtName
end if
end if
set n to 1
considering case
tell (get name of currentFolder's files) to repeat while txtName is in it
set txtName to baseName & "_" & n & "." & ext
set n to n + 1
end repeat
end considering
set newTxt to currentPath & txtName
do shell script "touch " & newTxt
if libkIsDeskTop is false then select the file txtName in currentFolder
do shell script "subl " & newTxt
end tell