Need AppleScript syntax to encode a "data: URL"

On this web page:
http://blog.clawpaws.net/post/2007/07/16/Storing-iPhone-apps-locally-with-data-URLs
… is a one-line Perl script to construct an HTML app inside a “data: URL.”

Does anyone know how to similarly do so with AppleScript?

“Some people think that you need net access to run web-based applications on your iPhone. … By using a data: URL, the entire page content is all in the URL. If you save a bookmark for this URL, you can access this little JavaScript-based app even in airplane mode.”

You could use perl with do shell script

-- This handler expects text
on dataURL(htmlText)
	do shell script "echo " & quoted form of htmlText & " | /usr/bin/perl -0777 -e 'use MIME::Base64; $text = <>; $text = encode_base64($text); $text =~ s/\\s+//g; print \"data:text/html;charset=utf-8;base64,$text\\n\";'"
end dataURL
-- This handler expects an alias, file, or posix path
on dataURL(htmlFile)
	do shell script "/usr/bin/perl -0777 -e 'use MIME::Base64; $text = <>; $text = encode_base64($text); $text =~ s/\\s+//g; print \"data:text/html;charset=utf-8;base64,$text\\n\";' < " & quoted form of POSIX path of htmlFile
end dataURL

Is this Perl script to encode a “data: URL” amenable to complete translation entirely into corresponding AppleScript syntax?

Using TextCommands:

on dataURL(htmlText)
	tell application "TextCommands"
		return "data:text/html;charset=utf-8;base64," & (strip (pack htmlText using base64 encoding) from throughout)
	end tell
end dataURL

A pure vanilla solution isn’t practical as the default AppleScript installation doesn’t include a command for encoding text as base64.

HTH

(Edited to fix bug in code.)

Ah-ha! That’s what I needed to know. I get it now. Thanks!

On this thread over two years ago:
http://bbs.applescript.net/viewtopic.php?id=10489
… the first post contains an entirely-AppleScript syntax base-64 encoder.

The thread petered-out without ever posting a final confirmed reliable algorithm.
I’ve emailed the poster, kel, but haven’t yet heard back as to the outcome.
Meanwhile, maybe someone could look it over and comment on it. I did a search here and that was the only base-64 AppleScript I could find.

What I’m seeking is to converge on a stand-alone dedicated AppleScript that can construct a data: URL, without the complications of having to keep up with a scripting addition osax, a separate Faceless Background Application, or a Perl Terminal command. I recognize that it may run slower, but I’m trying to come up with something self-contained, completely in AppleScript.

A single very quick test showed it producing correct output, but that’s hardly proof; you’d need to do more thorough testing yourself. As expected, however, being a ‘pure’ vanilla solution, it runs hopelessly slow. AppleScript is totally useless for anything involving raw data crunching; hence my previous comment about a pure vanilla version being impractical. Calling out to an external tool is definitely the way to go.

What’s the problem with using Perl (or openssl if you prefer) via ‘do shell script’? “Use the right tool for the job” and all that. Perl is available on every version of Mac OS X so your script will be quite portable. The only advice I’d give is that you have the shell script read from file rather than trying to pass the data via ‘do shell script’ due to the size limits the latter approach imposes.

e.g. Save the following as an applet, then drag an HTML file onto it and it’ll place the formatted data on your clipboard:

on open {htmlFile}
	do shell script "/usr/bin/perl -0777 -e 'use MIME::Base64; $text = <>; $text = encode_base64($text); $text =~ s/\\s+//g; print \"data:text/html;charset=utf-8;base64,$text\\n\";' < " & quoted form of POSIX path of htmlFile & " | pbcopy"
end open

You could even skip AppleScript entirely - just write is as a plain shell script and use Platypus to make it into a drag-n-drop applet.

HTH