Anyone scripted Byword?

I have been trying to build an Applescript to save a file from Byword to a specific file that I have created. The bit that creates the folder and the file within it works fine. But I can’t seem to get Byword to save it.

theFile definitely exists and is writable.

I’ve tried

tell application "Byword"
	save document 1 in theFile as text
end tell

and save document 1 in theFile and save document 1 as theFile

The error I get is

Can’t make «class docf» "" of «class cfol» "test-the-script--again" of «class cfol» "" of «class cfol» "pages" of «class cfol» "user" of «class cfol» "grav-admin" of «class cfol» "htdocs" of «class cfol» "MAMP" of «class cfol» "Applications" of «class sdsk» of application "Finder" into the expected type.

I’d prefer to keep using Byword if possible, but if the only answer is to use some other Markdown editor, I suppose I would have to consider that.

All suggestions gratefully received.


You’re passing it a Finder file reference, which only Finder understands.

Using what you’ve already got, you can coerce the value of theFile to text, which will give you an HFS path. From this, you can obtain a posix path using POSIX path of ....

That is probably what ByWord is expecting, although I can’t be sure as I’ve never used it. If it doesn’t like a plain file path, you can use the POSIX file specifier to create a file object reference that isn’t specific to any particular application.

Here’s all of that in AppleScript:

set filepath to the POSIX path of (theFile as text)
tell application "Byword" to save document 1 in the filepath
--OR: save document 1 in the POSIX file filepath

Normally, you shouldn’t need to create the file beforehand. There may also be sandboxing issues if you try to save it at a location to which ByWord has no write access.

If it prefers an alias file reference, then this is one that does have to refer to a file that already exists. You can coerce a Finder file reference directly to alias:

set theFile to theFile as alias
tell application "Byword" to save document 1 in theFile

If possible, I’d suggest doing it using either a plain posix path, or with a POSIX file specifier, because that will allow you to remove the part of your script that utilises Finder, which is rarely anything more than a curse on all AppleScripts and all of their children.

This works for me, saving a file to a couple of files using HFS and POSIX paths.

tell application "Byword" need not exist first
	save document 1 in file "Macintosh" must exist first to create an alias to it
	set strPath to POSIX file "/Users/stephen/Desktop/" as alias
	save document 1 in file strPath
end tell

Hope that helps.

Thank you both very much indeed. In the end, I went with second solution, because the first gave me an error about the file being locked. The script I have, thanks to both of you, now does what I need it to do.

I confess, though, that my script still uses Finder to create the file. It may be an abomination, but it works. So, getting theText from the clipboard and massaging it, the working script is

tell application "Finder"
	set thePath to alias ""
	make new folder at thePath with properties {name:theText}
	set newPath to thePath & theText & ":" as string
	set theFile to make new file at newPath with properties {name:""}
end tell

set theFile to theFile as alias

tell application "Byword"
	save document 1 in the theFile
end tell