Open a Pages Document?

I'm only trying to open an existing Pages document. That's it. And I'm failing.

osascript -e 'set myPath to "/Volumes:Alex:Users:gmark:Desktop:Yao_.pages"';
osascript -e 'tell application "Pages" to open file myPath'

I've activated Pages and had no trouble traversing all the already-open files but can't seem to just open an existing file from inside the KM macro. I've seen examples where you use an alias for some reason I don't understand, but that fails as well.

What am I missing?

Have you considered using the Open a File, Folder or Application action?

There are better ways of opening a Pages document from within KM, as @Richard_Tench has pointed out. Even using an AppleScript action would be better than using a shell script action calling osascript.

However, for completeness and in case there's a compelling reason to use AppleScript from the shell for this: Your script runs two separate instances of osascript, and variables do not carry over between them -- it's like you are using two documents in Script Editor. Compare:

osascript -e 'set myPath to \"Hello\"';
osascript -e 'display dialog myPath'

osascript -e 'set myPath to "Hello"' -e 'display dialog myPath'
...or even:

osascript -e 'set myPath to "Hello"
display dialog myPath'

From man osascript: "Multiple -e options may be given to build up a multi-line script." But Terminal (and, I assume, KM's shell script editor) knows that any newlines between two 's that mark the start and end of the AS script are newlines within the AS script, not newlines in the shell, which is why the last form works.

If you are going to use a shell script line (an Execute a Shell Script action), then osascript is probably redundant.

You can just write a direct open , followed by a unix path, for example, some variant of:

open ~/Desktop/existingDoc.pages

and if your file path contains any spaces, one approach to quoting would be:

open "$HOME/Desktop/some existing document.pages"

which is probably simpler than escaping each space:

open ~/Desktop/some\ existing\ document.pages

That took care of it! Thanks, folks!

Now I still don't know why osascript was so often unable to open a file, but I was theorizing it might have something to do with the interface to iCloud Apple uses, and which the "open" command they wrote interfaces properly with. But my initial problem is solved!

Is your file path well-formed in the original post ?

Looks like a mixture of unix slashes and old applescript colon separators.