`

Automator右键新建文本文件

    博客分类:
  • ios
 
阅读更多

用惯了Windows的右键新建文本文件, 在Finder里面没有这个功能,感觉很不方便. 
于是就自己动手, 在Automator里用AppleScript现查现学现用作了一个. 
这是我写的第一个Apple Script. 写下来感觉Apple Script还是满方便的, 基本跟英文差不多. 
唯一不方便的是里面居然没有内建正则表达式. 

要在Finder里面添加这个功能, 具体步骤如下: 
打开Automator, 选择自定, 然后将 操作->实用工具->运行 AppleScript 拖到右边工作区. 
 
在右边的AppleScript的内容删掉, 用附件中的代码代替 
 
3 另存为Automator的插件, 名称为"新建文本文件", OK, 搞定 
 
4 
现在进入Finder的一个目录,然后 右键->更多->Automator->新建文本文件. 就会在当前目录新建一个文本文件, 并用TextEdit打开让你编辑. 
 

本文轉自macfans 

 

(*
--Author: Libok Zhou (libk.8800.org, libkhorse@gmail.com). 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
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics