Suppose I have a script file. How can I get the text of the script from the file without first opening the script in script editor? For example, I can get the script’s text by first opening the file and then getting it’s contents like so…
set scriptFile to (path to desktop as text) & "example.scpt"
tell application "Script Editor"
set theScript to open scriptFile
set scriptText to text of theScript
close theScript
end tell
scriptText
But I don’t want to have to first open the script to get its text. Is there any other way? I tried to load the script but I can’t get the text from the loaded script… this doesn’t work…
set scriptFile to (path to desktop as text) & "example.scpt"
set theScript to load script file scriptFile
set scriptText to text of theScript
Yes Adam, I am talking about a compiled script. I thought what you said but hoped somebody might have a better idea. I know you can load a script as I showed in my second example above, then you can also use “store script” to save the loaded script to a file. I was hoping to use store script to save the loaded script in text format, but the store script command doesn’t have any way to change the saved format to text, so that didn’t help either.
Possibly not much help, but if you have a PPC machine and can use Jon’s commands, that has a script-to-string coercion. Load the script, coerce it to string, write the result to file.
I’ve also found that if a loaded script is stored with an “.applescript” extension, the next time it’s opened in Script Editor, it’s plain text. But it seems to be stored in the resource fork rather than as an actual text file.
Edit: This latter tip doesn’t seem to work in Tiger, so it’s even more useless than I’d thought. Sorry to have wasted your time with it.
Thanks so much StefanK!!! :lol:
I knew somebody could find a way. osadecompile does just what I need. I was hoping it worked in 10.4 Tiger too but it didn’t. I copied the executable over to Tiger, but one of the libraries it uses threw up some errors. That’s OK though because I’m using Leopard, but it would have been nice in Tiger too. Anyway, thanks again!
osadecompile is neat and fast; nice solution, Stefan. One word of warning: if you’re processing a lot of scripts, the applications “told” in each script will open, but not close again, so you need a little housekeeping handler to quit them before you’ve got every app you own open. Also, osadecompile won’t accept a list, so it’s one file at a time.
This isn’t entirely true. Only certain applications will open. For example in my tests iPhoto, Safari, Address Book, and Script Editor will open. But other applications such as iTunes don’t open. If you have any guesses as to why those apps open let me know.
This caused one major problem that took me too long to find… so be careful of this.
I was running my decompile script from within a Script Editor window. Everything was working fine until I tried to decompile a script file which had a tell application “script editor” call in it. The osadecompile shell process never returned any value so my script was stuck. I was able to get out of the stuck script by quitting the osadecompile process using Activity Monitor though. I assume this happened because Script Editor was busy running my decompile script, and the osadecompile process was trying to get an answer back from Script Editor (maybe because osadecompile wanted to launch Script Editor like we talked about above) but it couldn’t.
Note that this same decompile script ran perfectly if I ran it from the script menu, but when run from the script editor window it would hang whenever a script file had a tell application “script editor” call in it.
I made the same experience with the Library window in the Script Editor. When you choose to open a certain AppleScript dictionary, sometimes Script Editor will instantly display the dictionary browser, sometimes it needs to open the corresponding application first.
I guess it has to do with how the dictionary is saved in the application itself.
Regulus & Martin – There were changes made in Leopard so that newer apps reveal their Dictionaries without starting the application. So far, it seems that only recently updated Apple Apps comply.