Add time and date in custom/user Code Snippet in XCode

倖福魔咒の 提交于 2019-11-29 10:34:09

You can't add date or time automatically using the native Xcode snippet grammar.

Snippets do not have anything other than token substitution using the <#VisibleTokenName#> syntax.

File templates are generated differently and have token substitution for a small subset of predefined tokens (like ___DATE___) in addition to the ability for custom tokens gathered in the UI.

You could write a bash script (or whatever) to update the snippet file for you with the correct date.

Looks like you can't do this using XCode snippets but I can suggest a quick workaround using apple script:

set str to "// Created by Anoop Vaidya on " & (do shell script "date '+%d/%m/%Y'")
tell application "Xcode"
    activate
    set the clipboard to (str as text)
    tell application "System Events"
        keystroke "v" using command down
    end tell
end tell

You can set date using apple script:

 set str to ("// Created by Anoop Vaidya on " & day of (current date) & "/" & ((month of (current date)) as integer) as string) & "/" & year of (current date)

but it is not so convenient as using shell script.
Now you need only to bind that script to some shortcut (using FastScripts for example) and use it.

You can add some additional functionality to the script like saving previous value from clipboard and then restoring it or may be just using some XCode scripting properties to directly insert text without clipboard.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!