I’ve taken the liberty to restructure what you have posted. The primary issue is that you are opening and closing the files in TextEdit when you should be making a new document and saving it. I’ve also moved the Obsidian vault reference to a top level so that users have one place to change it. Finally, I collapsed the structure to a sequence of what are called run handlers, each completing their own focused goal.
(*
generate Obsidian files from a selection of OmniFocus projects
originator: JD
modified: jjw
2021-08-05
-- select a set of projects in OmniFocus and run this script
--> output is a set of markdown task lists in your Obsidian vault
Caveats: this flattens task lists; action groups are not supported
*)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- define your vault path here
property ObsidianVault : "/Volumes/Databases/Obsidian/> inbox"
property CheckBox : "- [ ] "
on run {}
set theProjectList to my getSelectedProjectsfromOF()
if theProjectList is {} then return
set theTextList to my generateMDTexts(theProjectList)
my putProjectListinObsidianVault(theTextList)
end run
on getSelectedProjectsfromOF()
set theList to {}
tell application "OmniFocus"
try
set my_trees to the value of selected trees of content of front window
on error
return theList
end try
repeat with the_Tree in my_trees
set theprojectname to name of containing project of the_Tree
set theprojectID to id of containing project of the_Tree
set theprojectnote to note of the_Tree
set thetasklist to tasks of the_Tree
copy {projectname:theprojectname, projectID:theprojectID, projectnote:theprojectnote, tasklist:thetasklist} to end of theList
end repeat
end tell
return theList
end getSelectedProjectsfromOF
on generateMDTexts(theList)
set theTextList to {}
set theTasks to ""
repeat with theProject in theList
set theProjectTitle to ("[[" & projectname of theProject & "]]") & return
set theprojectnote to the projectnote of theProject & return
repeat with theTask in tasklist of theProject
set theTasks to theTasks & (CheckBox & name of theTask) & return
end repeat
set theText to theProjectTitle & theprojectnote & theTasks
copy {projectname:projectname of theProject, itsText:theText} to the end of theTextList
end repeat
return theTextList
end generateMDTexts
on putProjectListinObsidianVault(theList)
tell application "Finder" to set theFolderPath to POSIX file ObsidianVault as alias
repeat with theProject in theList
set theFileName to (ObsidianVault & "/" & (projectname of theProject) & ".md") as text
tell application "TextEdit"
activate
set theDoc to make new document with properties {text:itsText of theProject}
tell theDoc to save in POSIX file theFileName
close front window
end tell
end repeat
end putProjectListinObsidianVault
Proposed Improvements Needed
-
I imagine that some changes are needed to the structure of the text that is supposed to be generated for the markdown file. I am not exactly sure here what is supposed to be enclosed in [[ ]] for example. This can be changed in the handler
generateMDTexts
. -
Folks may also want to include additional information such as due dates, defer dates, and so on. The items can be added to the list in the
getSelectedPropertiesfromOF
handler and then added to the text output appropriately in thegenerateMDTexts
handler.
I recommend getting ScriptDebugger if you are at all serious about continuing with AppleScript programming.
Hope you find the changes useful.
–
JJW