New File Here
12 June 2024Scratching an itch! Code self-explanatory – 😎
(*
Author: Per Møldrup-Dalum
Date: 2024-06-12
License: MIT
Description:
After three decades of missing the "New file
here" function in the Finder and every odd year looking for
some shareware program or app that provides that for then the
following year seeing that that app either breaking, being
abandonen, bloated out of proportion or becoming
subscription-ware, I've finally made my own. And my God, that
was easy!! Why oh why didn't I do that twenty years ago?
All that is needed is this small piece of AppleScript and a bit of UNIX.
Caveat: still in need of some exception handling 🤫
*)
(* create a folder in your Documents folder *)
set templateFolder to POSIX path of (path to documents folder) & "Templates"
(*
Put the templates you what to use into that. Notice, that
you can put ANYTING in that folder, be it Markdown, LaTeX,
Microsoft Word, Apple Pages, Excel, JPEG, MP4, etc!
Just make sure that you have a template for each of the
document types in the following list.
You can even have different versions of the same kind of
document. The "Website.html" element of the list is a
template I use for texts for my website.
*)
set docTypes to {".txt", ".docx", ".md", ".tex", "Website.html"}
tell application "Finder"
(* let the user see where the file will be created *)
activate
(* get the path to the front most window of the Finder *)
set theWin to window 1
set currentDir to POSIX path of (target of theWin as alias)
(* Now, get a file name for the new file *)
set file_name to display dialog "Create a new file in
📁 " & currentDir default answer "Untitled" with icon note buttons {"Cancel", "Continue"} default button "Continue"
(* leave if activated by mistake *)
if button returned of file_name is equal to "Cancel" then return
set file_name to (text returned of file_name)
(* ask for the type of the new document *)
set theDocType to item 1 of (choose from list docTypes with prompt "Select document type:" default items {".md"})
(* leave if activated by mistake *)
--if button returned of theDocType is equal to "Cancel" then return
(* construct the file path for the corresponding template *)
set templateFile to templateFolder & "/" & "template" & theDocType
(* Create a path to the new file, which dosen't exist yet *)
set newFile to (currentDir & file_name & theDocType) as string
(* we apparently need System Events to ask for existence of a file *)
tell application "System Events"
(* Ensure no file exists at the destination with the new name and document type *)
if exists file newFile then
(* Be kind and offer to open the old file *)
set answer to display dialog "File " & file_name & theDocType & " already exists. Should I open it for you?" buttons {"Yes", "No"} default button "No"
if button returned of answer is equal to "Yes" then
do shell script "open " & newFile
end if
else
(* Put a copy of the template file into the folder of the front most window of the Finder *)
do shell script "cp -p " & templateFile & " " & newFile
(* Open the new file in default application *)
do shell script "open " & newFile
end if
end tell
end tell