I’d like to use pandoc to convert formated notes (= html) into markdown files via AppleScript.
This is what I came up with:
property pTitle : "DEVONthink Batch convert HTML to MD via Pandoc"
tell application id "DNtp"
try
set selectedItems to selection
repeat with selectedItem in selectedItems
set itemName to name of selectedItem
-- get base-name of filename
set baseName to itemName
set baseNameWithExtension to baseName & ".md"
-- now convert via Pandoc
do shell script "/usr/local/bin/pandoc --atx-headers --from html --to markdown_strict -o " & quoted form of baseNameWithExtension & " " & quoted form of (baseName & ".html")
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
end try
end tell
This does not work because I did not pass the path of the current item to the shell script.
So how do I access the current path of the selected item?
This here works, but although the markdown file is generated in the DEVONthink folder (from the html file), it does not appear in DEVONthink’s current group.
Do I need to tell DT to add the newly created file to the database?
property pTitle : "DEVONthink Batch convert HTML to MD via Pandoc"
tell application id "DNtp"
try
set selectedItems to selection
repeat with selectedItem in selectedItems
set fullPathOfItem to (path of selectedItem as string)
-- convert via Pandoc
-- shell command:
-- pandoc --atx-headers --from html --to markdown_strict -o OUTPUT_FILE INPUT_FILE
do shell script "/usr/local/bin/pandoc --atx-headers --from html --to markdown_strict -o " & quoted form of (fullPathOfItem & ".md") & " " & quoted form of fullPathOfItem
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
end try
end tell
Below is a working version (with lots of input from @pete31).
The only disturbing thing is this: I cannot write to temp folder. So if I try to write to path to temporary items, I get a privilege error (error number -10004). That’s why I write to the desktop, but that’s problematic, because if there is a file with the same filename it will be overwritten. That’s why I need to come up with a different solution for the temp folder. Either by writing to a temp folder I create beneath my user folder for my own scripting, or something else.
Update Tuesday, 9. June 2020 15:23:29
Will ask if original files should be deleted.
NB: You need to create a temp folder in your user home folder:
/Users/YOURUSERNAME/temp
Could of course be done within the script, but didn’t have the time to implement.
-- Created: Tuesday, 9. June 2020 14:59
-- Author: Ugur T.
-- Convert `formated notes` to `markdown` via Pandoc
-- Dependencies: Pandoc - See: https://pandoc.org/installing.html
tell application id "DNtp"
-- Ask if user wants to delete original files
set theTitle to "Delete original files?"
set theDialogText to "Want me to delete original files?"
set checkIfDeleteOriginal to display dialog theDialogText ¬
buttons {"Do not delete", "Delete"} ¬
default button "Do not delete" with title theTitle
-- process selected `html` files
-- convert them to `markdown` files
try
set selectedItems to selection
if selectedItems is {} then error "Please select some records."
repeat with selectedItem in selectedItems
-- path of item
set inputFileWithPath to path of selectedItem as string
-- name and extension
-- set itemName to name of selectedItem
-- itemName without file-extension
set itemName to name of selectedItem
set itemType to type of selectedItem as string
-- process only "formatted note" items
if itemType = "formatted note" then
-- destination path
-- > temp folder
set destinationPath to (the POSIX path of (path to home folder)) & "temp/"
-- set destinationPath to the POSIX path of (path to temporary items from user domain as string)
-- output file: markdown file
set outputFileWithPath to (destinationPath & itemName & ".md")
-- convert via Pandoc
-- shell command:
-- pandoc --atx-headers --from html --to markdown_strict -o OUTPUT_FILE INPUT_FILE
do shell script "/usr/local/bin/pandoc --atx-headers --from html --to markdown_strict -o " & (quoted form of outputFileWithPath) & " " & (quoted form of inputFileWithPath)
-- finally move the new html file into DEVONthink
-- first: index it
set theRecord to indicate outputFileWithPath to (current group) -- or: (display group selector)
-- second: move it into DEVONthink
consolidate record theRecord
-- now set the correct creation date (copy from original record)
set date of theRecord to (date of selectedItem)
set creation date of theRecord to (creation date of selectedItem)
-- delete original?
if checkIfDeleteOriginal = {button returned:"Delete"} then
delete record selectedItem
end if
end if
end repeat
-- if original items were kept, then re-select them
if checkIfDeleteOriginal ≠ {button returned:"Delete"} then
-- re-select previously selected items
set selection of window 1 to selectedItems
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
end try
end tell