Fix `Go Back` working after creating a note at root with AppleScript

I have this script that @complexpoint kindly shared

tell application "Bike"
	tell front document
		select at make row at end of rows
	end tell
end tell

if i activate it from say somewhere deep in my bike document, so from here:

I activate and write something:

Then when I do Go Back, I see:

And Go Back again does nothing too.

I need a way to create a task into bottom of Home but also be able to Go Back to where I was before I ran above macro.

Is this possible to do?

Thank you.

Go Back only works for Focus In/Out changes. In particular selection changes are not considered for Go Back/Forward.

So if you are focused into a row… invoke that script, enter your new task… and then Go Back … I think it will take you back to where you were previously focused.

I think to get the behavior that you want you will have to store the original selection state somewhere. Then use a custom “Go Back” script that find and returns to that selection state.

Off the top of my head the two places that I can think of where you would store that state are:

  1. In attribute in outline
  2. In property in AppleScript

Maybe case 1 is easiest. You could try something like this and I think can work.

Here’s a script that doesn’t implement the full solution, but I think demonstrates the needed API. In particular this script:

  1. Saves the current selection ID as an attribute of root node
  2. Changes the selection… just as an example.
  3. And then restores that selection by reading the snapback attribute from root
tell application "Bike"
	tell front document
		set writeSnapback to id of selection row
		tell root row
			make attribute with properties {name:"snapback", value:writeSnapback}
		end tell
		
		select at last row
		
		set readSnapback to value of attribute named "snapback" of root row
		
		select at row id readSnapback
	end tell
end tell

For your use I think you would have the writing of the attribute in one script. And then the reading/restoring of the selection in another script that you invoke after you have entered the new row.

Make sense?

Note: There is an oddity (but kinda nice for this case) in that root row attributes are stored in the outline model, but don’t seem to be saved to file. Generally when you set an attribute like this it will be persisted to file… but I guess not in case of root.

2 Likes

Thank you, will try this out :heart:

1 Like