Tinderbox and Drafts (getdrafts.com)

Paul, you’re right in that this will work for the drafts note body, but it is not clear to me how the process could work for populating the tags, title, and other attributes. Perhaps the Drafts templated to make this work. Not sure.

I didn’t say it would.

HI @midas0441, love this script. I do have a question and could use your help (@sumnerg, maybe you have an idea).

When I use this script the $Name of the note in Tinderbox becomes the ensure body of my Drafts note. Is there a way that I can adjust the Apple Script so the $Name of the not is the first line of the Drafts file? I’d love to learn Apple Script to make the change myself, but I don’t know where to start.

I don’t use Drafts so can’t test but it will probably work by replacing

set theTitle to the clipboard

with

set theTitle to paragraph 1 of (the clipboard)
2 Likes

You’re a genius!!! Worked perfectly.

Do you have a recommended resource for learning how to write Apple Script?

2 Likes

Nope. I‘ve started by modifying scripts. That was far from ideal.

Here are some things that could be useful.

How to get started with writing AppleScript

  • Don’t modify existing scripts.
    Read and try to understand the steps taken in a script, then start writing your own.

  • Do use descriptive variable names.
    Using self-explanatory variable names makes it easier for you to understand what your script does while writing it and in the future. It also makes it easier for other users, e.g. when you post one that you need help with. You’ll often find variable names used in someone else’s script cryptic or not descriptive enough. Change these to your own when you write your script.

  • Do use comments.
    -- or # in front of a line comments it out. Use them to deactivate code while testing something or to add your comments.

  • Start every script with a short description of the desired outcome.

How to get started with searching

  • Use e.g. startpage.com to search for AppleScript plus the command or idea you have.

    • Do not rapidly scan search results
      Even if a result doesn’t seem to answer your current question there’s a value in reading it. This way I’ve found countless things I didn’t know were possible.
    • Do not skip results that seem to rely on an app you don’t use.
      There’s value in reading them too. See above.
    • Capture what could someday be of interest.
      Create a group in DEVONthink and simply store everything in it. Use a searchable format, e.g. webarchive.
  • Searching in an app’s forum is different.

    • Don’t search for AppleScript.
      In my experience most posts that include an AppleScript do not contain the term “AppleScript”. Instead you’ll find posts of users asking for a script.
    • Do search for something that’s part of almost all scripts, e.g. tell application

How to get started with understanding what happens in a script

  • Download Script Debugger.

  • Read Script Debugger’s help.

  • Use Script Debugger’s Results & Variables Tab

  • Try

  • Try

  • Try

:slight_smile:

2 Likes

Definite +1 for Script Debugger if doing more than tinkering. does what it says in the app name. I use it occasionally, but it’s a real help when I do use it.

1 Like

There is also Sal Soghoian and Bill Cheeseman book “AppleScript 1-2-3” and Matt Neuburg “AppleScript The Definitive Guide”

1 Like

Rosenthal’s “AppleScript” (pub. Apress) has also been useful. This and the books above have all been out a while so can likely be picked up for very little money from somewhere like abebooks.com.

The forums at MacScripter have also been helpful along the way, especially for old hands to helpfully confirm that some things that ought to work just don’t (but there there’s usually an alternative approach).

2 Likes

These steps work well for me and I’m grateful to all of you for the tutorial.

I would mention that this script requires an “Inbox” note or container in the target TBX file in order to work properly; not sure anyone mentioned that.

These may also be helpful @satikusala - AppleScript: Beginner's Tutorial

I modified the script according to my personal needs. I wanted to get the title of the Drafts note to become the $Name attribute of the TBX note - without having to select any text. And I wanted the title to be removed from the content in the $Text attribute. Ah - and a link to the note will be added as $ReferenceURL.

-- The action must be initiated from a note in Drafts.app while the title of the note will become the $Name of the TBX note. 
-- the script is based on the work of midas0441 (TBX forum)

on execute(draft)
	set theTBXFile to "/Users/detlefbeyer/Desktop/test.tbx"

	set theTitle to title of the draft
	set theFullBody to the content of the draft
    set theCleanBody to returnContentWithoutFirstLine(theFullBody)
 	set theCreateDate to ("" & createdAt of the draft)
	set theModifyDate to ("" & modifiedAt of the draft)
	set theTags to (tags of the draft)
	set theListOfTags to convertListToString(theTags, "; ")
    set theLinkToDrafts to the permalink of the draft
	
	tell application id "Cere"
		open theTBXFile
		
		tell front document
			set theContainer to note "inbox"
			set myNote to make new note with properties {name:theTitle} at theContainer
			tell myNote
				set value of attribute "Text" to theCleanBody
				set value of attribute "Created" to theCreateDate
				set value of attribute "Modified" to theModifyDate
				set value of attribute "Tags" to theListOfTags
				set value of attribute "ReferenceURL" to theLinkToDrafts
				set value of attribute "Prototype" to "Markdown"
				set value of attribute "DisplayedAttributes" to "Created;Modified;Tags;ReferenceURL"
			end tell
		end tell
	end tell
end execute

on convertListToString(theList, theDelimiter)
	set AppleScript's text item delimiters to theDelimiter
	set theString to theList as string
	set AppleScript's text item delimiters to ""
	return theString
end convertListToString

on returnContentWithoutFirstLine(allContent)
	set theResult to ""
	set theFirstNonEmptyLine to returnTheFirstNonEmptyLine(allContent)
	set text item delimiters to return
	set theResult to (paragraphs theFirstNonEmptyLine thru -1) of allContent as string
	set text item delimiters to ""

	return theResult
end returnContentWithoutFirstLine

on returnTheFirstNonEmptyLine(allContent)
	set theResult to 2
	repeat with i from 2 to the number of paragraphs in allContent
		if paragraph i of allContent is not "" then
			set theResult to i
			exit repeat
		end if
	end repeat

	return theResult
end returnTheFirstNonEmptyLine

Next thing is that I would like to have the note in Drafts moved to the archive. I found no method in AppleScript so I added a 2nd step using JavaScript:

// Drafts - Archive
// move draft into the Archive
draft.isArchived = true;
draft.update();

That’s it.

1 Like

Many thanks Detlef for the excellent script above.

One slight debug error I found is:
‘set value of attribute “Prototyp” to “Markdown”’

Prototype is misspelled: Prototyp → Prototype

and for those new to applescript, you will need to modify your own location here:
set theTBXFile to “/Users/detlefbeyer/Desktop/test.tbx”

Many thanks Detlef

Tom

[Admin note: As admin access allows me to make edits after the grace period, I’ve amended the typo above to assist others with copy/pate of the code. :slight_smile: ]

1 Like

Hi Tom - this was the only line I didn’t copy but wrote directly here into the forum :wink:
I’m still thinking if I should set the TBX prototype via Script in Draft or add an Edict to the Tinderbox “inbox” container - both option will do it…

:crazy_face:

Or, you could set the $Prototype in the OnAdd action of the inbox container.

As admin access allows me to make me make edits after the grace period, I’ve amended the typo above to assist others with copy/paste of the code. :slight_smile:

1 Like

One other, minor correction in the line:

set value of attribute “KeyAttributes” to “Created;Modified;Tags;ReferenceURL”

Tinderbox renamed KeyAttributes → “DisplayedAttributes”

I would recommend also updating the code to reflect the change.
Thanks
Tom

1 Like

Good point. Will amend.

Done, in both code examples up-thread.

To explain, DisplayedAttributes replaced KeyAttributes (and associated attributes in v8 [sic]. A new doc continues to contain the old attributes and for legacy support only the Displayed Attributes and Key Attributes values map to each other.

But, in v9 we shouldn’t be using new code that only works for legacy compatibility reasons. IOW, it may need updating in due course when using Displayed Attributes will keep working. :slight_smile:

Ok, I promise this is the last comment code I found.
This line works as is if I used a tinderbox file that was place in the $Path location like the “Desktop”. Mine was not located there. Interestingly, it failed for my files stored in iCloud. See below

‘set theTBXFile to “/Users/YourUserNameHere/Desktop/test.tbx”’

Here is what I changed to make it work for my tinderbox file in icloud NB the “/” at the beginning was changed to “//”

“//Users/YourUserNameHere/Library/Mobile Documents/com~apple~CloudDocs/TinderboxICloudDrive/_Tbxs/_ActiveTbxs/m/_me.tbx”

I do not understand the difference in using $Path names with Applescript syntax but perhaps MarkA or someone else can explain the logic. I think it has to do with the space in the “Mobile Documents” name but I am unsure.

I posted this to help others. My script is working great now with the above changes.

Cheers,
Tom