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