** Edit Nov 22, 2012: the script is completely revised, per Rob’s suggestion and question below
I’ve extended the functionality of the Markdown-style link script to give you three options for the link that the script places on the keyboard:
- a link to the document in the DEVONthink database (i.e., x-devonthink-item://)
- a link to the document in the file system (i.e., [file://localhost](file://localhost) )
- same as (2) but with the embedded image prefix “!” on the link – when the Markdown is rendered as HTML the image will appear on the page
- If the FTnote property in the script is “true” then the script will produce a link suitable for pasting into FoldingText as a link appended to a tag
Number (3) doesn’t have the logic in the script to decide that what you selected is actually an image. If you don’t select an image file, your rendered HTML will show the question mark icon instead of an image (depending on your browser). I’ll update the logic in the script someday to trap this condition.
(*
WARNING: this can create links directly to files inside a database package which
means you could damage something. Such links will break instantly if you
move the database anywhere. The script will ask if you understand the risk.
If you don't want to be warned, set the warnMe property to false
Script to put a MultiMarkDown-styled link to a DEVONthink database item on
the clipboard.
A optional property (set FTnote to true) will embed the styled link inside a
FoldingText tag.
**If FTnote is true:
(*
You can have the script produce the link embedded in a FT tag or a FT property
(See the PROPERTIES section in the script, below)
The default property is "note : ". This can be changed by
setting FTtag to some other string. Because FoldingText currently (20121122) does not
handle links to files very well, if FTnote is true then the only result of the script
is a link using the DEVONthink reference URL (x-devonthink-item://)
*)
**If FTnote is false:
(*
The selected document is an image (png, tiff, jpg, jpeg, or PDF):
You are prompted with three options:
(1) link using the DEVONthink reference URL,
(2) link directly to the document in the file system (WARNING: if the document
is not indexed, then this will create a link directly inside a database
package -- use at your own risk),
(3) produce the same link as (2), but using the MultiMarkdown embedded image format
*)
**The document is not an image: you get options (1) and (2) only.
*)
--PROPERTIES
property FTnote : false -- set this to true or false
-- property FTtag : "@note" -- use this style if you want FoldingText link embedded in a FT tag
property FTtag : "note : " -- use this style if you want a FoldingText link embedded in a FT property
property linkOptionsEmbedded : {"Link to Document in DEVONthink", "Link to Document in File System", "Embedded Image or PDF"}
property linkOptionsNoImage : {"Link to Document in DEVONthink", "Link to Document in File System"}
property warnMe : true
tell application id "com.devon-technologies.thinkpro2"
set theLink to ""
set the clipboard to ""
try
if the selection is {} then error
set theItem to (the first item of (the selection as list))
set theType to the type of theItem as string
if not FTnote then
if ({"PDF document", "picture"} contains theType) then
set linkOptions to linkOptionsEmbedded
else
set linkOptions to linkOptionsNoImage
end if
set linkType to (choose from list linkOptions with prompt "Choose the type of Markdown Link") as string
else
set linkType to "DEVONthink"
end if
if linkType contains "File System" then
if warnMe then
set warned to display alert "Do you accept the danger of using this link?" buttons {"Yes", "No"} default button 2
set answer to the button returned of warned
if answer is "No" then error
end if
set myPath to the path of theItem as string
set myURLPath to my encode(myPath)
-- set myURLPath to my encode_text(myPath, false, false)
set theLink to "[" & name of theItem & "](file://localhost" & myURLPath & ")"
end if
if linkType contains "Image" then
set myPath to the path of theItem as string
set myURLPath to my encode(myPath)
-- set myURLPath to my encode_text(myPath, false, false)
set theLink to ""
end if
if linkType contains "DEVONthink" then
set theLink to "[" & name of theItem & "](" & reference URL of theItem & ")"
end if
if FTnote then set theLink to FTtag & " (" & theLink & ")"
set the clipboard to theLink
end try
end tell
-- Sub-routine provided by Rob Trew
on encode(strPath)
do shell script "python -c 'import sys, urllib as ul; print ul.quote(sys.argv[1])' " & ¬
quoted form of strPath
end encode
Why bother with all this? Over here, I do all my first drafts and note taking in plain text with Markdown tags (actually, MultiMarkdown). For projects, I keep notes collected in VoodooPad wikis, which I publish as local HTML sites. It’s helpful to have links to reference documents in the database (the first style of link) or to have links that open a reference file directly into its native editor (the second style of link), or merely display images in my notes (the third style of link).
This might be helpful for other plain text + Markdown users. Of course, these links will work in any Markdown-aware editor. I’ve found that Folding Text’s handling of file:// and image URLs is buggy, so YMMV with that editor.
WARNING: the script can be used to make a link directly into the internals of a database package. That type of link is optional if you want to open a document for editing from inside a Markdown document - and should be used only if you understand and accept the risks.