AppleScript and pandoc help

I’m trying to create an action on the Mac that will take the current draft, run it through Pandoc, and open a Word file.

The AppleScript line here works fine in the Script Editor, but produces this error in Drafts:

“error: The operation couldn’t be completed. /Users/derekvan/Library/Application Scripts/com.agiletortoise.Drafts-OSX/Temp/applescript-620583394.959022.scpt: execution error: No error. (-1708)”

// pandoc to docx

let fm = FileManager.createCloud();
let text = draft.content;
fm.writeString("/Temp/panTemp.md",text);


let method = "execute";

let script = `do shell script "/usr/local/bin/pandoc -f markdown+smart --reference-doc=/Users/derekvan/Dropbox/CSS/out.docx '/Users/derekvan/Library/Mobile Documents/iCloud~com~agiletortoise~Drafts5/Documents/Temp/panTemp.md' -o ~/Desktop/test.docx"`;

let runner = AppleScript.create(script);
if (runner.execute(method)){
    alert(runner.lastResult)
}
else{
    alert("error: " + runner.lastError);
}

Your AppleScript has to have an execute method (or a method, can have a different name, but in this case you are passing execute as the method name).

There is a good example in the docs about using the AppleScript script object.

You need something like:

let method = "execute";
let script = `on execute()
	do shell script "/usr/local/bin/pandoc -f markdown+smart --reference-doc=/Users/derekvan/Dropbox/CSS/out.docx '/Users/derekvan/Library/Mobile Documents/iCloud~com~agiletortoise~Drafts5/Documents/Temp/panTemp.md' -o ~/Desktop/test.docx"
end execute`;

Ok, thanks. I don’t understand the subroutine thing at all. Where does the “bodyHTML” come from in the example in the docs?

At any rate, I got it to work by using the AppleScript action instead of building it into a script.

Sorry, updated the example above. Passing parameters to the subroutine is optional. It’s how you get data directly from Drafts to the AppleScript, but since you are working with a file, that’s not necessary. Your call to run the script can just be:

runner.execute(method, [])

Thanks Greg. I understand the example you shared in the thread here. However, in the example in the docs you shared above:

I’m not sure where that variable “bodyHTML” in the execute line in the AppleScript comes from. It doesn’t appear anywhere else in the script. Is it something from the Safari AppleScript dictionary?

@derekvan, if you ever create an action or a finalised version would be great if you could share it!

thx!

Z

Action seems too specific to my environment to share to directory, but I can post here:

1st step: Script:

// pandoc to docx

let fm = FileManager.createCloud();

let text = draft.content;

fm.writeString("/Temp/panTemp.md",text);

2nd Step: Applescript:

-- AppleScript content
-- Editing and testing in Script Editor recommended
on execute(draft)
   do shell script "/usr/local/bin/pandoc -f markdown+smart  '/Users/username/Library/Mobile Documents/iCloud~com~agiletortoise~Drafts5/Documents/Temp/panTemp.md' -o ~/Documents/Action/temp.docx"
	tell application "Microsoft Word" to open POSIX file "/Users/username/Documents/Action/temp.docx"
end execute

I sort of wish I knew how to pipe the text of the draft directly into the AppleScript call to pandoc (without having to create a file in iCloud first), but I couldn’t get it to work after a few tries, and since this does work, it didn’t seem worth it to keep banging my head against it.