This script creates links to records on the desktop.
It can be set up to create only bookmarks, only aliases or to have the choice with choose from list.
I have used @korm’s excellent script for years, later modified his idea as I wanted options for creating aliases and using selected text for renaming. After reading this thread I rewrote it as it was quite buggy when used with selected text (that AppleScript doesn’t cut a string correctly if it contains umlauts never came to my mind …).
There are some options:
- useMainWindow: controls if a record opens in a think or a document window.
- usePageNumber: controls if the current page of a PDF is used in the Reference URL. If false other page related options are ignored.
- showPageNumber: controls if the page number is added to the filename
- showPageNumberWithoutSel: only add page number if text is selected
-
deleteStorageFile: during export DEVONthink creates a
DEVONtech_storage
file in the export folder. This option deletes it. Doesn’t make much sense if you use the script often. Better hide this file by running the following script once
Export a record via menu or script first, this file is not created if you drag a record to a folder.
-- Hide "DEVONtech_storage" file. Don't wonder, the file is not immediately hidden.
property destinationPath : (POSIX path of (path to desktop)) as string
do shell script "chflags hidden" & space & quoted form of (destinationPath & "DEVONtech_storage") as string
The script can’t create bookmarks for global smart groups and smart rules.
All other records can be linked:
- record
- group
- tag
- database smart group
- database inbox
- global inbox
- trash
It can be run in a document window or a think window, if nothing is selected it uses the selection of the navigation sidebar.
Bookmarks don’t look pretty on the desktop but there’s a nice tool to change this:
- free
setFileIcon
tool which can be downloaded here Set a Custom Icon for a File
If the path to this tool is set in option setFileIconPath
(e.g. (POSIX path of (path to home folder)) & ".SetFileIcon" as string)
)
then all bookmarks use DEVONthink icons
Here’s the script
-- Create bookmark or alias on desktop
property useChooseFromList : true
property defaultChoice : "Bookmark" -- Bookmark / Alias
property useMainWindow : false
property useSelectedText : true -- if false no page number is used
property usePageNumber : true
property showPageNumber : true
property showPageNumberWithoutSel : false
property thePageFormat : ", S. "
property deleteStorageFile : false
property destinationPath : (POSIX path of (path to desktop)) as string
property setFileIconPath : "" -- don't change unless you use the free setFileIcon tool (http://www.hamsoftengineering.com/codeSharing/SetFileIcon/SetFileIcon.html)
tell application id "DNtp"
try
set windowClass to class of window 1
if {viewer window, search window} contains windowClass then
set currentRecord_s to selection of window 1
else if windowClass = document window then
set currentRecord_s to content record of window 1 as list
end if
set recordCount to (count currentRecord_s)
if recordCount = 0 then set currentRecord_s to root of window 1 as list
if recordCount > 1 then
set useSelectedText to false
set usePageNumber to false
end if
if useChooseFromList = false then
set linkType to defaultChoice
else
activate
set theChoice to (choose from list {"Bookmark", "Alias"} with title "Desktop" with prompt "" default items {defaultChoice})
if theChoice = false then return
set linkType to item 1 of theChoice
end if
repeat with theRecord in currentRecord_s
set theRecordName to name of theRecord
set theRecordPath to path of theRecord
if theRecordPath ≠ "" then
set theFilename to filename of theRecord
set revFileName to (reverse of characters in theFilename as string)
set theSuffix to reverse of characters 1 thru ((offset of "." in revFileName) - 1) in revFileName as string
if theRecordName ends with theSuffix then set theRecordName to characters 1 thru -((count theSuffix) + 2) in theRecordName as string
else
set theSuffix to ""
end if
set theRefURL to reference URL of theRecord
if useMainWindow = true then set theRefURL to theRefURL & "?reveal=1" as string
set theType to type of theRecord as string
set theName to theRecordName
set foundText to false
if useSelectedText = true then
set selectedText to selected text of window 1
try
set selectedText to selectedText as string
set theName to selectedText
set foundText to true
end try
end if
set thePage to ""
if usePageNumber = true then
if theType = "PDF document" then
set currentPage to current page of window 1
if currentPage ≠ -1 then
set theRefURL to theRefURL & "?page=" & currentPage as string
if showPageNumber = true then
if foundText = true then
set thePage to (thePageFormat & (currentPage + 1)) as string
else
if showPageNumberWithoutSel = true then
set thePage to (thePageFormat & (currentPage + 1)) as string
end if
end if
end if
end if
end if
end if
if linkType = "Bookmark" then
set theBookmark to create record with {name:theName, URL:theRefURL, type:bookmark} in incoming group
set theBookmarkPath to export record theBookmark to destinationPath
delete record theBookmark
if setFileIconPath ≠ "" then my setIcon(theType, kind of theRecord, theBookmarkPath, setFileIconPath)
tell application "Finder"
try
set theFile to file (POSIX file theBookmarkPath as alias)
set extension hidden of theFile to true
if theName ≠ theRecordName or thePage ≠ "" then
set theSuffix to "inetloc"
set trimmedName to my trimName(theName, thePage, theSuffix)
set name of theFile to (trimmedName & thePage & "." & theSuffix) as string
end if
if deleteStorageFile = true then delete file (POSIX file (destinationPath & "DEVONtech_storage") as alias)
on error
if deleteStorageFile = true then delete file (POSIX file (destinationPath & "DEVONtech_storage") as alias)
end try
end tell
else if linkType = "Alias" then
tell application "Finder"
try
exists (POSIX file ((destinationPath & theName) as string) as alias) as alias
activate
display alert "Hoppla!" buttons {"Beenden", "Ok"} default button 2 message "Name bereits vergeben." as critical
return
end try
set theAlias to make alias file to (POSIX file theRecordPath) at folder (POSIX file destinationPath as alias)
set extension hidden of theAlias to true
if theName ≠ theRecordName or thePage ≠ "" then
set trimmedName to my trimName(theName, thePage, theSuffix)
set name of theAlias to (trimmedName & thePage) as string
end if
end tell
end if
end repeat
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell
on trimName(theName, thePage, theSuffix)
if theName begins with "." then set theName to characters 2 thru -1 in theName as string
if theName begins with space then set theName to characters 2 thru -1 in theName as string
set pageSuffix to (thePage & "." & theSuffix) as string
set totalLength to (do shell script "export LANG=\"en_US.UTF-8\" && echo " & quoted form of ((theName & pageSuffix) as string) & " | wc -c") as number
if totalLength > 255 then
set pageSuffixLength to (do shell script "export LANG=\"en_US.UTF-8\" && echo " & quoted form of pageSuffix & " | wc -c") as number
set maxLength to 255 - pageSuffixLength
set trimmedName to (do shell script "export LANG=\"en_US.UTF-8\" && echo " & quoted form of theName & " | tr -d " & quoted form of "\\n" & " | cut -c-1-" & maxLength & " - | tr -d " & quoted form of "\\n")
else
set trimmedName to theName
end if
end trimName
on setIcon(theType, theKind, theBookmarkPath, setFileIconPath)
if theType = "group" then
if theKind = "Tag" then
set iconPath to "/Applications/DEVONthink 3.app/Contents/Resources/Tag-Group.icns"
else
set iconPath to "/Applications/DEVONthink 3.app/Contents/Resources/Group.icns"
end if
else if theType = "smart group" then
set iconPath to "/Applications/DEVONthink 3.app/Contents/Resources/Smart-Group.icns"
else
set iconPath to "/Applications/DEVONthink 3.app/Contents/Resources/dtRecord.icns"
end if
do shell script quoted form of setFileIconPath & " -image " & quoted form of iconPath & " -file " & quoted form of theBookmarkPath
end setIcon