2011/03/07

Automated creation of a LaTeX compatible citation only from Skim! (with hyperlink included)

In the post "DevonThink links in Skim notes and AppleScript" I presented how you can create to a PDF file a number of Skim notes with various versions of references to itself. An example of these notes is shown below:


Then you can insert manually the BibTeX cite command in the fist and the second note.


Now you are ready to capitalize on these four little notes!

The first way was presented in the post "Automated summary creation linked with the source pfd file" where the respective AppleScript use the fourth note:
 set pdfDevonThinkLink to (get text for note 4 of page 1 of document 1) as string
In the AppleScript of this post the second note will be used.

As you understand the numbering of these notes plays a fundamental role in the effective use of these AppleScripts. For this reason 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 (2011-04-05, an alternative solution is to use the AppleScript 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").

One of the most important reasons for which I have chosen to write my essays using LaTeX is that it makes possible the creation of rich hypertexts. By rich I mean that the final PDF document can have all the sorts of different kinds of links as you can see in the lower part of my mind map "The many faces of links". In order to create these links you should use the Hyperref package.

If you are accustomed to the LaTeX markup code you will recognize that the second note includes a proper citation as it is required by the Hyperref package:

(\cite{Deconstructing-the-Laws-of-Logic-Clark-2008a}: \href{x-devonthink-item://CDEC17B7-1EEE-42E1-B8C9-86A24C172BF3?page=}{})
The only things that are missing from the above citation are two numbers: the number for the DevonThink link and the the number of the referred page.

Now lets say that you would like to refer to the 27th page of the paper which is the third page of the PDF file. From now on you do not have to manually paste the necessary code and the appropriate numbers in your txt file but an AppleScript can do it for you!

There is only one but... you want the number that is shown in your text to be 27 but the number which is attached to the DevonThink link to be 2 (3-1: if you do not know why, read my post It's all about hyperlinks!). My AppleScript has the ability to recognize in which page you currently are but it cannot recognize the written number in the page! So you will have to enter it manually.

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

display dialog "Give the number which is written in you current page:" default answer "" buttons {"Cancel", "OK"} default button 2
set writtenNumber to text returned of the result as text

tell application "Skim"
    activate
   
    tell document 1
        set currentPageNumber to get index of current page
        set pageDevonThinkNumber to currentPageNumber - 1
        set pageDevonThinkNumberText to pageDevonThinkNumber as text
        set DevonThinkLink to get text of note 2 of page 1
       
        tell application "TextMate"
            activate
            insert DevonThinkLink
            tell application "System Events"
                key code 123
                key code 123
                key code 123
                key code 123
            end tell
            delay 0.5
            insert pageDevonThinkNumberText
            tell application "System Events"
                key code 124
                key code 124
            end tell
            delay 0.5
            insert writtenNumber
        end tell
    end tell
end tell

If you do not want to write even this number you can create a fifth note which will have only the written number of the first page and then the AppleScript can compute the rest.

--2011-03-07
--http://organognosi.blogspot.com

tell application "Skim"
    activate
   
    tell document 1
        set currentPageNumber to get index of current page
        set pageDevonThinkNumber to currentPageNumber - 1
        set pageDevonThinkNumberText to pageDevonThinkNumber as text
        set DevonThinkLink to get text of note 2 of page 1
        set writtenNumberOfFirstPage to (get text for note 5 of page 1) as string
        set currentPageWrittenNumber to currentPageNumber + writtenNumberOfFirstPage - 1 as string
       
       
        tell application "TextMate"
            activate
            insert DevonThinkLink
            tell application "System Events"
                key code 123
                key code 123
                key code 123
                key code 123
            end tell
            delay 0.5
            insert pageDevonThinkNumberText
            tell application "System Events"
                key code 124
                key code 124
            end tell
            delay 0.5
            insert currentPageWrittenNumber
        end tell
    end tell
end tell

Finally the easiest case is when the page numbers of the PDF file and the printed book coincide. In this case you can still use the above AppleScript if you write in the fifth note the number 1. If you do not want to use a fifth note the AppleScript is the following:

--2011-03-07
--http://organognosi.blogspot.com

tell application "Skim"
    activate
   
    tell document 1
        set currentPageNumber to get index of current page
        set pageDevonThinkNumber to currentPageNumber - 1
        set pageDevonThinkNumberText to pageDevonThinkNumber as string
        set currentPageNumberText to currentPageNumber as string
        set DevonThinkLink to get text of note 2 of page 1
       
       
        tell application "TextMate"
            activate
            insert DevonThinkLink
            tell application "System Events"
                key code 123
                key code 123
                key code 123
                key code 123
            end tell
            delay 0.5
            insert pageDevonThinkNumberText
            tell application "System Events"
                key code 124
                key code 124
            end tell
            delay 0.5
            insert currentPageNumberText
        end tell
    end tell
end tell
Before you execute any of the above AppleScripts you should have a TextMate document opened. Moreover the cursor should be at the point in which you want the citation to be inserted.

2011-04-05
There is an improved version of the above AppleScript in my post "Automated creation of references with hyperlinks from Skim"


No comments: