4

I'm trying to make a Swift app, 'Earwig.app', MacOS, scriptable. I've started by shamelessly stealing an example from Making Cocoa Application Scriptable Swift, adding two files, a .sdef and a .swift as listed below. I've set Scriptable and scripting definition filename in info.plist

My AppleScript is:

tell application "EarwigServer" to save in "path/to/file"

Running the script gives me an error, 'EarwigServer got an error: Can’t continue save.'

At this point I'm stuck. Any suggestions appreciated

sdef file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">

<dictionary title="ScriptableSwift Terminology">

    <suite name="ScriptableSwift Scripting Suite" code="SSss" description="Standard suite for application communication.">

        <command name="save" code="SSssSave" description="Save something.">
            <cocoa class="ScriptableSwift.SaveScriptCommand"/>
            <parameter name="in" code="Fpat" type="text" description="The file path in which to save the document.">
                <cocoa key="FilePath"/>
            </parameter>
            <result type="text" description="Echoes back the filepath supplied."/>
        </command>

    </suite>
</dictionary>

swift file:

import Foundation
import Cocoa

class SaveScriptCommand: NSScriptCommand {
    override func performDefaultImplementation() -> Any? {
        print( "in" )
        let filePath = self.evaluatedArguments!["FilePath"] as! String

        print( "here" )
        return filePath
    }
}

info.plist:

info.plist

Kim Aldis
  • 583
  • 1
  • 4
  • 15
  • There are a lot of reasons why it couldn't work. The hassle is that any tiny mistake breaks the entire functionality. For example did you add the required keys and values in Info.plist? – vadian Nov 21 '19 at 10:33
  • I'm pretty sure I've got info.plist right but I've edited the post with an added screengrab. I've pretty much just added the two files from an example project that works with a view to adjusting them as needed. I wondered if I'd got the class name, ScriptableSwift.SaveScriptCommand, wrong. I've tried a number of permutations without luck. – Kim Aldis Nov 21 '19 at 11:21

1 Answers1

1

It started working when I changed ScriptableSwift.SaveScriptCommand to EarwigServer.SaveScriptCommand. Which seems obvious, I know but I'd done this before with no effect. It seems that changes don't take effect until I cleaned the build folder.

This xml works, the swift file remains unchanged:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">

<dictionary title="EarwigServer Terminology">

    <suite name="EarwigServer Scripting Suite" code="ESss" description="Standard suite for application communication.">

        <command name="run" code="ESssErun" description="Save something.">
            <cocoa class="EarwigServer.SaveScriptCommand"/>

            <parameter name="in" code="Fpat" type="text" description="The file path in which to save the document.">
                <cocoa key="FilePath"/>
            </parameter>
            <result type="text" description="Echoes back the filepath supplied."/>
        </command>

    </suite>
</dictionary>

Thanks for the input

Kim Aldis
  • 583
  • 1
  • 4
  • 15
  • Also, XML in the sdef file is very touchy about syntax while being very unhelpful about feeding back errors when scripts are run in the script editor. I'd introduced a tab before the XML tag on the first line and it took a while to track it down. Running a script in osascript reported the error in the file where running one in the script editor just returned an obscure error. – Kim Aldis Nov 22 '19 at 06:59
  • You helped me a lot – aydar.media Feb 17 '22 at 21:41