赞
踩
注意:旧方法是好久之前写的,请直接略过,直接翻到最后查看新方法。新方法不需要python环境,也不需要其它环境,可以用快捷键在鼠标选中的窗口一键创建,而且可以生成多个新文本,序号会自动叠加。
python环境mac自带,大约是2.7版本
# -*- coding: UTF-8 -*- import random import os #path_name = os.getcwd() path_name = raw_input() path_name += '/' text_name = '新文本_' + str(random.randint(0, 65536)) completeName = path_name + text_name + ".txt" file1 = open(completeName , "w") init_text = "Hello world!" file1.write(init_text) file1.close()
脚本存放/Users/你的名字/Library/Services下
将对应脚本删除就可以了
只能在固定的目录下生成,现在的代码是在桌面生成一个空txt,无法在任意位置生成,主要问题在于无法获取到鼠标指针所在位置的当前目录
该方法是使用AppleScript创建的,所以可以获取当前选中窗口路径,并且创建txt文本,下面先贴代码。(不用删注释,可以直接复制粘贴进去)
-- 定义一个包含参数 input 和 parameters 的处理器函数 "run" on run {input, parameters} -- 告诉 Finder 应用程序执行下面的操作 tell application "Finder" -- 将变量 baseName 设为 "未命名" set baseName to "未命名" -- 将变量 extension 设为 ".txt" set extension to ".txt" -- 将变量 folderLocation 设为当前插入位置(指示新文件将创建在哪个文件夹) set folderLocation to get insertion location -- 初始化计数器变量为0 set counter to 0 -- 开始一个循环 repeat -- 将变量 fileName 设为 baseName、counter 和 extension 拼接而成的字符串 set fileName to baseName & counter & extension -- 检查文件夹 folderLocation 中是否存在同名的文件 if not (exists file fileName of folderLocation) then -- 如果不存在同名文件,尝试创建一个新文件,并将其命名为 fileName try set newFile to make new file at folderLocation with properties {name:fileName} -- 创建成功,显示一个系统通知,提示内容为文件名字+创建成功 do shell script "osascript -e 'display notification \"" & fileName & " 创建成功。\" with title \"文件创建成功\" subtitle \"\" sound name \"default\"'" exit repeat on error -- 创建失败,显示一个系统通知,提示内容为创建失败 do shell script "osascript -e 'display notification \"创建失败。\" with title \"文件创建失败\" subtitle \"\" sound name \"default\"'" exit repeat end try end if -- 如果存在同名文件,增加计数器的值,以便尝试下一个文件名 set counter to counter + 1 end repeat end tell -- 返回输入参数 return input end run
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。