mac add new text document for the finder

Function: default create a new text file in the current directory TextEdit (text document that comes with the program).

process:

Open automator

Select ---- Services

Select ---- utility

Drag ---- apple script to run right panel

The panel is then replaced by the following contents:

(*
--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

Then save it as: "New text document"

After you can see the "New Text Document" option finder service options.

Reproduced in: https: //my.oschina.net/dake/blog/196709

Guess you like

Origin blog.csdn.net/weixin_33736832/article/details/91508448
Recommended