Read script file

I thought it a nice idea to reverse engineer the “Open this Scriplet in your Editor:” from this forum for my own blog. Then I had the idea to do this in AppleScript. I found a nice method to do the urlencoding. But how do I read a script file?

The below script reads the script file as text:

set theFile to choose file with prompt "Please select a script file"
set theFile to theFile as string
return read file theFile

There isn’t any error handling, yet, and I need to restrict the file dialog to scpt files.

You’ll want load script for a script(scpt or scptd extension). It’s in Standard Additions, in the Scripting Commands suite.
If it’s text (applescript extension) you use read file. What you do next depends on what your goal is.

Thanks. But this doesn’t work. I need the text of the script and don’t want to execute it.

set theFile to choose file with prompt "Please select a script file"
set theScript to load script theFile
display dialog (theScript as string)

gives me an error “error “Can’t make «script» into type string.” number -1700 from «script» to string”

Ah - the goal is to load a script as text:

set pathToSource to (choose file with prompt "Please select a script file") as text
set textCode to do shell script "/usr/bin/osadecompile " & quoted form of POSIX path of pathToSource