Tinderbox and Drafts (getdrafts.com)

You’re a genius!!! Worked perfectly.

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

1 Like

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

Hi Tom,

“//” or “/” should make no difference on the Mac as far as I can tell.
However the iCloud Drive doesn’t act like a normal Finder folder. The path to the iCloud drive should be “~/Library/Mobile\ Documents/com~apple~CloudDocs/” - while the tilde is a shortcut for the current users home directory (if you like to address a specific user instead of the current user: replace the tilde with /Home/USERNAME as you did).

So “~/Library/Mobile\ Documents/com~apple~CloudDocs/TinderboxICloudDrive/_Tbxs/_ActiveTbxs/m/_me.tbx” should do it too.

Detlef

Sorry, not my area of expertise. however, on reading, I note the following…

Um, I think a double // snuck in the middle there, after ‘CloudDocs’. Shouldn’t the path be

"~/Library/Mobile\ Documents/com~apple~CloudDocs/_Tbxs/_ActiveTbxs/m/_me.tbx"

But looking at @TomD’s example the path is actionally

"~/Library/Mobile\ Documents/com~apple~CloudDocs/TinderboxICloudDrive/_Tbxs/_ActiveTbxs/m/_me.tbx"

…otherwise we lose the ‘TinderboxICloudDrive’ folder.

It’s confusing that the tilde ~ gets used two different ways. The one at the start is a Unix-style ‘shortcut’ for the user’s home directory. For use with short user account name ‘xyz’, expaning the first tilde gives:

"/Users/xyz/Library/Mobile\ Documents/com~apple~CloudDocs/TinderboxICloudDrive/_Tbxs/_ActiveTbxs/m/_me.tbx"

The second use of the tilde is a a a literal character in the folder named ‘com~apple~CloudDocs’. So, not really confusing at all. :roll_eyes:

1 Like

Thank you @webline for this very helpful script.

Is there a way of batch-sending a couple of Drafts-Notes to Tinderbox simultaneously?

Hi Andreas,

this should be possible - you need a loop to collect all items in Drafts and a loop in TBX to create individual notes. If I find the time I’m happy to create an update of my script - or try it yourself - not to complicated…

Best,

Detlef