psb01 Posted February 2, 2023 Share Posted February 2, 2023 Hi, I have seen several posts about inserting date/time and linking it to a hotkey trigger or short text. However these all seem to be about using the system date/time information and formats - where we have little control over the date/time elements and their placement in the date/time string we want. As someone who doesn't like the space character in my filenames I wish to be able to quick-enter a text string (mostly in the Finder Save dialog) of the current date/time in the specific format of... YYYY-MM-DD_HH-NN-SS_ Where... YYYY - current year in 4 digits. MM = current month in 2 digits DD = current date in 2 digits HH = current hour in 24hour format, 2 digits NN = current minutes of the hour in 2 digits SS = current seconds of the minute in 2 digits Also note the structure of the date/time string with the hyphens & underscore characters in place as separators. Is there a way to have this level of control in Alfred and assign it to a shortcut key for easy insertion into any app/field as a plan text string? In Windows I'm sure it can be done with AutoHotKey, but there doesn't seem to be an equivalent application for Macs with this level of control over the date/time elements within some sort of hot-key/text-snippet mechanism. Thank you. Link to comment
Stephen_C Posted February 2, 2023 Share Posted February 2, 2023 (edited) This piece of AppleScript will return today's date and the current time in the format you require: set theDateString to my createDate((current date)) set theTimeString to my createTime((current date)) set theResult to theDateString & "_" & theTimeString & "_" return theResult on createDate(theDate) -- Process Date return (year of theDate as string) & "-" & my zeropad(month of theDate as integer) & "-" & my zeropad(day of theDate) end createDate on createTime(theDate) --Process Time return (my zeropad(hours of theDate) & "-" & my zeropad(minutes of theDate) & "-" & my zeropad(seconds of theDate)) end createTime on zeropad(num) -- Add prefixing zero if num ≥ 10 then return (num as string) return ("0" & num as string) end zeropad You could link a hotkey (set not to require an argument) to a Run Script action, containing that piece of AppleScript. Link that to a Copy to Clipboard action. In the latter action don't set the action to copy to the frontmost app. Then, when saving a file, all you need to do is to press the hotkey and then ⌘V to replace the suggested file name with the date and time in the relevant format. It all sounds more complicated that it actually is in practice. I've tried it and it works well. Edit: apologies: had reversed day and month but have now corrected script to give the order you want. Stephen Edited February 2, 2023 by Stephen_C Correction to script Link to comment
vitor Posted February 2, 2023 Share Posted February 2, 2023 You can also use the date Dynamic Placeholders. They support advanced formatting so you can output it however you need. Link to comment
Stephen_C Posted February 2, 2023 Share Posted February 2, 2023 @psb01 Vitor is, of course, right (he always is! 😀). To save you messing around with the dynamic placeholder and, out of interest because I've not before used it, this should work if you save it as an Alfred snippet: {isodate: yyyy-MM-dd}_{isotime: HH-mm-ss}_ That's less clumsy than my method because you can simply trigger the snippet in a save dialog by typing the trigger over the highlighted word Untitled and that word will then be replaced by the snippet. Stephen Link to comment
margaretamartin Posted February 3, 2023 Share Posted February 3, 2023 Thanks so much for this! I use a YYYY-MM-DD date stamp in a lot of places on my Mac and after I got it working with one text expansion app, I was reluctant to switch because it had always seemed so tricky to set up on other apps. And, FWIW, the simple {isodate} placeholder automatically formats it as YYYY-MM-DD. No need to specify anything further. Stephen_C and Peter17 2 Link to comment
Peter17 Posted February 15, 2023 Share Posted February 15, 2023 Is there a trick to getting {isodatetime} etc to work? I can use {date} formats successfully, but any isodate formats aren't expanded in my snippets, e.g. the actual text "{isodatetime}" appears instead. Link to comment
Peter17 Posted February 15, 2023 Share Posted February 15, 2023 I'm answering this myself, as I think I have found the answer: I was using the Snippet area, which is fine for {date} placeholders. But I am now experimenting with Workflow Snippet Triggers, and I think I have found the solution. 😊 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now