├── prepend-metadata.py ├── LICENSE ├── search-replace.py └── README.md /prepend-metadata.py: -------------------------------------------------------------------------------- 1 | # prepend a metadata block to .md file 2 | 3 | # Set the folder 4 | folder = input('What folder:') 5 | 6 | # get the list of files in the folder https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory 7 | import glob 8 | 9 | files = folder + "/*.md" 10 | # All files ending with .md 11 | filenames = glob.glob(files) 12 | 13 | # get the MOC reference 14 | moc_link = input('MOC link:') 15 | 16 | # create the string 17 | metadata = ''' 18 | - metadata 19 | - when: 20 | - project: 21 | - area: 22 | - resource: ''' + moc_link + ''' 23 | - author: 24 | - source: 25 | - links: 26 | - 27 | - knowledge discovery: 28 | - specific: 29 | - related: 30 | - tags: 31 | 32 | --- 33 | 34 | ''' 35 | 36 | # prepend the string to the file 37 | for filename in filenames: 38 | print('processing file > '+filename) 39 | with open(filename, 'r') as original: data = original.read() 40 | with open(filename, 'w') as modified: modified.write(metadata + data) 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 throwaway-4736528 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /search-replace.py: -------------------------------------------------------------------------------- 1 | # search and replace text in files 2 | import glob 3 | import fileinput 4 | import urllib.parse 5 | 6 | #%% 7 | # Set the folder 8 | folder = input('What folder:') 9 | 10 | # get the list of files in the folder https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory 11 | files = folder + "/*.md" 12 | # All files ending with .md 13 | filenames = glob.glob(files) 14 | 15 | searchstring1 = "](images/" 16 | searchstring2 = "](attachments/" 17 | 18 | 19 | #%% 20 | 21 | for filename in filenames: 22 | print('processing file: '+filename) 23 | for line in fileinput.input(filename, inplace=True, backup='.bak'): 24 | if searchstring1 in line: 25 | original_str = line.split(searchstring1,-1)[1] 26 | original_str = original_str.split(")")[0] #remove trailing ")" 27 | original_str = urllib.parse.unquote(original_str) 28 | replace_str = '![['+original_str+']]' 29 | print(replace_str, end='') 30 | elif searchstring2 in line: 31 | original_str = line.split(searchstring2,-1)[1] 32 | original_str = original_str.split(")")[0] #remove trailing ")" 33 | original_str = urllib.parse.unquote(original_str) 34 | replace_str = '![['+original_str+']]' 35 | print(replace_str, end='') 36 | else: 37 | print(line, end='') 38 | 39 | fileinput.close() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # apple-obsidian 2 | Scripts to help migrate notes from Apple notes to Obsidian. 3 | 4 | >Note that all scripts are provided as-is, and are tested only on my files. Test them on a subset of your notes before using them for real, and of course ensure you have multiple backups. Data loss is not my responsibility :) 5 | 6 | ## exporting notes from Apple notes 7 | I'm on a Mac and used the very excellent [Exporter](https://apps.apple.com/de/app/exporter/id1099120373) to do the initial export. Notes were exported into folders that mimicked the structure in Apple Notes. 8 | 9 | Once that was done though I had a couple of challenges: 10 | 1. How to prepend metadata to each note? (I attach metadata to each note in Obsidian that forces me to associate it with a MOC and also link it to at least two other notes to aid future discoverability) 11 | 2. How to fix the file attachment links? (I wanted to make the links point to the file stored in the attachments folder in my vault, once I'd moved it there) 12 | 13 | So I wrote the Python scripts below. 14 | 15 | 16 | ## prepend-metadata.py 17 | - prepends obsidian metadata to the files in a folder 18 | - for what folder select the folder in finder, then option-right-click to copy the path as Pathname 19 | - for MOC, copy the full MOC as it will be inserted as a string, ie `[[Gardening MOC]]` 20 | - obviously change the metadata in the script to what you want 21 | 22 | ## search-replace.py 23 | - replaces the apple notes exports links to link back to your vault 24 | - note that it simply removes any path information from the attachment filename string, on the basis that Obsidian knows where to look for attachments based on the configuration under Settings > Files & Links 25 | - If you have multiple attachments with the same name, then this script will likely cause problems 26 | - the script is set to create backups (.bak) of any files it changes, so you can always revert if needed. 27 | 28 | ## How to run these scripts 29 | If you're on a Mac then you have python pre-installed: 30 | - open terminal 31 | - cd into the folder where the scripts are, ie `cd /Users/myname/obsidian` 32 | - to run the script it's `python3 prepend-metadata.py` or `python3 prepend-metadata.py` 33 | 34 | If you don't have python installed then it's not too hard to do but beyond the scope of this readme. --------------------------------------------------------------------------------