mac 为finder添加 新建文本文档

功能:默认使用TextEdit(自带文本文档程序)在当前目录建立新文本文档。

过程:

打开automator

选择----服务

选取----实用工具

拖动----运行apple script 到右边的面板中

然后把面板内容替换如下:

(*
--Author: Libok Zhou (libk.8800.org, [email protected]). Feel free to contact me if you have questions about this script
--Usage: This script is used for Automator to add an "new text file" to the context menu, 
	     when you want to create a new text file in current folder of Finder
*)
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 currentPath to (POSIX path of (target of the front window as alias))
	set currentFolder to (folder of the front window)
	*)
	
	set txtName to text returned of (display dialog "Please enter the text name, " default answer "untitled.txt")
	
	--if txtName is empty using "untitled.txt" as default
	--no trailing extension, suffix with ".txt"
	--have extension, don't touch it.
	if length of txtName = 0 then
		set ext to "txt"
		set baseName to "untitled"
		set txtName to "untitled.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
	
	
	
	-- if the file name already exists in current folder, attach the "_n" to the filename
	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
	tell application "TextEdit"
		activate
		open newTxt
	end tell
	
end tell

然后保存为:“新建文本文档“

以后即可在finder的服务选项中看到”新建文本文档“选项。

转载于:https://my.oschina.net/dake/blog/196709

猜你喜欢

转载自blog.csdn.net/weixin_33736832/article/details/91508448