在Mac中用快捷键快速插入日期时间

在Windows中有AutoHotkey这个好东东帮我快速插入日期时间,输入[mm然后敲空格就可以了。在Mac中就没有这么好的事了,虽然能够实现,但是没有AutoHotkey方便。还是要借助于AppleScript,但是并不能适用于Mac中任何文本输入的地方,比如DayOne的弹出窗口就不能用,另外还有一个限制:只能对选中的文字进行操作。

我使用的是这个文章里面的方案:

http://www.sixhat.net/applescript-insert-date-and-time-into-your-documents.html 

代码是:

-- Insert Date and Time into your documents
-- @2012 David Rodrigues
set date_ to ((current date) as string)
set the clipboard to the date_
tell application "System Events"
    set frontmostApplication to name of the first process whose frontmost is true
end tell
tell application frontmostApplication
    activate
    tell application "System Events"
        keystroke "v" using {command down}
    end tell
end tell

注意它会改变粘贴板的内容。我为它定义了一个快捷键:Cmd+Alt+Shift+F。使用之前,可以先敲一个字母,然后选中字母,按这个快捷键,就会显示出来日期时间了。比如现在的时间是:  2014-12-22 19:39:03 +0800。

下面的英文描述是下面参考第一个链接文章的内容,有时间再翻译。


Below is a very simple AppleScript that inserts the date, time and timezone in the format that Jekyll requires.

do shell script "date +%Y-%m-%d\\ %H:%M:%S\\ %z"

To make inserting it simple, it’s possible to assign a keyboard shortcut, by turning it into a Service in Automator.

  1. Open Automator.app

  2. Create a new Service

  3. Set it to receive selected text in any application

  4. Check the ‘Output replaces selected text’ tick-box

  5. Insert the ‘Run AppleScript’ action

  6. Copy and paste the above AppleScript

  7. Press run to make sure it works

  8. Save the .workflow file to ~/Library/Services

  9. Open the Keyboard > Keyboard Shortcuts system preference pane, select Services in the left column, then find the Service you just created.

  10. Assign a keyboard shortcut. I use Command + Option + d

There is one pitfall: it only works on selected text.

When saving a post for Jekyll, the file-name also requires a date—but not a time. For this, you can follow the same process as above with a modified AppleScript:

do shell script "date +%Y-%m-%d"

For this, I use the keyboard shortcut Command + Option + Shift + d.

I would love it if the Service created the entire file name. So if the post title ‘This is a Post Title’ was on the clipboard, it would create a file called 2012-11-10-this-is-a-post-title.md; replacing spaces with hyphens and converting it to lowercase. However, that’s well beyond my very limited AppleScript knowledge.

参考:

http://andytaylor.me/2012/11/10/creating-an-osx-service-to-insert-the-current-date-and-time/

http://www.question-defense.com/2013/01/28/title-osx-keyboard-shortcut-for-insert-date-and-time-insert-datetime-in-textmate 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。