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

2011/06/18

Convert DEVONthink tags to MediaWiki categories

DEVONthink has a quite efficient tagging capability. Especially, the autocomplete feature of the tag bar, at the lower part of a DEVONthink window, saves me a lot of time and provides increased consistency in my tags.

Moreover, I maintain a separate page in my wiki for each book or paper that I have read. There I write all my notes and thoughts about each source. MediaWiki's name for tag is category and the required markup code for it is the following:
[[Category:tagName]] 
As I did not want to insert manually the tags for each source twice I wrote an AppleScript, which converts DEVONthink tags to MediaWiki categories. One item should be selected in DEVONthink before executing the script. The result is sent to the clipboard.

The AppleScript code is the following:
--2011-06-18
--http://organognosi.blogspot.com

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 newCategories to ""
    repeat with thisItem in thisSelection
        set tagsItem to tags of thisItem
        repeat with eachTag in tagsItem
            set tagForCategory to eachTag
            if tagForCategory is in {"Linked with MediaWiki"} then
                set newCategories to newCategories
            else
                set newCategories to "[[category:" & tagForCategory & "]] "
                set the clipboard to (the clipboard) & newCategories
            end if
        end repeat
    end repeat
end tell

2011/06/10

Export Skim notes according to their highlight colors

Adam Cronkright mentioned, in a thread in the Skim forum, that he uses different colors for highlighting various sections of a PDF, which have a common characteristic (e.g. author's points he agrees with, points he disagrees with, good phrases...). His problem is that he would like to have an efficient way to organize these different categories of notes and Skim's present features does not help him much to this end.

I think that a way to solve this problem is to use a specialized app for note management and an AppleScript, which exports Skim notes according to their color. So I found a reason to write an additional AppleScript or actually two!

The first is a simplified version, which has only one prerequisite for execution. Particularly, you need to use one or more of the following colors: ice, honey drew, flora, lemon, cantaloupe, silver, which are shown below.


You can change a color in the Favorite Colors by dragging a new color from the Color Panel (Tools> Show Colors) and drop it on the Favorite Colors toolbar item. Hold down the Option key to add a new color. You can remove a color from the Favorite Colors by dragging it to the Trash (from Tips and Tricks page in Skim's wiki).

Each color in Skim is represented as a list of four numbers (red, green, blue, alpha). The fourth number is constant (65535). For example, the ice color is represented by the list  {26214, 65535, 65535, 65535}. So you can change the hardwired colors in the scripts if you know the corresponding RGB (red, green, blue) code in 16-bit format for your preferred colors. Then you can add it in in the chooseColor subroutine (see the AppleScript below). 

When you execute the script the following windows will open consecutively:


You can choose more than one colors by pressing cmd + click.



If you choose "Some" then you will be asked to give the range of pages from which the notes will be extracted.


 

The result is sent to the clipboard. The code is the following:
--2011-06-10
--http://organognosi.blogspot.com/
tell application "Skim"
    set the clipboard to ""
    set numberOfPages to count pages of document 1
    activate
    set myColorCodes to my chooseColor()
    display dialog "Do you want to export all the notes or some of them?" buttons {"All", "Some"} default button 1
    set answer to button returned of the result
   
    if answer is "All" then
        set firstPage to "1" as number
        set lastPage to numberOfPages
        set the clipboard to "Skim notes" & return
    else
        display dialog "Give the number of the first page." default answer ""
        set firstPage to text returned of the result as number
       
        display dialog "Give the number of the last page" default answer ""
        set lastPage to text returned of the result as number
    end if
   
    repeat with currentPage from firstPage to lastPage
        set pageNotes to notes of page currentPage of document 1
        exportPageNotes(pageNotes, currentPage, myColorCodes) of me
    end repeat
   
end tell

on exportPageNotes(listOfNotes, pageForProcessing, myColorCodes)
    tell application "Skim"
       
        set currentPDFpage to pageForProcessing
        repeat with coloredNote in listOfNotes
           
            repeat with i from 1 to the count of myColorCodes
                if color of coloredNote is item i of myColorCodes then
                    set noteText to get text for coloredNote
                   
                    set the clipboard to (the clipboard) & noteText & return & return
                end if
            end repeat
        end repeat
       
    end tell
end exportPageNotes

on chooseColor()
    set selectedColors to (choose from list {"ice", "honey drew", "flora", "lemon", "cantaloupe", "silver"} with prompt ("Choose the color of notes for exporting (you can select multiple colors):") default items {"lemon"} with multiple selections allowed)
   
    set colorCodes to {}
    set noteColor to ""
    repeat with noteCol in selectedColors
        set noteColor to noteCol as text
        if noteColor is "ice" then
            set end of colorCodes to {26214, 65535, 65535, 65535}
        else if noteColor is "honey drew" then
            set end of colorCodes to {52428, 65535, 26214, 65535}
        else if noteColor is "flora" then
            set end of colorCodes to {26214, 65535, 26214, 65535}
        else if noteColor is "lemon" then
            set end of colorCodes to {65535, 65535, 2, 65535}
        else if noteColor is "cantaloupe" then
            set end of colorCodes to {65535, 52428, 26214, 65535}
        else if noteColor is "silver" then
            set end of colorCodes to {52428, 52428, 52428, 65535}
        end if
    end repeat
   
    return colorCodes
end chooseColor

The second AppleScript is that which I use and is quite more complicated. As you may know, my preferred program for managing notes is MediaWiki so the AppleScript's result is customized for import in a MediaWiki page.

Some additional features of this AppleScript are the following:
  • a hyperlink to the exact PDF page is created for each exported note
  • the annotation's date and time are added at the end of each exported note
  • the notes from each page are separated by a line 
  • the extended text of the note is exported as well
  • the first five notes of the first page are not exported
  • the written page numbers should be given in the dialog windows

If you want to use this AppleScript you need firstly to create the five Skim notes in the first PDF page as I describe in my post "How to create correctly the Skim notes which have the DEVONthink links, when you have already annotated the first page of the PDF document.

The code is the following:


--2011-06-10
--http://organognosi.blogspot.com/
tell application "Skim"
    set the clipboard to ""
    set numberOfNote5 to (get text for note 5 of page 1 of document 1) as string
    set pdfTitle to get (extended text of note 4) of page 1 of document 1 as string
    set numberOfPages to count pages of document 1
   
    activate
    set myColorCodes to my chooseColor() --εκτός των loops πρέπει να βρίσκεται, μια φορά το θέτεις
   
    display dialog "Do you want to export all the notes or only some of them?" buttons {"All", "Some"} default button 1
    set answer to button returned of the result
   
    if answer is "All" then
        set firstPage to "1" as number
        set lastPage to numberOfPages
        set the clipboard to "===Skim notes===" & return
    else
        display dialog "Give me the written number of the first page." default answer ""
        set firstPageWritten to text returned of the result as number
        set firstPage to firstPageWritten - numberOfNote5 as number
       
        display dialog "Give me the written numbers of the last page" default answer ""
        set lastPageWritten to text returned of the result as number
        set lastPage to lastPageWritten - numberOfNote5 as number
       
        set the clipboard to (the clipboard) & "Notes from \"[[@" & pdfTitle & "]]\" (pages " & firstPage & " - " & lastPage & ") <br />" & return
       
    end if
   
    repeat with currentPage from firstPage to lastPage
        --special provision for page 1
        if currentPage is 1 then
            set pageNotes to notes of page 1 of document 1
            set notesAfter5 to items 6 thru -1 of pageNotes
           
            exportPageNotes(notesAfter5, currentPage, myColorCodes) of me
        else
            set pageNotes to notes of page currentPage of document 1
           
            exportPageNotes(pageNotes, currentPage, myColorCodes) of me
        end if
    end repeat
   
end tell

on exportPageNotes(listOfNotes, pageForProcessing, myColorCodes)
    tell application "Skim"
        set numberOfNote5 to (get text for note 5 of page 1 of document 1) as string
        set pdfTitle to get (extended text of note 4) of page 1 of document 1 as string
        set numberOfPageNotes to count notes of page pageForProcessing of document 1
        set theWrittenPage to pageForProcessing + numberOfNote5 as string
        set pdfDevonThinkLink to (get text for note 4 of page 1 of document 1) as string
        set pdfDevonThinkLinkWihtoutZero to (text 1 thru ((length of pdfDevonThinkLink) - 1) of pdfDevonThinkLink) as string
       
        repeat with coloredNote in listOfNotes
           
            repeat with i from 1 to the count of myColorCodes
                if color of coloredNote is item i of myColorCodes then
                    set pdfText to get text for coloredNote
                    set pdfText2 to get extended text of coloredNote as text
                    set fullNoteText to pdfText & " " & pdfText2
                   
                    set annotationDate to modification date of coloredNote as text
                   
                    set pageForDevonThinkLink to pageForProcessing - 1
                   
                    --for use with MediaWiki semantic annotations
                    set firstCharacter to get the character 1 of fullNoteText
                    if firstCharacter = "[" then
                        set endofNote to "]]]"
                    else
                        set endofNote to "]"
                    end if
                   
                    set textAfterFullNoteText to "[" & pdfDevonThinkLinkWihtoutZero & pageForDevonThinkLink & " p. " & theWrittenPage & endofNote & " <small>(" & annotationDate & ")</small>"
                    set textForTextMate to fullNoteText & " " & textAfterFullNoteText
                    set textForTextMate2 to replaceText(textForTextMate, "missing value", "") of me
                    set textForTextMate3 to replaceText(textForTextMate2, " ()", "") of me
                   
                    set the clipboard to (the clipboard) & textForTextMate3 & return
                   
                end if
            end repeat
        end repeat
       
        set theSeperationLine to ""
        repeat with coloredNote in listOfNotes
            if numberOfPageNotes > 0 then
                repeat with i from 1 to the count of myColorCodes
                    if (color of coloredNote is item i of myColorCodes) then
                        set theSeperationLine to "----" & return
                        exit repeat
                    end if
                end repeat
            end if
        end repeat
        set the clipboard to (the clipboard) & theSeperationLine
    end tell
end exportPageNotes

on replaceText(thisText, searchString, replacementString)
    set AppleScript's text item delimiters to the searchString
    set the itemList to every text item of thisText
    set AppleScript's text item delimiters to the replacementString
    set thisText to the itemList as string
    set AppleScript's text item delimiters to {""}
    return thisText
end replaceText

on chooseColor()
    set selectedColors to (choose from list {"ice", "honey drew", "flora", "lemon", "cantaloupe", "silver"} with prompt ("Choose the color of notes for exporting (you can select multiple colors):") default items {"lemon"} with multiple selections allowed)
    --selectedColors is a list of lists
    set colorCodes to {}
    set noteColor to ""
    repeat with noteCol in selectedColors
        set noteColor to noteCol as text
        if noteColor is "ice" then
            set end of colorCodes to {26214, 65535, 65535, 65535}
        else if noteColor is "honey drew" then
            set end of colorCodes to {52428, 65535, 26214, 65535}
        else if noteColor is "flora" then
            set end of colorCodes to {26214, 65535, 26214, 65535}
        else if noteColor is "lemon" then
            set end of colorCodes to {65535, 65535, 2, 65535}
        else if noteColor is "cantaloupe" then
            set end of colorCodes to {65535, 52428, 26214, 65535}
        else if noteColor is "silver" then
            set end of colorCodes to {52428, 52428, 52428, 65535}
           
        end if
    end repeat
   
    return colorCodes
end chooseColor

I hope this AppleScript will help Adam! Anyway, your post was an inspiration for me. Finally, I would like to thank Martin who informed me about it.

2011/06/05

Add tags to many DEVONthink items at the same time

In DEVONthink, when two or more items have exactly the same tags, new tags can be added to them using the tag bar at the lower part of a DEVONthink window. But when their tags are different the message "Multiple Selection" appears in the bar and you cannot edit it. With the following AppleScript you can add multiple tags even to these items.
--2011-06-05
--http://organognosi.blogspot.com/

display dialog "Enter tags to add (separated by commas):" default answer ""
set newTags to text returned of the result

set old_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set tagList to text items of newTags
set AppleScript's text item delimiters to old_delimiters

tell application id "com.devon-technologies.thinkpro2"
   
    try
        set these_items to the selection
        if these_items is {} then error "Please select some contents."
       
        repeat with this_item in these_items
            set tags of this_item to (tags of this_item) & tagList
        end repeat
    end try
end tell

2011/06/01

Add keywords to many entries of BibDesk at the same time

The following AppleScript performs the above function:
tell application "BibDesk"
    set theSelection to the selection of document 1
    display dialog "Give the new keywords separated by commas:" default answer "" buttons {"Cancel", "OK"} default button 2
    set the newKeywords to the text returned of the result
   
    repeat with thePub in theSelection
        set oldKeywords to keywords of thePub
        if oldKeywords is "" then
            set keywords of thePub to newKeywords
        else
            set keywords of thePub to keywords of thePub & ", " & newKeywords
        end if
    end repeat
end tell

2011/05/31

Create MediaWiki links to local folders and files without any extension!

In my previous post "Embed AppleScripts in a MediaWiki page", I showed how an AppleScript, embedded in a HTML link, can be inserted in a MediaWiki page. A way that I use this MediaWiki feature is by creating links to local files and folders. Moreover, after executing the AppleScript, the files open with their assigned applications. Finally as you can see in the same post, the whole link syntax is quite complicated so I wrote an AppleScript, which creates automatically all the required code for the selected files and folders in Finder. The names of them should be in Latin characters.

The AppleScript code is the following:

--2011-05-31
--http://organognosi.blogspot.com

tell application "Finder"
    set selectedItems to the selection
    if selectedItems is {} then error "Please select some contents."
    set the clipboard to ""
    repeat with selectedItem in selectedItems
        set selectedItem to selectedItem as string
        set encodedText to my encode_URL_string(selectedItem)
       
        set embeddedAppleScript to "<html>" & return & "<a href=\"" & "apple" & "script" & "://com.apple.scripteditor?action=new&script= tell%20application%20%22Finder%22%0D
%09open%20(alias%20%22" & encodedText & "%22)%0D" & return & "end%20tell\"l>" & selectedItem & "</a>" & return & "</html>"
       
        set the clipboard to (the clipboard) & embeddedAppleScript & "<br/>" & return
    end repeat
end tell

--Text handlers from Apple to encode the selectedItem string
property allowed_URL_chars : (characters of "$-_.+!*'(),1234567890abcdefghijklmnopqrstuvwxyz")
on encode_URL_string(this_item)
    set character_list to (characters of this_item)
    repeat with i from 1 to number of items in character_list
        set this_char to item i of character_list
        if this_char is not in allowed_URL_chars then set item i of character_list to my encode_URL_char(this_char)
    end repeat
    return character_list as string
end encode_URL_string

property hex_list : (characters of "0123456789ABCDEF")
on encode_URL_char(this_char)
    set ASCII_num to (ASCII number this_char)
    set x to item ((ASCII_num div 16) + 1) of hex_list
    set y to item ((ASCII_num mod 16) + 1) of hex_list
    return ("%" & x & y) as string
end encode_URL_char

2011/05/29

Embed AppleScripts in a MediaWiki page

You can embed an AppleScript in a MediaWiki page if you insert the script as the URL in a proper HTML link. Particularly, the HTML link syntax looks like this:
<a href="URL">Link text</a>
The AppleScript code should be converted in a format which is compatible with HTML URLs. For example, each space in the script should be replaced by the string %20, each tab by %09 and each enter by %0D. Moreover, the "applescript://" URL scheme will be used and the whole link should be within <html>, </html> tags.   Fortunately the whole conversion can be done automatically by an AppleScript or using the freeware AppleScriptHTML app.

Finally, raw HTML code is not allowed by default in a public wiki for security reasons but if you maintain a private wiki you can permit it by setting
$wgRawHtml = true;
in the LocalSettings.php file.

For example, suppose you want to embed the following AppleScript
tell application "Finder"
    open (alias "MBSystem:Users:MB:Downloads:")
end tell
which opens the "Downloads" folder in a Finder window. Then, you should write the following code in a MediaWiki page:
<html>
<a href="applescript://com.apple.scripteditor?action=new&script= tell%20application%20%22Finder%22%0D
%09open%20(alias%20%22MBSystem%3AUsers%3AMB%3ADownloads%3A%22)%0D
end%20tell"l>MBSystem:Users:MB:Downloads:</a>
</html>
and you will see the link, after saving the page.


Afterwards, every time you click the link an AppleScript editor window opens with the AppleScript code inside it, as is shown in the next image.

Now, you may press cmd + R (executes the script), cmd + W (closes the window) and cmd + D (without saving)!

In my next post, I will present how you can create automatically HTML links to local files and folders, with AppleScript code inside them, which can be embedded in a MediaWiki page.

2011/05/20

Sync PDF page numbers between Skim's snapshot window and main window.

A quite useful feature of Skim is that "you can take "snapshots" of important sections (like the bibliography section) of a PDF document, to keep them on your screen for easy reference. This way you don't have to leaf back to the section every time you want to see it" (Skim help file).

But the snapshot windows are just for viewing the material. For example, you cannot highlight or select text in them. In order to make annotating of these pages easier I wrote an AppleScript, which instantly shows you in the main window, the current page in the snapshot window. When you finish annotating this page you can go back to the previous page by using the cmd + [ shortkey or by triggering the corresponding gesture (I use the "three finger left" gesture for this task).

If you use more than one snapshot windows from the same document, after selecting the page that you want to annotate in one of them, you need to click one time all the remaining snapshots windows, before running the AppleScript.

The AppleScript code is the following:

--2011-05-20
--http://organognosi.blogspot.com

set nameSnapshot to ""
tell application "Skim"
    set frontNameDocument to name of document 1
    set windowsNames to get the name of windows
   
    repeat with x from 1 to count of items of windowsNames
        set windowName to item x of windowsNames
        if windowName contains frontNameDocument & " —" then
            set nameSnapshot to windowName
        end if
    end repeat
   
    set position to offset of "—" in nameSnapshot
    set AppleScript's text item delimiters to ""
    set stg_numberPage to characters (position + 7) thru -1 of nameSnapshot as text
    set numberPage to stg_numberPage as number
   
    go document 1 to page numberPage of document 1
   
end tell

2011/05/17

Recreate the folder and file hierarchy in text form compatible with MediaWiki syntax

Let's say we have the following folder and file structure and we would like to pass it as text in a MediaWiki page.

With the AppleScript of this post you can create automatically the necessary text, which is for our example the following:

===DevonThink scripts===
Number of files: 0<pre>/MBSystem/Users/MB/Downloads/DevonThink scripts/</pre>

====Camino====
Number of files: 3<pre>/MBSystem/Users/MB/Downloads/DevonThink scripts/Camino/</pre>
Add PDF document to DEVONthink.scpt<br/>
Add link to DEVONthink.scpt<br/>
Add web document to DEVONthink.scpt<br/>

====DEVONagent====
Number of files: 11<pre>/MBSystem/Users/MB/Downloads/DevonThink scripts/DEVONagent/</pre>
Add PDF document to DEVONthink.scpt<br/>
Add abstracts to DEVONthink Pro.scpt<br/>
Add linked images to DEVONthink's downloads.scpt<br/>
Add linked images to DEVONthink.scpt<br/>
Add links to DEVONthink's downloads.scpt<br/>
Add links to DEVONthink.scpt<br/>
Add page to DEVONthink.scpt<br/>
Add selection to DEVONthink.scpt<br/>
Add tabs to DEVONthink.scpt<br/>
Add text to DEVONthink.scpt<br/>
Add web document to DEVONthink.scpt<br/>

====Firefox====
Number of files: 3<pre>/MBSystem/Users/MB/Downloads/DevonThink scripts/Firefox/</pre>
Add PDF document to DEVONthink.scpt<br/>
Add link to DEVONthink.scpt<br/>
Add web document to DEVONthink.scpt<br/>

====endo====
Number of files: 1<pre>/MBSystem/Users/MB/Downloads/DevonThink scripts/endo/</pre>
Add news to DEVONthink.scpt<br/>
As you can see the folder hierarchy is recreated as a hierarchy of MediaWiki sections. Also the number of files in each folder and its path are added under the name of the corresponding section. If you choose to apply this AppleScript in a folder structure with thousands of files there will no be any problem but it will take a long time to finish.  The result is sent to the clipboard.

The AppleScript code is the following:
set folderContents to show_folder_hierarchy(choose folder, "=") of me

set the clipboard to folderContents
--this subroutine is an expansion of an example from the book Learn AppleScript - The Comprehensive Guide to Scripting on Mac OS X (p. 485-486)
on show_folder_hierarchy(the_folder, the_indent)
    --process this folder
    tell application "Finder"
        set folderContents to get files of the_folder
        set numberOfFiles to count items of folderContents
       
        set folderName to the_folder as alias
        set folderNamePOSIX to get the POSIX path of folderName
        set folderPATH to "<pre>" & folderNamePOSIX & "</pre>"
       
        set fileNames to ""
        repeat with currentFile from 1 to numberOfFiles
            set fileNames to fileNames & name of item currentFile of folderContents & "<br/>" & return
        end repeat
        set the_result to the_indent & "==" & name of the_folder & "==" & the_indent & return & "Number of files: " & numberOfFiles & folderPATH & return & fileNames & return
    end tell
   
    --process each sub-folder in turn
    tell application "Finder"
        repeat with sub_folder_ref in (get every folder of the_folder)
            set the_result to the_result & my show_folder_hierarchy(contents of sub_folder_ref, the_indent & "=")
        end repeat
    end tell
    return the_result
   
end show_folder_hierarchy
2011-05-20 I updated the code which creates the POSIX paths of the folders.  
 

2011/05/01

Delete the duplicate Skim notes

With the following AppleScript you can delete the Skim notes which have exactly the same text in each page of a PDF file.

--2011-05-01
-- http://organognosi.blogspot.com

tell application "Skim"
    --the following code deletes the note text which is repeated one or more times in a PDF page
    set numberOfMyPages to count pages of document 1
    repeat with currentPage from 1 to numberOfMyPages
        set numberOfPageNotes to count notes of page currentPage of document 1
        set textOfCurrentPageNotes to text of notes of page currentPage of document 1 as list
       
        repeat with currentNoteNumber from 1 to numberOfPageNotes
            set noteText to (get text for note currentNoteNumber of page currentPage of document 1) as text
            set offsetPositionsOfDuplicates to list_positions(textOfCurrentPageNotes, noteText, true) of me
            set the numberOfDuplicates to length of offsetPositionsOfDuplicates
            repeat with noteForTextDeletion from 2 to numberOfDuplicates
               
                set noteNumber to item noteForTextDeletion of offsetPositionsOfDuplicates
                delete text of note noteNumber of page currentPage of document 1
            end repeat
        end repeat
    end repeat
    --the following code deletes the empty notes   
    set numberOfMyPages to count pages of document 1
    set numberOfAlltheNotes to count notes of document 1
    set indexesOfEmptyNotes to {}
    repeat with currentNote from 1 to numberOfAlltheNotes
        set noteText to (get text for note currentNote of document 1) as text
       
        if noteText is "" then
            set the end of the indexesOfEmptyNotes to currentNote as string
            set ABC to the length of indexesOfEmptyNotes
        end if
    end repeat
   
    repeat with x from 1 to ABC
        set indexNote to item x of indexesOfEmptyNotes as number
        set indexNote to indexNote - x + 1
        delete note indexNote of document 1
    end repeat
end tell

--the following subroutine is from Soghoian's AppleScript 1-2-3 book (p. 569)
on list_positions(this_list, this_item, list_all)
    set the offsetlist to {}
    repeat with i from 1 to the count of this_list
        if item i of this_list is this_item then
            set the end of the offsetlist to i
            if list_all is false then
                return item 1 of offset_list
            end if
        end if
    end repeat
   
    if list_all is false and offsetlist is {} then return 0
    return the offsetlist
end list_positions

2011/04/22

Count the words of a PDF file from Skim

Here is a small AppleScript for counting the words of a PDF file from Skim.

tell application "Skim"
    set PDFwords to 0
    set numberOfMyPages to count pages of document 1
   
    repeat with currentPage from 1 to numberOfMyPages
        set numberOfPageWords to count words of page currentPage of document 1
        set PDFwords to PDFwords + numberOfPageWords
    end repeat
   
    display dialog "Number of words: " & PDFwords buttons {"Cancel", "OK"} default button 2
end tell

2011/04/09

How to create hypermedia notes during your lectures!

There are some apps, which combine the features of a note taking and a voice recording app but in a very distinctive way. Specifically, they have the ability to continuously synchronize the recorded audio with your notes. As a result, each word acts as a link to the time point of the recorded audio at which it was written! Obviously this makes navigating a long lecture fast and easy and after a while you will start to appreciate the power of another kind of link!

Currently, I use "AudioNote" for taking my hypermedia notes mainly because there is a version of it both for iOS and Mac OS X. Consequently, I can read my notes and hear the synced audio in all my platforms. An other app with a somewhat more polished interface and richer organizational abilities is "Notability" but you can you use it only in your iPhone or your iPad.

2011/04/05

Automated creation of references with hyperlinks from Skim


In the post "Automated creation of a LaTeX compatible citation only from Skim! (with hyperlink included)" there is an AppleScript which creates a LaTeX reference in TextMate for the PDF file that you currently read in Skim. Now I will present an expanded and improved version of that AppleScript. Specifically the new AppleScript takes advantage of all my standardized notes in the first page of a PDF document.

These notes can be created automatically from the AppleScript in the post "How to create correctly the Skim notes which have the DEVONthink links, when you have already annotated the first page of the PDF document". Moreover you can find more information about these notes in my posts "How to put DevonThink links in Skim notes" and "Latin page numbers, Arabic page numbers and the fifth Skim note".

The references and hyperlinks that can be created from my new AppleScript are the following and correspond to the notes one through four:

1. A LaTeX reference to the exact PDF page which can be inserted in a MediaWiki page without any modifications or problems. Moreover, the necessary code for the creation of MediaWiki link is also included. An example is the following:
 (<rawtex>\cite{Deconstructing-the-Laws-of-Logic-Clark-2008a}</rawtex>: [x-devonthink-item://CDEC17B7-1EEE-42E1-B8C9-86A24C172BF3?page=2 27])
A prerequisite for the use of this reference is to have installed and properly customized the Wiki2LaTeX MediaWiki extension. I will write more about this and its importance in a future post.

2. A LaTeX reference that can be inserted in a text file together with the proper LaTeX code for the creation of the corresponding hyperlink. An example is the following:
(\cite{Deconstructing-the-Laws-of-Logic-Clark-2008a}: \href{x-devonthink-item://CDEC17B7-1EEE-42E1-B8C9-86A24C172BF3?page=2}{27})
3. A MediaWIki internal link to the wiki page of the source, followed by an external link to the PDF file itself. An example is the following:
([[Deconstructing the Laws of Logic - Clark]]: [x-devonthink-item://CDEC17B7-1EEE-42E1-B8C9-86A24C172BF3?page=2 27]) 
4. A DEVONthink url

How to use this AppleScript

You should run the AppleScript when you read a PDF file in Skim and you want to make a reference to the current page. Additionally, a TextMate file should be opened. After the execution you can select from a menu which kind of reference you would like to be created. All the necessary numbers for the hyperlinks and the references are automatically created with the help of the fifth note. You can see this AppleScript in action in the following video:



The AppleScript code is the following:
--2011-04-05
--http://organognosi.blogspot.com

tell application "Skim"
    activate
    set numberOfNote5 to (get text for note 5 of page 1 of document 1) as string
   
    set RefType to my chooseRefType({"Latex for MediaWiki", "Latex", "MediaWiki", "DEVONthink link"})
    if RefType is 0 then return
   
    tell document 1
        set currentPageNumber to get index of current page
        set currentPageNumberText to currentPageNumber as text
        set pageDevonThinkNumber to currentPageNumber - 1
        set pageDevonThinkNumberText to pageDevonThinkNumber as text
        set DevonThinkLink to get text of note RefType of page 1
        set theRealPage to currentPageNumber + numberOfNote5 as string
       
        tell application "TextMate"
            activate
            if RefType is 1 then
                insert DevonThinkLink
                delay 1
                tell application "System Events"
                    key code 123
                    key code 123
                end tell
                delay 1
                insert pageDevonThinkNumberText & " " & theRealPage
                tell application "System Events"
                    key code 124
                    key code 124
                end tell
            else if RefType is 2 then
                insert DevonThinkLink
                delay 1
                tell application "System Events"
                    key code 123
                    key code 123
                    key code 123
                    key code 123
                end tell
                delay 1
                insert pageDevonThinkNumberText
                tell application "System Events"
                    key code 124
                    key code 124
                end tell
                delay 1
                insert theRealPage
                tell application "System Events"
                    key code 124
                    key code 124
                end tell
            else if RefType is 3 then
                insert DevonThinkLink
                delay 1
                tell application "System Events"
                    key code 123
                    key code 123
                end tell
                delay 1
                insert pageDevonThinkNumberText & " " & theRealPage
                tell application "System Events"
                    key code 124
                    key code 124
                end tell
            else if RefType is 4 then
                insert DevonThinkLink
                delay 1
                tell application "System Events"
                    key code 51
                end tell
                delay 1
                insert pageDevonThinkNumberText
               
            end if
        end tell
       
    end tell
   
   
   
end tell

on chooseRefType(typeList)
    tell application "Skim"
        set theResult to choose from list typeList with prompt "Reference type:"
        if theResult is false then return 0
        set refTypeNumber to theResult as string
        if refTypeNumber is "Latex for MediaWiki" then
            return 1
        else if refTypeNumber is "Latex" then
            return 2
        else if refTypeNumber is "MediaWiki" then
            return 3
        else if refTypeNumber is "DEVONthink link" then
            return 4
        end if
    end tell
    return RefType
end chooseRefType

2011/04/02

Go to the written page number of a PDF document

In my previous post "Latin page numbers, Arabic page numbers and the fifth Skim note" I described the logic behind the number in the fifth note. Now we can use this information in order to go to the written page number of a PDF document without doing any calculations. This proves quite convenient especially in the case of academic papers in which the written numbers are totally out of sync with the page numbers which are given by Skim.

The AppleScript which performs this function is the following:

--2011-04-02
--http://organognosi.blogspot.com

tell application "Skim"
    display dialog "Give the written page number:" default answer "" buttons {"Cancel", "OK"} default button 2
    set writtenNumber to text returned of the result as integer
   
    set numberNote5 to (get text for note 5 of page 1 of document 1)
    set numberInThePDF to writtenNumber - numberNote5
    go document 1 to page numberInThePDF of document 1
end tell


2011/04/01

Latin page numbers, Arabic page numbers and the fifth Skim note

I have observed that my ebooks and papers which are in PDF format can be separated in one of the following three groups according to the relation between the page numbers that are given by Skim and the correspoding written numbers in the potentially printed PDF pages. 

The first group contains the PDF files in which these two numbers coincide for any given page.  In this case there is just the number zero in the fifth note in the first PDF page. This is the simplest case and an example of such a document is shown in the following image.


The second group contains ebooks in which Latin numbers are used in a number of pages such as in its table of contents. In this case I put in the fifth note the negative total number of pages that precede the first PDF page with the written Arabic number 1. For example lets say that an ebook has its cover in the first PDF page, followed by six pages with Latin numbers. As a result the first PDF page with the Arabic number 1 is the eighth PDF page and the number -7 is written in the fifth note. In the following image you can see a page from the aforementioned ebook.


By the way I do not find any value in the use of Latin numbers in electronic documents. To the contrary it makes the navigation of PDF files more difficult and cumbersome. One way to solve this problem is to move all the pages with Latin numbers and the cover at the end of the file. However, if you create these notes in your PDF files there is an alternative way of overcoming the problems which are caused by this relic of publishing tradition.

Finally the third group contains papers from academic journals in which the written page numbers are totally out of sync with the page numbers which are given by Skim. In this case I put in the fifth note the written number in the first PDF page decreased by one. An example of such a document is shown in the following image.


If there is an extra page with journal and paper information at the start of the document then you should substract by two.



In my next posts I will present what can be achieved with the help of the fifth note. 

2011/03/25

How to create correctly the Skim notes which have the DEVONthink links, when you have already annotated the first page of the PDF document

In my post "How to put DevonThink links in Skim notes" I described how you can create to a PDF file a number of Skim notes with various versions of references to itself. These notes are created in the first page of the document and an example of them is shown below:



In my post "Automated creation of a LaTeX compatible citation only from Skim! (with hyperlink included)" I write that "it is required that you create these notes when there are no annotations in the first page of the PDF file. Otherwise these four notes will take another index number and you will need to adjust accordingly the AppleScript".

Now it is time to overcome this limitation!

Specifically, I wrote an AppleScript which temporarily removes all your annotations from your PDF file, then creates the desirable notes with the correct index numbers and at the end reinserts your annotations. You should run this script when you have selected one or more PDF files from a DEVONthink database.

The AppleScript code is the following:
--2011-03-25
--http://organognosi.blogspot.com

tell application id "com.devon-technologies.thinkpro2"
    set these_items to the selection
    if these_items is {} then error "Please select some contents."
   
    repeat with this_item in these_items
       
        set RecordLink to the reference URL of this_item
        set DevonThinkLink to RecordLink & "?page=0"
        set PdfPath to get the path of this_item
        set PdfName to the name of this_item
       
        tell application "Skim"
            open PdfPath
            set docPath to path of front document
            set notesPath to text 1 thru -5 of docPath & " (notes).skim"
           
            save front document
            save front document in notesPath as "Skim notes"
           
            delete notes of front document
           
            tell page 1 of document 1
                make note with properties {type:anchored note, bounds:{523, 820, 540, 820}, text:"(<rawtex></rawtex>: [" & RecordLink & "?page=])", extended text:DevonThinkLink}
                delay 0.1
                make note with properties {type:anchored note, bounds:{523, 800, 540, 800}, text:"(: \\href{" & RecordLink & "?page=}{})"}
                delay 0.1
                make note with properties {type:anchored note, bounds:{523, 780, 540, 780}, text:"([[" & PdfName & "]]: [" & RecordLink & "?page=])"}
                delay 0.1
                make note with properties {type:anchored note, bounds:{523, 760, 540, 760}, text:DevonThinkLink, extended text:PdfName}
                delay 0.1
                make note with properties {type:anchored note, bounds:{523, 760, 540, 760}, text:"0"}
            end tell
            read notes front document from notesPath without replacing
        end tell
    end repeat
end tell

2011-04-02
Moreover, this AppleScript creates a fifth Skim note in the first PDF page with the default value zero. For more information about this note see my post "Latin page numbers, Arabic page numbers and the fifth Skim note".

2011/03/24

How to reveal the front Skim PDF document in Finder and in DEVONthink

You can use the following AppleScript to reveal the front Skim PDF document in Finder:

tell application "Skim"
    set FilePath to get the file of front document
    tell application "Finder"
        activate
        reveal FilePath
    end tell
end tell
You can use the following AppleScript to reveal the front Skim PDF document in DEVONthink:

tell application "Skim"
    set the xDEVONthinkLink to (get text for note 4 of document 1) as string
    set customUUID to text 21 thru -8 of xDEVONthinkLink as string
end tell

tell application "DEVONthink Pro"
    activate
    tell database "MySources"
        set myPDF to get record with uuid customUUID
        open tab for record myPDF
        tell application "System Events"
            tell process "DEVONthink Pro" to click menu item "Reveal" of menu 1 of menu bar item "Data" of menu bar 1
        end tell
    end tell
    close document window 1
end tell
In order for the last AppleScript to work properly, you need to have the DEVONthink URL in the fourth Skim note of the first page of the PDF in the following form:

x-devonthink-item://648847AF-4714-4328-916C-6169A6389124?page=0

In my post "How to put DevonThink links in Skim note" I describe how this can be done automatically. 

Moreover you need to adjust appropriately the name of the DEVONthink database  in the line:
    tell database "MySources" 

2011/03/17

The roadmap of my blog on-line in MindMeister

Yesterday, I found a new on-line mind mapping tool, named MindMeister and I uploaded there the mind map about the various kinds of links that I use in my workflow. Moreover it is now embedded in the original post "A road map for my blog - The many faces of links" and you can navigate it also from here. I think it deserves a second post in this, greatly improved and interactive form!


How to easily assign shortkeys and gestures to AppleScripts

In my daily workflow I use a significant number of AppleScripts, as you will have understoond from my previous posts :-). So it is handy for me to assign shortkeys to some of them. For this purpose I use the FastScripts utility.  Although it is not a commercial app, it gives you for free up to 10 keyboard shortcuts.
 
After the previous assignments you are able to trigger the execution of AppleScripts using customized gesture definitions with the help of BetterTouchTool app.

In the following screenshot you can see some of my Skim gestures:


How to automatically embed hyperlinks into citations and bibliographical entries of a downloaded PDF document! Part III

In the last post of this series I present how the embedding of hyperlinks is made in a sample PDF file. A prerequisite for the following actions it to have already created one or more link dictionaries (if you do not know how, read part II for detailed instructions and part I for more general related information).

Let's say that you have the PDF file opened in Skim. Then you should open it in Adobe Acrobat Pro in your Windows virtual machine. To make things faster you can have the Windows formatted path of this file in your clipboard by executing the AppleScript which I wrote in my post: "How to convert a Mac-Unix style path to a Windows style path from Skim".

In the following sreenshots you can see the required steps:




 

2011/03/11

How to automatically embed hyperlinks into citations and bibliographical entries of a downloaded PDF document! Part II

How to create the dictionaries

Firstly your ebooks and epapers should be inside inside a DevonThink database. Moreover each source should have its respective record in BibDesk. The "local URL" field of each record should have the DevonThink URL of the source. If you do not know how to do this you can read my post "How to connect a PDF file inside DevonThink with its record in BibDesk". Finally you should select the records in BibDesk which will constitute the records of the dictionary and you should have one TextMate txt file open. Now you are ready to execute either the AppleScript for the creation of the link dictionary with the titles or for the second kind of dictionary.

The AppleScript code for the link dictionary with the titles is the following:
--2011-03-11
--http://organognosi.blogspot.com
--Works with AutoBookmark 3.7
tell application "BibDesk"
    set these_items to the selection of document 1
    if these_items is {} then error "Please select some contents."
       
    repeat with this_item in these_items
        set DevonThinkLink to the linked URL 1 of this_item
        set PDfTitle to the value of the field "Title" of this_item
        set keywords of this_item to keywords of this_item & "," & " title link"
        set DictionaryLinkEntry to PDfTitle & tab & "uri:" & DevonThinkLink & "
"
        tell application "TextMate"
            activate
            insert DictionaryLinkEntry
        end tell
    end repeat
end tell

A sample from the text which can be created is shown below:


The AppleScript code for the link dictionary with author-year compinations is the following:
--2011-03-11
--http://organognosi.blogspot.com

tell application "BibDesk"
    set these_items to the selection of document 1
    if these_items is {} then error "Please select some contents."
   
    repeat with this_item in these_items
        try
            set AuthorLink to the last name of the first author of this_item
            set firstNameLink to the first name of the first author of this_item
            set AbbrNormName to abbreviated normalized name of the first author of this_item
           
            set YearLink to the publication year of this_item
            set DevonThinkLink1 to the linked URL 1 of this_item
            set DictionaryLinkEntry to AuthorLink & " " & YearLink & tab & "uri:" & DevonThinkLink1 & "
"
            set DictionaryLinkEntry2 to AuthorLink & " (" & YearLink & tab & "uri:" & DevonThinkLink1 & "
"
            set DictionaryLinkEntry3 to AuthorLink & " [" & YearLink & tab & "uri:" & DevonThinkLink1 & "
"
            set DictionaryLinkEntry4 to AuthorLink & ", " & firstNameLink & " (" & YearLink & tab & "uri:" & DevonThinkLink1 & "
"
            set DictionaryLinkEntry5 to AbbrNormName & " " & YearLink & tab & "uri:" & DevonThinkLink1 & "
"
            set DictionaryLinkEntry6 to AbbrNormName & " (" & YearLink & tab & "uri:" & DevonThinkLink1 & "
"
           
            tell application "TextMate"
                activate
                insert DictionaryLinkEntry
                insert DictionaryLinkEntry2
                insert DictionaryLinkEntry3
                insert DictionaryLinkEntry4
                insert DictionaryLinkEntry5
                insert DictionaryLinkEntry6
            end tell
        end try
    end repeat
end tell
A sample from the text which can be created is shown below:


Now you are ready to go to Adobe Acrobat Pro and use the AutoBookmark plug-in. In the following screen shots you can see the detailed steps in order to create the new dictionaries.




In this step you should select the txt file which was created by one of the two AppleScripts
Now your link dictionary is ready.