OSAScript read source

Is it possible to read the source of a compiled script with OSAScript (or any other way) avoiding the launch of applications?

Would OSADECOMPILE(1) do that thing for you?

The osadecompile command will show you the source for a compiled script. This command will launch applications as needed to get their scripting meta-data in order to display their source.

1 Like

I kinda suspected that.
So there is no alternative?

If all the scripts are created with Script Debugger, you can read the backup source from the script file/bundle instead of using osadecompile. Other than that, no, there is no alternative.

Script Debugger avoids launching target applications through trickery with the APIs and by caching application terminology files. This is not available to you from the command line.

All my scripts are saved with SD.
But if I know where to find a ‘backup’ for bundles (the main.rtf file) I don’t know how to get it for compiled scripts. More than this, I thought it was not possible.
Is it a resource or maybe a metadata?

Hey @ionah,

Yes – SD’s compiled script source RTF is packed into a resource in the resource-fork of the .scpt file.

I’ve been using the Satimage.osax to extract source text from compiled scripts for decades.

(I think Mark’s conversion still works up to Ventura, but I’ve not tested this.)

Making the Satimage.osax work on macOS Mojave and beyond

set compiledScriptAlias to alias ((path to home folder as text) & "test_directory:compiled_applescript_test.scpt")
# set resourceList to list resources from compiledScriptAlias

# Script Source Text
set scriptText to load resource 2001 type "TEXT" from compiledScriptAlias as «class utf8»

Now with the Satimage.osax sunsetted I need an alternative means.

I’d think it could be done with AppleScriptObjC, but I haven’t found anything useful yet.

@ShaneStanley , @alldritt – ?

You can use xattr and cat in the shell:

# Lists the Extended Attributes.
xattr -l ~/test_directory/compiled_applescript_test.scpt

# Prints the given resource in HEX (in this case).
xattr -p com.apple.ResourceFork ~/test_directory/compiled_applescript_test.scpt

# Decodes the HEX.
xattr -p com.apple.ResourceFork ~/test_directory/compiled_applescript_test.scpt | xxd -r -p

# Prints the resource-fork.
cat ~/test_directory/compiled_applescript_test.scpt/..namedfork/rsrc

But these don’t produce nice, human-readable output.

There ought to be a simple way to parse the output, but the only thing I can think of is brute-force find/replace to extract the RTF and then run that through the textutil command line app to convert the RTF to text.

-Chris

1 Like

Alas no, it’s all done by ancient Resource Manager code. (Which I suspect is what’s causing the Spotlight issues.)

1 Like

Thanks Shane.

Then the easiest method is going to be to use Mark’s adaptation of the Satimage.osaxen, while macOS continues to support it.

FWIW, we are considering moving it to a normal xattr – we already did something similar with .applescript files and their resources. The issue is a bit complicated (a) because resources have to be used for some stuff anyway, and (b) backwards compatibility.

1 Like

Chris, I got rid of osaxen years ago and I don’t want to go back: my script will be obsolete once the problem is fixed.

Shane, I get the dilemma.

So…

Mark repackaged the Satimage Osaxen into an app.

It’s very simple to install, get running, and then uninstall when you’re done.

Look at the documentation:

Use Satimage scripting additions on Catalina and Mojave

Chris,

SatimageOSAX lets me get the RTF Quicklook resource but there is 2 issues:

  • the result is sent as raw data and I don’t know how to convert it to rich text
  • the command triggers an error when adding any use framework call

The former is not really a problem. But the latter… is why I won’t use an osax.

Whups… I’m still using Script Debugger 5 for most things, because I’m stuck on Mojave for now and have thousands of scripts to convert – so I didn’t think about Script Debugger 8.x using a different resource schema.

Here’s a remedy for that:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2023/01/01 08:59
# dMod: 2023/01/01 08:59 
# Appl: AppleScriptObjC, lFinder
# Task: Extract Script Debugger's Backup Script Text from the Selected .scpt File.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Satimage.osax, @Finder, @Extract, @RTF
--------------------------------------------------------
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
--------------------------------------------------------
property NSUTF8StringEncoding : a reference to 4
--------------------------------------------------------

# Get Selected File in the Finder
tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set compiledScriptAlias to item 1 of finderSelectionList

# Satimage.osax – get script resource from script file as RTF string.
set rtfSource to load resource 2000 type "RTF " from compiledScriptAlias as «class utf8»

set ns_String to current application's NSString's stringWithString:rtfSource
set theData to ns_String's dataUsingEncoding:NSUTF8StringEncoding
set attributed_String to current application's NSAttributedString's alloc()'s initWithRTF:theData documentAttributes:(missing value)
set scriptText to attributed_String's |string|() as «class utf8»

return scriptText

--------------------------------------------------------

You should be able to work around that by wrapping your Satimage.osax commands in a ‘tell application’ block rather than employing a ‘use’ statement. Unfortunately I can’t test to verify…

tell application "SatimageOSAX"
   set rtfSource to load resource 2000 type "RTF " from compiledScriptAlias as «class utf8»
end

Hopefully this will get you going.

Hi Chris,

Thank you for pointing me in the right direction.
The clash I was experiencing was not with Foundation framework but with Scripting Additions: I was using tell application "SatimageOSAX" to set theData to load resource 2000 type "RTF " from (alias theScript) as «class utf8»
It was the term alias that was causing the error.
Then, where you get the data as «class utf8» I was looking to get it as «class data».

use framework "Foundation"
use scripting additions

set theScript to ("path:to:script.scpt" as «class furl»)
tell application "SatimageOSAX" to set theData to load resource 2000 type "RTF " from theScript as «class utf8»
set theData to (current application's NSString's stringWithString:theData)'s dataUsingEncoding:4 -- NSUTF8StringEncoding
set theRTF to current application's NSAttributedString's alloc()'s initWithRTF:theData documentAttributes:(missing value)
1 Like