Skip to content

Instantly share code, notes, and snippets.

@cwhittl
Last active March 19, 2024 01:14
Show Gist options
  • Save cwhittl/50fd0799636d66bb5f392f207ebfa82a to your computer and use it in GitHub Desktop.
Save cwhittl/50fd0799636d66bb5f392f207ebfa82a to your computer and use it in GitHub Desktop.
SuperNoteSyncWithNotebooksApp
# Define the paths to the Supernote, Supernote Tool, and Obsidian directories
$notesStoragePath = "/Users/chris/Dropbox"
$superNotePath = "${notesStoragePath}/Supernote/Note"
$notebooksAppStoragePath = "/Users/chris/Library/Mobile Documents/iCloud~com~aschmid~notebooks/Documents"
$inboxPath = "${notebooksAppStoragePath}/Inbox"
$pdfRelativePath = "/NBResources/Misc/SuperNoteImages"
$pdfPath = "${notebooksAppStoragePath}${pdfRelativePath}"
$superNoteToolPath = "/Library/Frameworks/Python.framework/Versions/3.12/bin/supernote-tool"
$returnLine = "`r`n"
# check for existing PDF storage, create io not
if (!( Test-Path $pdfPath)) {
# Remove-Item -Force -Recurse -Path $pdfPath
New-Item -ItemType Directory -Path $pdfPath
}
# 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 }
# Get the file name and path without the extension
$noteFilePath = $noteFile.DirectoryName
$noteFileNameWithOutExt = $noteFile.BaseName
$notePathTags = $noteFilePath.Replace($notesStoragePath,"").Replace(" ", "").Replace("/","${returnLine} - @")
$noteFileNameTags = $noteFileNameWithOutExt.Replace(" ", "").Replace("_","${returnLine} - @")
$noteCreatedDate = $noteFile.CreationTime.ToString()
$noteTags = "${notePathTags}${returnLine} - @${noteFileNameTags}"
# Generate the paths to the PDF and image files
$pdfFileName = "${fileID}.pdf"
$pdfFile = Join-Path -Path $pdfPath -ChildPath "${pdfFileName}"
# Convert the .note file to a PDF file if it doesn't exist or is newer than the existing PDF file
if (!(Test-Path $pdfFile) -or ((Get-Item "${pdfFile}").LastWriteTime -lt $noteFile.LastWriteTime)) {
$noteFullPath = $noteFile.FullName
# Write-Output "${superNoteToolPath} convert --policy=loose -t pdf -a ""$noteFullPath"" ""$pdfFile"""
Invoke-Expression "${superNoteToolPath} convert --policy=loose -t pdf -a ""$noteFullPath"" ""$pdfFile"""
} else {
Write-Output "No Dice"
}
# Get the path to the Markdown file
#$markdownPath = Join-Path -Path $inboxPath -ChildPath $noteFilePath.Substring($superNotePath.Length)
$markdownFile = Join-Path -Path $inboxPath -ChildPath "${noteFileNameWithOutExt}.md"
# Check if the Markdown file already contains a reference to the image file
#$existingMarkdownFile = Get-ChildItem -Path $notebooksAppStoragePath -Filter *.md -Recurse | Where-Object { Get-Content -Path $_.FullName -Raw | Select-String -Pattern "${fileID}.pdf" }
$existingMarkdownFile = Get-ChildItem "${notebooksAppStoragePath}\*.md" -recurse | Select-String -Pattern "${fileID}.pdf"
# If the Markdown file doesn't contain a reference to the image file, add it
if ($existingMarkdownFile.length -eq 0) {
New-Item -ItemType File -Path "$markdownFile" -Force
Add-Content -Path "$markdownFile" -Value "---${returnLine}tags:${returnLine}${noteTags}${returnLine}createddate: ${noteCreatedDate}${returnLine}participants: ${returnline}---${returnLine}"
Add-Content -Path "$markdownFile" -Value "- [ ] File Incoming SuperNote ${noteFileNameWithOutExt}"
Add-Content -Path "$markdownFile" -Value "#### *SuperNote Files Do Not Edit Below This Line*"
Add-Content -Path "$markdownFile" -Value "![[$pdfRelativePath/$pdfFileName]]"
} else {
Write-Output "Already Exists"
}
}
# SuperNote Can export
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment