OSAKit : save script

Is it possible to re-save the “main” script of an applet as a compiled script without using SD?

The following script returns the error -1752 “Bad storage type”.

If I change OSAStorageScriptType for OSAStorageTextType, it succeeds but the saved script won’t be searchable using Spotlight.

use framework "Foundation"
use framework "OSAKit"
use scripting additions

set sourceFile to (current application's NSURL's fileURLWithPath:"/Users/ionah/Desktop/source.scpt")
set destFile to (current application's NSURL's fileURLWithPath:"/Users/ionah/Desktop/dest.scpt")

set {theScript, theError} to current application's OSAScript's alloc()'s initWithContentsOfURL:sourceFile |error|:(reference)
set {theResult, theError} to theScript's writeToURL:destFile ofType:(current application's OSAStorageScriptType) usingStorageOptions:(current application's OSANull) |error|:(reference)

It works fine here. Could there be a problem with source.scpt?

Nope! But with the dest.scpt that was corrupted!!!
:wink:

Unfortunately, the newly created script is not listed when doing a Spotlight (or HoudahSpot) search.
I’m back to the starting point.

Is there something we can do to register a file in Spotlight database?

It’s being found here…

Do you mean I can find the solution on this forum ? (my bad English…)
Could you, please, point me to the right post?
Despite all my searches I’m unable to find it.

No – I mean a Spotlight search finds the file made by your code when I run it here.

Maybe I was not clear. By saying: the saved script won’t be searchable using Spotlight.
I meant: the source (the text content) of the script file is not searchable using Spotlight.

Are you able to search contents of dest.scpt on your system?

You may try to use :[code]
set p2d to path to desktop

set scriptPath to choose file of type {“app”, “scptd”}

–set fullPath to (scriptPath as text) & “Contents:Resources:Scripts:main.scpt”

set fullPath to (scriptPath as text) & “Contents:Resources:Scripts:main.recover.rtf”

tell application "Finder"
duplicate file fullPath to p2d
end tell
[/code]

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) vendredi 30 juin 2017 21:30:28

Thank you Yvan.

I know I can duplicate the recover.rtf file for applets saved with Script Debugger.
But it’s not my goal: I’m just trying to learn AppleScriptObjC.
(Actually, I open the main.scpt in SD and re-save it with the applet’s name.)

The problem is the same with a RTF file saved with the writeToUrl method.
I’m just trying to understand why.

Un chaleureux bonsoir depuis Avignon ! :wink:

First, you need to understand how the Spotlight data is collected.

When a file is copied or saved to a folder, a background mds process tries to collect its Spotlight data. It sees what kind of file it is, and then looks to see if it has a suitable importer – these are .mdimporter files, which contain code for extracting all the information from the file. There are built-in importers for some common file types, but not for AppleScript files.

Script Debugger ships with a Spotlight importer, which is loaded by the system. In the case of files saved by Script Debugger, this importer knows where to look to find the source – in bundles it’s in main.recover.rtf, and in .scpt files it’s saved as a resource using the Carbon resource manager.

But if a file isn’t saved by Script Debugger, it won’t have this .rtf file or resource. Therefore the only way for the importer to extract the source would be to decompile the script. That, in turn, could lead to applications targeted in the scripts being launched. Obviously having applications mysteriously launch for no apparent reason – or worse, having dialogs appear asking where to find them – is less than ideal, so the only practical option for the importer is to ignore the content of such files.

The problem, really, is that compiled formats and Spotlight filters don’t work well together. And the Script Debugger importer can only deal with files that also include a separate version of the source, which means files saved by Script Debugger (and ASObjC Explorer). Oh, and, strangely enough, uncompiled autosaved .scpt files saved by Script Debugger (because they save a copy of the source in .rtf).

There’s a system-provided Spotlight importer for .rtf files, so I’m not sure why that’s happening.

Thank you, Shane.
Everything is clear now.

After all, my old script is not so bad. Even if it launches apps on decompilation, sometimes.

:slight_smile: