That script looks interesting! I’ll have to study that.
Imagine you are working on a project (or writing a chapter in a book) and want to tag notes as components of what you’re working on.
You can click on individual notes and see them individually, but consider clicking on a group or tag and seeing a view identical to “merge documents.”
The difference would be it would be a live aggregation. Every time you changed the component documents in the group or tag, the merged view would update.
A script might be fun, or I can just use the merge documents function.
Being able to see, overview several notes (eg „atomic notes“) at the same time, is also something I would find very useful. Scrivener Has the widely praised corkboard and in Agenda one has several notes in view at the same time. I like your idea of a virtual or only displayed merge view of (md) notes.
This script creates a transclusion for the current group’s Markdown records.
-- Create transclusion for current group's Markdown records
tell application id "DNtp"
try
if not (exists think window 1) then error "Please open a window"
set theGroup to current group
set theChildren_ReferenceURLs to reference URL of children of theGroup whose type = markdown and comment is not "Transcluded group contents"
if theChildren_ReferenceURLs = {} then error "This group doesn't contain Markdown records"
set theTransclusionRecord_Source to "{{" & my tid(theChildren_ReferenceURLs, "}}" & linefeed & "{{") & "}}"
set theTransclusionRecord to create record with {name:"_Transclusion - " & (name of theGroup), type:markdown, source:theTransclusionRecord_Source, comment:"Transcluded group contents"} in theGroup
open window for record theTransclusionRecord
activate
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell
on tid(theInput, theDelimiter)
set d to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
if class of theInput = text then
set theOutput to text items of theInput
else if class of theInput = list then
set theOutput to theInput as text
end if
set AppleScript's text item delimiters to d
return theOutput
end tid
Thanks for posting the script! I didn’t try this so far but I just wondered whether an advanced triggered script (see Script option in Info inspector) of groups could automatically create/update the transclusion or even automatically select it.
This script creates/updates a Transclusion record for the group’s Markdown records and selects or opens it in a new window.
From help:
Attaching a triggered script
- Select the item you want to attach a script to.
- Select Tools > Get Info or Tools > Inspectors > Generic.
- Click the down arrow next to Script and choose Select. Note the script can be located anywhere, but it must remain in that location for the script to trigger. For convenience, you can create a folder for them in ~/Library/Application Scripts/com.devon-technologies.think3/Menus.
-- Triggered script - Create transclusion for current group's Markdown records
-- Setup: Click "script" in a group's info inspector. Select path
property selectTransclusionRecord : true -- if false Transclusion record opens in a new window
on triggered(theGroup)
tell application id "DNtp"
try
set theResults to search "kind:markdown comment:\"Transcluded group contents\"" in theGroup
if theResults ≠ {} then
set theTransclusionRecord to item 1 of theResults
else
set theTransclusionRecord to create record with {name:"_Transclusion - " & (name of theGroup), type:markdown, source:"", comment:"Transcluded group contents"} in theGroup
end if
set theChildren_ReferenceURLs to reference URL of children of theGroup whose type = markdown and comment is not "Transcluded group contents"
if theChildren_ReferenceURLs = {} then error "This group doesn't contain Markdown records"
set theTransclusionRecord_Source to "{{" & my tid(theChildren_ReferenceURLs, "}}" & linefeed & "{{") & "}}"
set plain text of theTransclusionRecord to theTransclusionRecord_Source
if selectTransclusionRecord then
set selection of think window 1 to {theTransclusionRecord}
else
open window for record theTransclusionRecord
activate
end if
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell
end triggered
on tid(theInput, theDelimiter)
set d to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
if class of theInput = text then
set theOutput to text items of theInput
else if class of theInput = list then
set theOutput to theInput as text
end if
set AppleScript's text item delimiters to d
return theOutput
end tid
@cgrunenberg By the way, I know this has been asked several times and will probably not be implemented but it would be very very useful if we could set some window properties. Especially setting the state of the preview, the sidebar and the inspector would be very handy (at least whether they are shown or not). Bonus points if we could open the inspector to specific tabs Thank you very much for all the new AppleScript stuff, it’s amazing that almost everything users asked for is implemented after some months.
Nice
Awesome!
I was going to try this last night but got stuck in a rabbit hole. Long story - it looked like I had permissions issues on files. I didn’t, but I still blew an evening.
This script is really cool and works perfectly. I can set the script on a tag, then as I tag new files, they appear in the next view of the transclusion.
To access the transclusion, click the tag or group. To access the members of the tag or group, expand first and choose the member you want to mess with.
Pete, if you ever find yourself in Central Texas, the Bar-B-Que is on me. The first time I ran this script it was like a great disturbance in the force, as if millions of voices cried “Hooray!” and suddenly silenced as they turned to their keyboards.
It’s that nice.
I’m pretty sure Pete can script a BBQ wherever he wants
Nice use of text item delimiters
Interesting use of the comment “Transcluded group contents” to exclude the transclusion from transcluding itself.
Although, for producing “All work and no play” in a snowed-in haunted resort, I suppose you could delete that clause and save yourself a lot of typing.
Probably one of the all-time best user-contributed scripts. A+++
Another handy thing. You can export an empty group with a script as a template. Create a new group from the template, and you don’t have to mess with hunting down the script.
And, in the prescient and immortal words of Monty Python, there was much rejoicing.
Nice!
This script can be used to attach a triggered script to exsting groups
-- Attach triggered script to selected records
property theTriggeredScript_Path : "/Users/User/Desktop/Triggered Script - A.scpt" -- Set your path here
tell application id "DNtp"
try
set theRecords to selected records
if theRecords = {} then error "Please select some records"
repeat with thisRecord in theRecords
set attached script of thisRecord to theTriggeredScript_Path
end repeat
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell
This does not work with tags though, right? Only with proper “groups”?
Just tried on a tag, and it does not seem to work for me.
It works with tags if you select them in the item list, not in the sidebar.
Oh Wow. Now I also own you a BBQ Thank you This scrip must become part of DT3 core Beautiful stuff.
This is really great, with the potential to save huge amounts of time otherwise spent manually selecting and adding transclusion links!
@pete31: Is there a way to include the transcluded items’ names as well through the script, in addition to the contents? For my use case, this would be essential.
The output as I envision it might look like this:
Item name for transcluded item A
{{x-devonthink-item://9DDB6F83-AA13-4F88-925D-3D7015B1B02D}}
Item name for transcluded item B
{{x-devonthink-item://9DDB6F83-AA13-4F88-925D-3D7015B1B02D}}
This is the item name for transcluded item C
{{x-devonthink-item://9DDFDFG83-AA13-4F88-925D-3D7015B1B02D}}
This is by no means as sophisticated as anything that @pete31 produces but a simple change or two to my transclusion script may give what you need:
(* This script creates a new markdown record in the chosen database
and group and then adds to that record transclusion item links for plain text,
markdown or HTML records you have selected in the database with a line drawn
between each item. It will prompt you for the name of the new record or
you can choose to use the default name. *)
-- the database to open
property pDatabase : "/Users/stapp/Documents/DevonThink/Diaries.dtBase2"
-- the group to use for tbe markdown file of transclusions
property pGroup : "F3E59383-23EF-4CAB-9912-369CF9CF45CC"
-- the valid types of record for transclusion
property validTypes : {"markdown", "txt", "html"}
tell application id "DNtp"
try
activate
-- open the database
set theDatabase to open database pDatabase
set myGroup to get record with uuid pGroup
--check that we have some records selected for transclusion
set theSelection to the selection
if (count of theSelection) is 0 then
error "Please select some markdown records for transclusion"
end if
--set up the new record for the transclusions
set dAnswer to "New document"
set userEntry to display dialog ¬
"Enter the name of the new document (without the extension) or accept the default: " default answer dAnswer with title "New document for the transclusions"
set dAnswer to text returned of userEntry
set theNewRecord to create record with {name:dAnswer, type:markdown} in myGroup
-- Set up an empty list to populate with new strings, etc.
set textUpdates to {}
repeat with theRecord in (selection as list)
-- Check the document type
if (type of theRecord as string) is in validTypes then
--Get the reference URLs of the selected records
set recURL to reference URL of theRecord
set recordName to the name of theRecord
-- Format the transclusions
set transcludedItem to my createTransclusion(recURL)
-- Copy the string to the list, noting a linefeed is added after each line
copy (recordName & linefeed & transcludedItem & linefeed) to end of textUpdates
else
error "Check selected records: only markdown, plain text and HTML records are suitable for transclusion"
end if
-- Keep adding to the list until the loop expires
end repeat
-- Update the text of the file in one move
set plain text of theNewRecord to (textUpdates as string)
set root of viewer window 1 to myGroup
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell
--The handler to format the transclusion links is separate simply to make it easy
--to change the formatting between items if you wish.
on createTransclusion(theURL)
set theLink to "{{" & theURL & "}}" & linefeed & linefeed & "---" & linefeed
return theLink
end createTransclusion
Stephen