Creating a webloc file with Apple Script

Creating weblocs
================

This is just a small project, for creating weblocs to links in Omnifocus, both to Tasks, Projects and Perspectives. You get the idea that I am lazy, and therefore want to go directly to the task in speak when I am standing in a folder containing something with something to be done in Omnifocus. If you are really imaginative, you’ll guess that I am so lazy, that I set it all up when I create the Omni focus project, creating a url to the folder into to project, and puts the webloc for the project into the folder of Finder. :slight_smile:

Truth is, is that this isn’t really decided yet, and that I for now will have a folder on my Desktop, named OmnifocusProjects containing said weblocks, that are aliased into the script menu for easy access. I also have it like that from within Quicksilver.

This is about creating the webloc, which according to this post at Stack Overflow objective c - Crafting .webloc file - Stack Overflow isn’t well known, so I thought I’d share the method, so it may be put to use if it is needed.

You can of course use the technique to fill out propertylist files as schemes, before writing them out, for any other kind of propertylist files as well, using the "delimiter method".


local plistTxt
set plistTxt to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
    <key>URL</key>
    <string>ENCODED_URL</string>
</dict>
</plist>"
local theUrl, tids, plistTxtParts
set theUrl to first paragraph of (the clipboard) as text
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "ENCODED_URL"}
set plistTxtParts to text items of plistTxt
set AppleScript's text item delimiters to theUrl
set plistTxtParts to text items of plistTxtParts as text
set AppleScript's text item delimiters to tids

local cancelled, weblocFilename
set cancelled to false
try
	set weblocFilename to (choose file name default name ".webloc" default location (path to desktop folder) with prompt "Choose a filename for the webloc") as text
on error e number n
	set cancelled to true
end try
if cancelled = true then return 0
local writeRes

set writeRes to writeToFileAsUtf8(weblocFilename, plistTxtParts)
if writeRes < 240 then display alert "Error during saving webloc"
# less than the minimal length of the plist with contents
on writeToFileAsUtf8(fname, stuff)
	local fRef, n, fsz
	set fRef to open for access fname with write permission
	try
		write stuff to fRef as «class utf8» starting at 0
		set fsz to get eof fRef
		close access fRef
		return fsz
	on error e number n
		try
			close access fRef
		end try
		set fsz to n
	end try
	return n
end writeToFileAsUtf8

Stefan’s post here made me remember that Finder can create .webloc files. It works with DEVONthink, so probably with OmniFocus as well:

tell application "Finder" to make new internet location file to "x-devonthink-item://2BB8344B-07F1-41D2-8B61-18ACB44A86FB" at desktop with properties {name:"myDTPloc"}

Didn’t speed test it.