All the credit goes to Chris @ccstone for a great macro/script.
Actually, this is very doable with a small change to the AppleScript:
tell application "Finder"
### REPLACE Block to Get destFolder from User Prompt ###JMTX CHG
try
set finderInsertionLocation to (insertion location as alias)
set destFolder to POSIX path of (choose folder with prompt ¬
"Choose Folder for NEW FILE" default location finderInsertionLocation)
on error
set destFolder to POSIX path of (choose folder with prompt ¬
"Choose Folder for NEW FILE")
end try
end tell
It uses the Finder Insertion Location as the default folder (if it exists), but then prompts the user to pick the folder for the new file.
Here's the entire script with my mods. Just replace the last AppleScript in @ccstones macro with the below script.
AppleScript to Prompt User for New File Folder
-------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2019/02/02 16:54
# dMod: 2019-02-03 16:40 by JMichaelTX
# Appl: Finder, Keyboard Maestro Engine
# Task: Creating a new file from a template file in the Finder's Insertion Location.
# Mod by JMichaelTX: Prompt User for Folder for New File
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Keyboard_Maestro_Engine, @Create, @New, @File, @Template, @Insertion, @Location
-------------------------------------------------------------------
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
-------------------------------------------------------------------
try
# Get variables from Keyboard Maestro
tell application "Keyboard Maestro Engine"
set chosenFilePath to getvariable "chosenTemplate"
set newFileName to getvariable "newFileName"
set fileExt to getvariable "fileExt"
end tell
tell application "Finder"
### REPLACE Block to Get destFolder from User Prompt ###JMTX CHG
try
set finderInsertionLocation to (insertion location as alias)
set destFolder to POSIX path of (choose folder with prompt ¬
"Choose Folder for NEW FILE" default location finderInsertionLocation)
on error
set destFolder to POSIX path of (choose folder with prompt ¬
"Choose Folder for NEW FILE")
end try
end tell
----------------------------------------------------------------
# Setup for AppleScriptObjC.
set sourcePath to current application's NSString's stringWithString:chosenFilePath
set sourceName to sourcePath's lastPathComponent()'s stringByDeletingPathExtension()
set sourceExt to sourcePath's pathExtension()
### set destFolder to finderInsertionLocation ##JMTX Delete
set destFolder to current application's NSString's stringWithString:destFolder
set someName to current application's NSUUID's UUID()'s UUIDString() -- unique UUID string
set destTempPath to destFolder's stringByAppendingPathComponent:someName
# Move or copy to temp path:
set fileManager to current application's NSFileManager's defaultManager()
fileManager's copyItemAtPath:sourcePath toPath:destTempPath |error|:(missing value)
set newFilePath to (destFolder's stringByAppendingPathComponent:(newFileName & "." & fileExt))
--» Try to rename the newly copied file:
set renameResult to fileManager's moveItemAtPath:destTempPath toPath:newFilePath |error|:(missing value)
--» Try renaming with a serial number if the above rename action fails.
if renameResult as boolean is false then
repeat with i from 1 to 1000
set destName to current application's NSString's stringWithFormat_("%@-%@.%@", sourceName, i, sourceExt)
set renameResult to (fileManager's moveItemAtPath:destTempPath toPath:(destFolder's stringByAppendingPathComponent:destName) |error|:(missing value))
set newFilePath to (destFolder's stringByAppendingPathComponent:destName)
if renameResult as boolean then exit repeat
end repeat
end if
if renameResult as boolean is false then
error "Failure in while renaming template file!"
end if
set theDate to current date
set theURL to (current application's |NSURL|'s fileURLWithPath:(newFilePath as text))
set {resultOfCreationDate, theError} to (theURL's setResourceValue:theDate forKey:(current application's NSURLCreationDateKey) |error|:(reference))
if resultOfCreationDate as boolean is false then error (theError's localizedDescription() as text)
set {resultOfModificationDate, theError} to (theURL's setResourceValue:theDate forKey:(current application's NSURLContentModificationDateKey) |error|:(reference))
if resultOfModificationDate as boolean is false then error (theError's localizedDescription() as text)
set {resultOfContentAccessDate, theError} to (theURL's setResourceValue:theDate forKey:(current application's NSURLContentAccessDateKey) |error|:(reference))
if resultOfContentAccessDate as boolean is false then error (theError's localizedDescription() as text)
set newFileAlias to alias POSIX file (newFilePath as text)
--» Reveal the new file in the front Finder window.
tell application "Finder"
set fileParent to parent of newFileAlias
tell fileParent to update
delay 0.15
reveal newFileAlias
delay 0.15
tell fileParent to update
end tell
on error e number n
set e to e & return & return & "Num: " & n
if n ≠ -128 then
try
tell application (path to frontmost application as text) to ¬
set ddButton to button returned of ¬
(display dialog e with title ¬
"ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
default button "OK" giving up after 30)
if ddButton = "Copy Error Message" then set the clipboard to e
end try
end if
end try
-------------------------------------------------------------------