2011/06/24

Send DEVONthink tags to BibDesk as keywords

Continuing in the same spirit as in the previous post, I wrote an AppleScript for sending DEVONthink tags to BibDesk. Only one item should be selected in DEVONthink and BibDesk. No duplicate keywords are created in BibDesk.

The AppleScript code is the following:
tell application "BibDesk"
    --only one item should be selected in BibDesk
    set thePub to the selection of document 1
    set keywordsPub to keywords of (item 1 in thePub)
   
    set old_delimiters to AppleScript's text item delimiters
    --the space inside quotes is very important!
    set AppleScript's text item delimiters to ", "
    set keywordsPubList to text items of keywordsPub
    set AppleScript's text item delimiters to old_delimiters
   
end tell

tell application "DEVONthink Pro"
    set thisSelection to the selection
    if thisSelection is {} then error "Please select something"
    if (length of thisSelection) is greater than 1 then error "Please select only one item"
    set the clipboard to ""
    set thisItem to (item 1 of thisSelection)
    set tagsItem to tags of thisItem
   
    repeat with x from 1 to the count of tagsItem
        set positionOfDuplicate to list_position(item x of tagsItem, keywordsPubList) of me
       
        if positionOfDuplicate is 0 then
            set keywordsPub to keywordsPub & ", " & item x of tagsItem
        else
            set keywordsPub to keywordsPub
        end if
       
    end repeat
   
end tell
tell application "BibDesk"
    if the first character of keywordsPub is "," then
        set keywordsPub to text 3 thru -1 of keywordsPub as text
    end if
    set keywords of (item 1 in the thePub) to keywordsPub
end tell

--subroutine from "AppleScript 1-2-3" p.567 for checking for duplicates
on list_position(this_item, this_list)
    repeat with i from 1 to the count of this_list
        if item i of this_list is this_item then return i
    end repeat
    return 0
end list_position

No comments: