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

No comments: