Mac添加新建文件按钮或服务

  1. 打开/Applications/Automator.app应用,新建一个应用程序。(新建服务也可以,只不过服务需要选中文件或文件夹右键来新建,应用程序可以拖到finder的工具栏,点击新建)
  2. 左侧资源库选择实用工具,把下面AppleScript代码贴到右侧的代码区域。
  3. 下面代码配置是新建文件后用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

转载于:https://my.oschina.net/u/2303129/blog/902607

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值