Executing Script

I use BTT and DevonThink 3.

I have assigned a gesture to execute a script for DevonThink 3.

It works BUT.
Why doesn't the script execute the first time? It seems to me that the first gesture gets lost.

I have to repeat the gesture(second time) so it executes, this happens whether DevonThink3 is active, not active, front , not front.

*This happens just ONCE. It happens again if I close DevonThink then reopen it.

Any reason why the first gesture doesn't work?

can you post the script? for BTT there is no difference in how it handles the script on first or second run (unless there is some bug)

Sorry I don't know how to post script here, but here it is.

Thank you.


Summary
use AppleScript version "2.4" -- Yosemite (10.10) or later

use scripting additions

(* SETUP REFERENCE

Sidebar

1 Navigate keystroke 1 using {option down, command down}

2 Reading List keystroke 2 using {option down, command down}

3 Import keystroke 3 using {option down, command down}

4 Extras keystroke 4 using {option down, command down}

View

5 as Icons keystroke 1 using {command down}

6 as List keystroke 2 using {command down}

7 as Columns keystroke 3 using {command down}

8 Cover Flow keystroke 4 using {command down}

Preview

9 None keystroke 5 using {command down}

10 Standard keystroke 6 using {command down}

11 Widescreen keystroke 7 using {command down}

InspectorBar

12 Generic keystroke 1 using {control down}

13 Custom keystroke 2 using {control down}

14 Annotations & Reminder keystroke 3 using {control down}

15 Table of Contents keystroke 4 using {control down}

16 Thumbnails keystroke 5 using {control down}

17 Properties keystroke "p" using {control down}

18 Annotations keystroke 6 using {control down}

19 Links keystroke 7 using {control down}

20 List keystroke 8 using {control down}

21 Clouds keystroke 9 using {control down}

22 See Also & Classify keystroke "s" using {control down}

23 Search keystroke "u" using {control down}

*)

global theDisplayList, theIdxList, theNameList, theSeparatorList, theDBList, theFullList, theViewList

global hd1, hd2, hd3, hd4, hd5

global theDB, theDBID, theDBRoot, theDBSel

property labelView : false

property labelDB : false

property dbListOnly : true

set theDisplayList to {1, 3, 6, 7, 9, 10, 11, 12, 13}

set theSeparatorList to {2, 2, 3, 2}

activate application "DEVONthink 3"

tell application id "DNtp"

try

set {sym, len, align} to {"_", "25", "C"}

set theIdxList to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}

set theNameList to {"Navigate", "Reading List", "Email and Import", "Extras", "Icons View", "List View", "Columns View", "Cover Flow", "None", "Standard", "Wideccreen", "Generic", "MD-CustomMD", "Annotations & Reminder", "Table of Contents", "Thumbnails", "Properties", "Annotations", "Links", "List", "Clouds", "See Also & Classify", "Search", "Documents-Only"}

--If no section title but only view-section seperator is wanted

set hd1 to { my setWrappingLine("", sym, len, align), ""}

set hd2 to { my setWrappingLine("", sym, len, align), ""}

set hd3 to { my setWrappingLine("", sym, len, align), ""}

set hd4 to { my setWrappingLine("", sym, len, align), ""}

set hd5 to { my setWrappingLine("", sym, len, align), ""}

-- if separator titles are wanted

--set hd1 to {my setWrappingLine("Sidebar", sym, len, align), ""}

--set hd2 to {my setWrappingLine("View", sym, len, align), ""}

--set hd3 to {my setWrappingLine("Preview", sym, len, align), ""}

--set hd4 to {my setWrappingLine("InspectorBar", sym, len, align), ""}

--set hd5 to {my setWrappingLine("Database", sym, len, align), ""}

set theDBList to my getDBList()

set theViewList to my prepareList(theDisplayList, theIdxList, theNameList, theSeparatorList)

if dbListOnly then

set theFullList to theDBList

else

set theFullList to theViewList & theDBList

end if

my selectActionFrList(theFullList)

activate

end try

end tell

script D

-- 5 viewer windows, 20 dbs each, item 1's item 1 is viewer window id, item 1's item 1 is eXist or Nonexist

property vws : {{"", "", {{"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}}}, ¬

{"", "", {{"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}}}, ¬

{"", "", {{"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}}}, ¬

{"", "", {{"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}}}, ¬

{"", "", {{"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}}}}

-- property vwdbs : {"", "", {{"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}, {"", "", {}, ""}}}

end script

on getDBList()

local dbl, db, dbs, l -- all opened databases,database, current database selection

local theDB, theDBID, theDBRoot, theDBQuery

local vwdbs -- list contains all databases in current viewer window

local vids, vid -- all opned view windows, the id of current view window

set str to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

tell application id "DNtp"

set {vids, dbl, l} to {{}, {}, {}}

set db to databases

repeat with each in db as list

set end of dbl to {name of each, id of each, uuid of each}

end repeat

set dbl to my sortLOLByItemIdex(dbl, 2)

set vid to id of viewer window 1

repeat with each in viewer windows

set end of vids to id of each

end repeat

repeat with i from 1 to length of D's vws

if ( item 1 of item i of D's vws) is not in vids then set ( item 2 of item i of D's vws) to ""

end repeat

set {vpos, vstatus, vwdbs} to my lolLookup(vid, 1, 2, D's vws)

if vpos = 0 then

set {vpos, vstatus, vwdbs} to my lolLookup("", 1, 2, D's vws)

set item 1 of vwdbs to vid

set item 2 of vwdbs to "A"

set dbs to item 3 of vwdbs

else

set dbs to item 3 of vwdbs

end if

repeat with i from 1 to length of dbl

set item 1 of item i of dbs to item 3 of item i of dbl

end repeat

set theDB to database of (root of think window 1)

set theDBID to id of theDB

set theDBRoot to uuid of (root of viewer window 1)

try

set theDBQuery to search query of viewer window 1

on error

set theDBQuery to ""

end try

if selection ≠ {} then

set theDBSel to item 1 of (selection as list )

else

set theDBSel to {}

end if

set item 2 of item theDBID of dbs to theDBRoot

set item 3 of item theDBID of dbs to theDBSel

try

set item 4 of item theDBID of dbs to theDBQuery

on error

set item 4 of item theDBID of dbs to ""

end try

if labelDB then

repeat with i from 1 to length of dbl

set (dbl's item i)'s item 1 to (str's text i) & " " & ((dbl's item i)'s item 1 as string )

end repeat

end if

if dbListOnly is false then

set beginning of dbl to hd5

end if

return dbl

end tell

end getDBList

on prepareList(theDisplayList, theIdxList, theNameList, theSeparatorList)

local ld, lx, ln, ls, lst, l, xx, i, j

set {ld, lx, ln, ls} to {theDisplayList, theIdxList, theNameList, theSeparatorList}

set {l, lst} to {{}, {1}}

if labelView then

repeat with i from 1 to 9

set end of l to {i & " " & ( item (ld's item i) of ln) as string , item (ld's item i) of lx}

end repeat

else

repeat with i from 1 to 9

set end of l to {( item (ld's item i) of ln) as string , item (ld's item i) of lx}

end repeat

end if

repeat with j from 2 to 4

set end of lst to (ls's item (j - 1)) + (lst's item (j - 1)) + 1

end repeat

set beginning of l to hd1

set l to my insertItemInList({hd2}, l, lst's item 2)

set l to my insertItemInList({hd3}, l, lst's item 3)

set l to my insertItemInList({hd4}, l, lst's item 4)

return l

end prepareList

on selectActionFrList(theFullList)

local l, ln, lu, theItemChosen, theChoiceIdx, theChoice, theDestDB

local vid

tell application id "DNtp"

set l to theFullList

set {ln, lu} to my split2itemList(l)

set theItemChosen to choose from list ln with prompt "Switch To"

set theChoiceIdx to my indexOfOneItem(theItemChosen, ln)

set theChoice to item theChoiceIdx of lu

set vid to id of viewer window 1

set {vpos, vstatus, vwdbs} to my lolLookup(vid, 1, 2, D's vws)

set dbs to item 3 of vwdbs

if dbListOnly is false then

if labelView then

if ( character 1 of (theItemChosen as string )) is in "123456789" then

my SendShortCut(theChoice)

else

set theDestDB to database id theChoice

if item 2 of ( item theChoice of dbs) is "" then

set db to ( get record with uuid item 1 of ( item theChoice of dbs))

set the root of think window 1 to db

activate think window 1

else

set root of think window 1 to get record with uuid ( item 2 of ( item theChoice of dbs))

set selection of think window 1 to {( item 3 of ( item theChoice of dbs))}

if ( item 4 of ( item theChoice of dbs)) is not "" then set search query of viewer window 1 to ( item 4 of ( item theChoice of dbs))

activate think window 1

end if

end if

else

if theChoiceIdx ≤ 13 then

my SendShortCut(theChoice)

else if theChoiceIdx ≥ 15 then

set theDestDB to database id theChoice

if item 2 of ( item theChoice of dbs) is "" then

set db to ( get record with uuid item 1 of ( item theChoice of dbs))

set the root of think window 1 to db

activate think window 1

else

set root of think window 1 to get record with uuid ( item 2 of ( item theChoice of dbs))

set selection of think window 1 to {( item 3 of ( item theChoice of dbs))}

if ( item 4 of ( item theChoice of dbs)) is not "" then set search query of viewer window 1 to ( item 4 of ( item theChoice of dbs))

activate think window 1

end if

end if

end if

else

set theDestDB to database id theChoice

if item 2 of ( item theChoice of dbs) is "" then

set db to ( get record with uuid item 1 of ( item theChoice of dbs))

set the root of think window 1 to db

activate think window 1

else

set root of think window 1 to get record with uuid ( item 2 of ( item theChoice of dbs))

set selection of think window 1 to {( item 3 of ( item theChoice of dbs))}

if ( item 4 of ( item theChoice of dbs)) is not "" then set search query of viewer window 1 to ( item 4 of ( item theChoice of dbs))

activate think window 1

end if

end if

end tell

end selectActionFrList

on sortLOLByItemIdex(theList, i)

tell application id "DNtp"

set theIndexList to {}

set theSortedList to {}

repeat (length of theList) times

set theLowItem to ""

repeat with a from 1 to (length of theList)

if a is not in theIndexList then

set theCurrentItem to item a of theList

if theLowItem is "" then

set theLowItem to theCurrentItem

set theLowItemIndex to a

else if ( item i of theCurrentItem) comes before ( item i of theLowItem) then

set theLowItem to theCurrentItem

set theLowItemIndex to a

end if

end if

end repeat

set end of theSortedList to theLowItem

set end of theIndexList to theLowItemIndex

end repeat

return theSortedList

end tell

end sortLOLByItemIdex

on splitList(theList, firstNItemToKeep)

set list1 to {}

set list2 to {}

-- set a to ((length of theList's item 1) - firstNItemToKeep) as integer

set a to firstNItemToKeep

repeat with i from 1 to length of theList

set end of list1 to items 1 thru a of ( item i of theList)

set end of list2 to items (a + 1) thru -1 of ( item i of theList)

end repeat

return {list1, list2}

end splitList

on SendShortCut(idx)

tell application "System Events"

if idx is 1 then

delay 0.05

keystroke 1 using { option down , command down }

else if idx is 2 then

delay 0.05

keystroke 2 using { option down , command down }

else if idx is 3 then

delay 0.05

keystroke 3 using { option down , command down }

else if idx is 4 then

delay 0.05

keystroke 4 using { option down , command down }

else if idx is 5 then

delay 0.05

keystroke 1 using { command down }

else if idx is 6 then

delay 0.05

keystroke 2 using { command down }

else if idx is 7 then

delay 0.05

keystroke 3 using { command down }

else if idx is 8 then

delay 0.05

keystroke 4 using { command down }

else if idx is 9 then

delay 0.05

keystroke 5 using { command down }

else if idx is 10 then

delay 0.05

keystroke 6 using { command down }

else if idx is 11 then

delay 0.05

keystroke 7 using { command down }

else if idx is 12 then

delay 0.05

keystroke 1 using { control down }

else if idx is 13 then

delay 0.05

keystroke 2 using { control down }

else if idx is 14 then

delay 0.05

keystroke 3 using { control down }

else if idx is 15 then

delay 0.05

keystroke 4 using { control down }

else if idx is 16 then

delay 0.05

keystroke 5 using { control down }

else if idx is 17 then

delay 0.05

keystroke "p" using { control down }

else if idx is 18 then

delay 0.05

keystroke 6 using { control down }

else if idx is 19 then

delay 0.05

keystroke 7 using { control down }

else if idx is 20 then

delay 0.05

keystroke 8 using { control down }

else if idx is 21 then

delay 0.05

keystroke 9 using { control down }

else if idx is 22 then

delay 0.05

keystroke "s" using { control down }

else if idx is 23 then

delay 0.05

keystroke "u" using { control down }

else if idx is 24 then

delay 0.05

keystroke "d" using { control down , option down , command down }

end if

end tell

end SendShortCut

on setWrappingLine(str, sym, len, align)

set a to count str

set ul to "" as string

set ur to "" as string

if len < a then

repeat 3 times

set ul to ul & sym

end repeat

set w to ul & str & ul

else

if align is "C" then

set c to (len - a) / 2 - ((len - a) / 2 mod 1)

repeat c times

set ul to ul & sym

end repeat

set w to ul & str & ul

else if align is "L" then

set c to len - a - 3

repeat 3 times

set ul to ul & sym

end repeat

repeat c times

set ur to ur & sym

end repeat

set w to ul & str & ur

end if

end if

return w

end setWrappingLine

on insertItemInList(theItem, theList, thePosition)

-- cannot insert lol into beginning or last of a list

--source https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateListsofItems.html

set theListCount to length of theList

if thePosition is 0 then

return false

else if thePosition is less than 0 then

if (thePosition * -1) is greater than theListCount + 1 then return false

else

if thePosition is greater than theListCount + 1 then return false

end if

if thePosition is less than 0 then

if (thePosition * -1) is theListCount + 1 then

set beginning of theList to theItem

else

set theList to reverse of theList

set thePosition to (thePosition * -1)

if thePosition is 1 then

set beginning of theList to theItem

else if thePosition is (theListCount + 1) then

set end of theList to theItem

else

set theList to ( items 1 thru (thePosition - 1) of theList) & theItem & ( items thePosition thru -1 of theList)

end if

set theList to reverse of theList

end if

else

if thePosition is 1 then

set beginning of theList to theItem

else if thePosition is (theListCount + 1) then

set end of theList to theItem

else

set theList to ( items 1 thru (thePosition - 1) of theList) & theItem & ( items thePosition thru -1 of theList)

end if

end if

return theList

end insertItemInList

on split2itemList(theList)

set list1 to {}

set list2 to {}

repeat with i from 1 to length of theList

set end of list1 to item 1 of ( item i of theList)

set end of list2 to item 2 of ( item i of theList)

end repeat

return {list1, list2}

end split2itemList

on indexOfOneItem(theItem, theList)

-- credits Emmanuel Levy

set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}

set theList to return & theList & return

set AppleScript's text item delimiters to oTIDs

try

-1 + ( count ( paragraphs of ( text 1 thru ( offset of (return & theItem & return) in theList) of theList)))

on error

0

end try

end indexOfOneItem

on lolLookup(lookupVal, lookUpPos, getValPos, theList)

--only for list of list with more than 1 items

local i, j, k

set j to lookUpPos

set k to getValPos

repeat with i from 1 to length of theList

if ( item j of item i of theList) is equal to lookupVal then return {i, item k of item i of theList, item i of theList}

end repeat

return {0, {}, {}}

end lolLookup

on firstN(n, s)

return text from character 1 to character n of s

end firstN

This is another script, I execute it when I'm on Safari ( It saves window URLs to DevonThink3)
Again, I have to execute it twice in order to work.

Here, I use Keyboard sequence.


property pURL : ""
property pName : ""

set the date_stamp to ((the current date) as string)
set NoteTitle to "URL List from Safari Tabs on " & the date_stamp

tell application "Safari"
set pURL to URL of current tab of window 1
set pName to name of current tab of window 1
end tell

tell application "DEVONthink 3"
create record with {name:pName, URL:pURL, type:bookmark}
end tell