Tinderbox Training Video 63- Export Drafts Note to Tinderbox

if you use the AppleScript in Drafts to move the note into TBX - maybe you would like to move the notes to a specific target container. Here you go…
Just add a new attribute called “MoveByTag” to the gPreferences note. There you store a reference where the key is a tag you set in Drafts and the value is the target container in TBX. Like “default:PKM;pkm:PKM;uni:Uni”.

Now add the call to the function moveToTargetContainer_db(); to the $Rule of the inbox container.

And add the function itself to a note in the /Hints/Library/ container:

function moveToTargetContainer_db(){
	var myList = collect(children,$Path);
	myList.each(theNote){
		if($Created(theNote) <= date("now + 5 seconds")){
			var myFirstTag = $Tags(theNote)[0];
			if(myFirstTag=""){
				myFirstTag="default";
			};
			var myTarget = $Path($MoveByTag("gPreferences")[myFirstTag]);
			$Container(theNote) = myTarget;
		};
	};
};

Now you can add a tag to a new note in Drafts. After exporting the note it will get moved into your target container (specified in gPreferences).

P.S.: I first tried to add this function to the $OnAdd attribute of the Inbox note - but because the AppleScript first creates the note and will set some attributes after creating the note - $OnAdd is too fast… $OnAdd would have been the more elegant solution because it would run only once.

Thank you everyone for your contribution and @satikusala for creating yet another wonderful video.

1 Like

I’m a little confused here. I was under the impression that $Created is one of the handful of Attributes that cannot be user-modified. But in this example it quite clearly is modifiable, and I just also tested it.

How/why does this happen? Is AppleScript overriding whatever lock is on $Created?

No - it will not get modified.

function moveToTargetContainer_db(){
	// get a list of all children in this container
	var myList = collect(children,$Path);
	// no we iterate through the list just created, step by step, for each child
	myList.each(theNote){
		// the variable "theNote" contains each element in the list, one after another
		// first round first child, 2nd round 2nd child an so on
		
		// now I check if the attribute $Created of the child contains a value that is
		// less or equal to the current date plus 5 seconds
		// I need this delay because the transfer from Drafts may be incomplete if this
		// function will do it work to fast. But the function is NOT changing the value of
		// $Created
		if($Created(theNote) <= date("now + 5 seconds")){
			var myFirstTag = $Tags(theNote)[0];
			if(myFirstTag=""){
				myFirstTag="default";
			};
			var myTarget = $Path($MoveByTag("gPreferences")[myFirstTag]);
			$Container(theNote) = myTarget;
		};
	};
};
1 Like

I see. I was/am confused by this line:

Ah - you were right - with AS it is possible to set the value of this attribute!

1 Like

This is actually brilliant for my purposes :smiley:.

I had been carrying around an extra Attribute labeled $TimeStamp, as the moment of an idea’s creation is invariably different from the moment I enter it in Tinderbox. Thanks so much!!

I think I would stay with $Created = time when the note was created in TBX and your date of intellectual creating should go into your user attribute. It’s a very popular feature every data store uses: created and modified are set by the system, not by the user. My AppleScript approach is a little bit different because I create the note from an app outside of TBX…
OK - you will reach the target on both streets - it’s just not the common way to go - but hey… :wink:

1 Like

Yes, I agree with what you say and that the secondary User Attribute is safer than messing with System Attributes… it’s just that $Created appears to be pivotal while using other Tinderbox features that reference the Attribute. Besides, it’s the only use I have for $Created - particularly as I am usually working both in Drafts and Tinderbox concurrently.

Hi - I’m a heavy user of Drafts and am very keen to get this script working - however after several hours today I’ve failed :frowning:
I’ve followed Michael’s video, and the conversation from the other thread too covering this topic and despite being an experienced user of both apps and setting up complex draft actions can’t get this one to work

I’ve been careful with file path names and have tried with both local file and cloud one. I’m running TBX version 9, on a Monterey Mac and the latest drafts version
I’ve cut and pasted the AS from both threads and used the auto-install from the drafts community link above - all failing - not sure how to resolve this?

Hi Mark - you might consider coming on the meetup tomorrow, we can go through a test export using share screen and trouble-shoot with you.

1 Like

Yes, I do enjoy the recordings - I will if I can but I’m travelling then and may be difficult

By way of update - I looked at the Action log in Drafts and it shows this;

“Copied to clipboard.
The file “compiler-669981795.704123.scpt” couldn’t be opened because you don’t have permission to view it.”

Drafts has accessibility permissions in the security tab area so not quite sure where else in system preferences to look

yes - you run into permission problems - very common with the current MacOS. It is awful what Apple created here.
I had the same problems here - it was a try and error approach to solve it. I’m sorry that I can’t give you any hints but: search for Automator/AppleScript and security permissions. There is a way to solve this!

This is me too. Wish I could get this thing working. The exact error that I get in the Action Log is as follows:

The operation couldn’t be completed. /Users/fidelnamisi/Library/Application Scripts/com.agiletortoise.Drafts-OSX/Temp/applescript-691338200.242875.scpt: execution error: Can’t make paragraphs 2 thru -1 of “testing” into type string. (-1700)

I believe I’ve found the solution. It’s really simple. The first time you run the script, make sure your note has a #Title specified. That’s because error -1700 means “Bad parameter data was detected or there was a failure while performing a coercion.” This information led me to experiment with adding a title before exporting to TBX, because the script looks for a title in the first step…

After exporting a note from Drafts App to TBX with a title, the very first time you run the script, it seems you don’t need to always add a title everytime. It just works from that point, title or no… Why? That’s beyond me :sweat_smile:

1 Like