With the script below I create a new markdown file in the selected group; the title of the markdown file is used as a filename.
Now I would like the script to select the item after it has been created so that I can start editing it.
I managed to open the record via
open window for record myRec
But I would like to edit the file in the DT window without opening the item.
What I have
Since I create the record, I have a reference to that record:
set myRec to create record with {name:theName, type:markdown, content:theMD} ¬
in the current group
My Script
-- Thursday, 4. June 2020 11:54:09
-- Taken from @cgrunenberg, see:
-- https://discourse.devontechnologies.com/t/keyboard-maestro-macro-applescript-new-markdown-file-from-selection/51571/2
tell application id "DNtp"
-- Create a markdown file
try
if not (exists think window 1) then error "No window opened."
set theName to display name editor info "Filename for the Markdown note:"
set {dateStamp, dateObj, newDateString} to my getDateString()
-- dateStamp: yyyy-mm-dd
-- dateObj: date "Thursday, 4. June 2020 at 15:33:49"
-- newDateString: "Thursday, 4. June 2020 15:33:49"
-- get filename
try
set invalidName to false
-- set theName to selected text of think window 1
if theName is "" then set invalidName to true
on error
set invalidName to true
end try
if invalidName then error "No filename entered."
-- set theContent to display name editor info "Please enter the Markdown note:"
set theMD to "# " & theName & return & "Created: " & newDateString & return & return
set myRec to create record with {name:theName, type:markdown, content:theMD} in the current group
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
end try
-- open the markdown file
-- open window for record myRec
-- from: https://discourse.devontechnologies.com/t/applescript-to-open-specific-item-in-dtp/45794/4?u=ugur
end tell
on getDateString()
set dateObj to (current date)
-- reformat date
-- from: date "Thursday, 4. June 2020 at 15:33:49"
-- to: "Thursday, 4. June 2020 15:33:49"
set dateString to (dateObj as string)
-- replace in date
-- " at " => " "
set newDateString to ¬
do shell script ¬
"echo " & quoted form of dateString & ¬
" | sed -E 's/ at / /g'"
-- Padding for month and day
-- => No matter what, take the last 2 chars
set theMonth to text -1 thru -2 of ("0" & (month of dateObj as number))
set theDay to text -1 thru -2 of ("0" & day of dateObj)
set theYear to year of dateObj
-- Now create a date like this: yyyy-mm-dd
set dateStamp to "" & theYear & "-" & theMonth & "-" & theDay
-- return {dateStamp:dateStamp, dateObj:dateObj}
return {dateStamp, dateObj, newDateString}
end getDateString