Skip to content

Instantly share code, notes, and snippets.

@cwhittl
Created June 18, 2024 15:23
Show Gist options
  • Save cwhittl/25635e0beefcbc5b7d992932a1230241 to your computer and use it in GitHub Desktop.
Save cwhittl/25635e0beefcbc5b7d992932a1230241 to your computer and use it in GitHub Desktop.
This powershell script takes SuperNote (https://supernote.com/) notes and exports them in in the format needed by NotePlan (http://noteplan.co/). No matter where you move them it will update the PNGs of your Supernote notes
# Define the paths to the Supernote, Supernote Tool, and Obsidian directories
$superNoteParentStoragePath = "/Users/chris/Dropbox"
$superNotePath = "${superNoteParentStoragePath}/Supernote/Note"
$superNoteToolPath = "/Library/Frameworks/Python.framework/Versions/3.12/bin/supernote-tool"
$superNoteToolConversionType = "png"
$notesApplicationStoragePath = "/Users/chris/Library/Containers/co.noteplan.NotePlan3/Data/Library/Application Support/co.noteplan.NotePlan3/Notes"
$notesApplicationFileExt = ".txt"
$notesApplicationInboxPath = "${notesApplicationStoragePath}/0 - Inbox"
$notesApplicationAttachmentSuffix = "_attachments"
$returnLine = "`r`n"
# Get a list of all of the .note files in the Supernote directory
$noteFiles = Get-ChildItem -Recurse -Path $superNotePath -Filter *.note
# Iterate over each .note file and convert it to a PDF and image file
foreach ($noteFile in $noteFiles) {
# Get the file ID from the .note file
$fileID = Get-Content -Path $noteFile.FullName -Raw | Select-String -Pattern "<FILE_ID:(.*?)>" | ForEach-Object { $_.Matches.Groups[1].Value }
$noteFileNameWithOutExt = $noteFile.BaseName.Trim()
$noteFullPath = $noteFile.FullName
# Check if the Markdown file already contains a reference to the image file
$existingMarkdownFileSearch = Get-ChildItem "${notesApplicationStoragePath}\*${notesApplicationFileExt}" -recurse | Select-String -Pattern "${fileID}"
if ($existingMarkdownFileSearch.length -eq 0) {
$newMarkdownFilePath = Join-Path -Path $notesApplicationInboxPath -ChildPath "${noteFileNameWithOutExt}${notesApplicationFileExt}"
$noteDirectoryPath = $noteFile.DirectoryName.Trim()
$noteTags = $noteDirectoryPath.Replace($superNoteParentStoragePath, "").Replace(" ", "").Replace("/", "${returnLine} - #")
$noteCreatedDate = $noteFile.CreationTime.ToString()
# If the Markdown file doesn't contain a reference to the image file, create a new MarkdownFile
New-Item -ItemType File -Path "$newMarkdownFilePath" -Force
Add-Content -Path "$newMarkdownFilePath" -Value "#${noteFileNameWithOutExt} ${returnLine}" #File Title
Add-Content -Path "$newMarkdownFilePath" -Value "---${returnLine}tags:${returnLine}${noteTags}${returnLine}---${returnLine}"
Add-Content -Path "$newMarkdownFilePath" -Value "- [ ] File Incoming SuperNote ${noteFileNameWithOutExt}>today"
Add-Content -Path "$newMarkdownFilePath" -Value "#### *SuperNote Files Do Not Edit Below This Line*"
$existingMarkdownFile =(Get-Item "$newMarkdownFilePath")
$existingMarkdownFile.LastWriteTime = ${noteCreatedDate}
}
else {
Write-Output "Markdown Already Exists"
$existingMarkdownFile = (Get-Item $($existingMarkdownFileSearch | Select-Object Path -First 1 | % { $_.Path }))
}
$attachmentsPath = $existingMarkdownFile.FullName.Replace("${notesApplicationFileExt}", $notesApplicationAttachmentSuffix)
# check for existing PDF storage, create io not
if (!( Test-Path $attachmentsPath)) {
Write-Output "New Folder ${attachmentsPath}}"
New-Item -ItemType Directory -Path "$attachmentsPath"
}
$relativePathToExistingMarkDownFile = "${noteFileNameWithOutExt}_attachments"
$defaultConversionPath = Join-Path -Path "$attachmentsPath" -ChildPath "${fileID}.${superNoteToolConversionType}"
# Generate the paths to the converted files
Invoke-Expression "${superNoteToolPath} convert --policy=loose -t $superNoteToolConversionType -a ""$noteFullPath"" ""$defaultConversionPath"""
$iteration = 0
$stop = $false
DO {
$fileNameIteration = "${fileID}_${iteration}.${superNoteToolConversionType}"
$pngPageNumberFilePath = Join-Path -Path "$attachmentsPath" -ChildPath "$fileNameIteration"
$textFound = Select-String -Path "$($existingMarkdownFile.FullName)" -Pattern "$fileNameIteration"
if (($textFound.length -eq 0) -And (Test-Path $pngPageNumberFilePath)) {
$embedRelatvePath = "${relativePathToExistingMarkDownFile}/${fileNameIteration}"
$pngEmbed = "![image]($embedRelatvePath)"
Add-Content -Path "$($existingMarkdownFile.FullName)" -Value "$pngEmbed"
Write-Output "Iteration $filenameIteration added to $($existingMarkdownFile.FullName)"
}
else {
if (($textFound.length -ne 0)) {
Write-Output "Nothing needed to be added for iteration $filenameIteration it already exists"
}
else {
Write-Output "Iteration $filenameIteration does not exists"
$stop = $true
}
}
if ($stop -ne $true) {
$iteration = $iteration + 1
}
} Until (($stop -ne $false) -Or ($iteration > 100))
Write-Output "Stopped at $($iteration)"
}
# SuperNote Can export
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment