macos 在 Mac OS 上创建一个空的 txt 文件而无需先打开应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2012623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an empty txt file on Mac OS without opening an application first
提问by Bernd
Is there a way to simply create a new document e.g. on the Desktop and open it with e.g. textmate with a simple shortcut or script. I know that the MS Windows approach where you can just create a new empty txt file directly is not working for Mac. I am looking for a method achieving something similar. Any ideas?
有没有一种方法可以简单地创建一个新文档,例如在桌面上,并使用例如带有简单快捷方式或脚本的 textmate 打开它。我知道 MS Windows 方法(您可以直接创建一个新的空 txt 文件)不适用于 Mac。我正在寻找一种方法来实现类似的东西。有任何想法吗?
采纳答案by xyz
alt text http://img64.imageshack.us/img64/2280/screenshot20100106at125.png
替代文字 http://img64.imageshack.us/img64/2280/screenshot20100106at125.png
This uses TextMate's mate
command line helper application.
这使用 TextMate 的mate
命令行助手应用程序。
If it's not installed, go to TextMate > Help > Terminal Usage.
如果未安装,请转到 TextMate > 帮助 > 终端使用。
#!/bin/bash
cd "$(dirname "touch filename.txt
")"
ntf="Untitled $(date +%s).txt"
touch "$ntf"
mate "$ntf"
- Save this on your Desktop as "New Text File.command"
- Make it executable (in Terminal: chmod +x "New Text File.command")
- Optional: Copy and paste the TextMate icon from TextMate.app's "Get Info" dialog in to your new file's "Get Info" dialog.
- 将此保存在您的桌面上作为“新文本文件。命令”
- 使其可执行(在终端中:chmod +x "New Text File.command")
- 可选:将 TextMate 图标从 TextMate.app 的“获取信息”对话框复制并粘贴到新文件的“获取信息”对话框中。
回答by Emil Vikstr?m
You can write this in the terminal:
您可以在终端中这样写:
#!/bin/sh
touch filename.txt
Or as a script:
或者作为脚本:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
close(open("myfile.txt", O_WRONLY | O_CREAT | O_TRUNC, 0664));
回答by pavium
How about the unix approach of creating an empty file with touch
?
创建一个空文件的unix方法怎么样touch
?
It could be done in a script, and passed to an application.
它可以在脚本中完成,并传递给应用程序。
回答by slebetman
Traditional on the shell is to use the touch
command. But in any programming language you can do it without running an external program by opening a file with the O_CREAT flag:
传统上的shell是使用touch
命令。但是在任何编程语言中,您都可以通过打开带有 O_CREAT 标志的文件来完成,而无需运行外部程序:
in C:
在C 中:
open TEMP '>', 'myfile.txt';
close TEMP;
in Perl:
在Perl 中:
close [open "myfile.txt" w+]
in Tcl:
在Tcl 中:
##代码##回答by Werner Bierwirth
There is another to create new files, you can also create your own templates with it. Just search for NewDoc in AppStore.
还有另一个用于创建新文件,您也可以用它创建自己的模板。只需在 AppStore 中搜索 NewDoc。