8

How can I add date time in my custom code snippet?

I need frequent use to add my codes on other codes, and for others, I need to add my name and date time.

I created a code snippet with shortcut _ase, but I am not finding any help on net how can I add time to it.

enter image description here

4
  • Do you mean as a comment, similar to the // Created by Anoop Vaidya on 04/01/13 comment that Xcode generates when creating new header & implementation files? Commented Apr 1, 2013 at 14:55
  • @JohnSauer: Exactly :) Commented Apr 1, 2013 at 14:57
  • 1
    What is the purpose of that? Inline commentary is usually frowned upon in codebases.. Better to just let people focus on the code.. Commented Apr 2, 2013 at 3:14
  • 2
    Sometimes it is required who and when changed / added the code. Commented Apr 2, 2013 at 6:42

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.