I’m modifying the contents of a Bike file with Python and want to see those changes reflected in my open Bike window automatically. The Python script runs in the background via cronjob.
I can do this by closing and reopening the file via AppleScript in Python (osascript), but I was wondering if there’s a more elegant way to do this?
Bike’s reload is provided by NSDocument machinery. I think the key to making reload run is to make appropriate calls to NSFileCoordinator API. Reload works when file is saved by another NSDocument based app, doesn’t work when change is made by a unix program.
Seems like there should be a way to trigger NSFileCoordinator events from terminal, but I can’t seem to find anything at the moment.
Better Way: Use AppleScript API
With the above said I think you can do what you want in a more robust way by using Bike’s AppleScript API. That will eliminate chance for overwrites and also make sure changes are on undo stack, so if your script goes bad you can undo changes.
General approach would be:
osascript -e "tell front document of application \"Bike\" to export as bike format"
// Process that result in python
osascript -e "tell front document of application \"Bike\" to delete rows"
osascript -e "tell front document of application \"Bike\" to import from \"PROCESSED_TEXT\" as bike format"
Does that make sense? Also if you are not relying on python too much you could also use AppleScript API more directly to make changes.
Makes sense, thank you. The main reason I went with Python was possibility of editing the .bike file on iOS with Pythonista and Shortcuts. I think I’ll just wait the iOS version (no pressure! )
I reimplemented my script in Shortcuts using around 40 actions but it’s just too hard to maintain and debug in Shortcuts. I’ll consider moving it to Applescript. Just need to get familiar with the syntax first
Good luck! I still don’t understand AppleScripts syntax. There is a JavaScript version… which is better for some syntax, but just as magical for other syntax.
My strategy is that through repeated hammering I get something to work, and then I write it down and copy/paste/modify for years to come. Here’s my source: